Wemos d1 mini + sen0177 + Blynk

I have problem with this project. Cant set Wemos D1 mini with sensor SEN01777 to sent data for Blynk server :confused:

 #include <SoftwareSerial.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define LENG 31   //0x42 + 31 bytes equal to 32 bytes
unsigned char buf[LENG];
 
static const int RXPin = 3, TXPin = 1;
 
int PM01Value=0;          //define PM1.0 value of the air detector module
int PM2_5Value=0;         //define PM2.5 value of the air detector module
int PM10Value=0;         //define PM10 value of the air detector module
 
SoftwareSerial PMSerial(RXPin, TXPin);  // RX, TX
BlynkTimer timer;
 
// char auth[] = "0ee7c91ad7b7485f821a9d0c89230b91"; // galaxy s8+ blynk auth
// char ssid[] = "911436-2.4G"; // home wifi
// char pass[] = "280446199"; // home pass
 
 
char auth[] = "0ee7c91ad7b7485f821a9d0c89230b91"; // galaxy s8+ blynk auth
char ssid[] = "TEST"; // home wifi
char pass[] = "marcin123"; // home pass
 
 
void setup()
{
  PMSerial.begin(9600);  
  PMSerial.setTimeout(1500);
  Blynk.begin(auth, ssid, pass);  
  Serial.begin(9600);
  Serial.println();
}
 
void loop()
{
  if(PMSerial.find(0x42)){    
    PMSerial.readBytes(buf,LENG);
 
    if(buf[0] == 0x4d){
      if(checkValue(buf,LENG)){
        PM01Value=transmitPM01(buf); //count PM1.0 value of the air detector module
        PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module
        PM10Value=transmitPM10(buf); //count PM10 value of the air detector module
      }          
    }
  }
 
  static unsigned long OledTimer=millis();  
    if (millis() - OledTimer >=1000)
    {
      OledTimer=millis();
     
      Serial.print("PM1.0: ");  
      Serial.print(PM01Value);
      Serial.println("  ug/m3");
      Blynk.virtualWrite(V4, PM01Value);  // Display PM1 Value
   
      Serial.print("PM2.5: ");  
      Serial.print(PM2_5Value);
      Serial.println("  ug/m3");
      Blynk.virtualWrite(V5, PM2_5Value);  // Display PM2.5 Value    
     
      Serial.print("PM1 0: ");  
      Serial.print(PM10Value);
      Serial.println("  ug/m3");  
      Serial.println();
      Blynk.virtualWrite(V5, PM10Value);  // Display PM10 Value
      Blynk.run();
      timer.run();
    }
 
}
char checkValue(unsigned char *thebuf, char leng)
{  
  char receiveflag=0;
  int receiveSum=0;
 
  for(int i=0; i<(leng-2); i++){
  receiveSum=receiveSum+thebuf[i];
  }
  receiveSum=receiveSum + 0x42;
 
  if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1]))  //check the serial data
  {
    receiveSum = 0;
    receiveflag = 1;
  }
  return receiveflag;
}
 
int transmitPM01(unsigned char *thebuf)
{
  int PM01Val;
  PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
  return PM01Val;
}
 
//transmit PM Value to PC
int transmitPM2_5(unsigned char *thebuf)
{
  int PM2_5Val;
  PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
  return PM2_5Val;
  }
 
//transmit PM Value to PC
int transmitPM10(unsigned char *thebuf)
{
  int PM10Val;
  PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module  
  return PM10Val;
}

And a problem with following directions :stuck_out_tongue_winking_eye:

I fixed your posts formatting as required.

Also look at this document…

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/keep-your-void-loop-clean

@Marcin_Liskowski you will definitely have a problem with your project using that “cut and paste” code!

I’m by no means an expert programmer but I do have a lot of experience of air quality sensors and TTL level serial communication. I don’t know this SEN01777 sensor but the code you are using is designed for the Plantower range which I do know a lot about.

I suggest you start from the beginning, use the Blynk libraries but forget about trying to write to Blynk and setting up your UI. Start by understanding and perfecting the sensor part first, output the data to the serial monitor and when you are happy with that part THEN start “Blynkyfying” the project.

So now some advice, the golden rule with Blynk is to “KEEP YOUR VOID LOOP CLEAN”, for now there should only be 2 lines in your void loop ().

void loop ()
{
Blynk.run();
timer.run();
}

Every other routine should be handled by timers. The way you have written your code means that the sensor is being read 1000’s of times in 1 second! Firstly, the sensor sends data approximately every 2.3 seconds so you only need to set up a timer to read the data every 2.3 seconds and secondly, as you have it now (if it works at all!) you will be sending Blynk.virtualWrite commands 1000’s of times a second which will immediately block your account.

So, put the sensor code in a separate void routine and call that routine with a timer every say 2 seconds. Then add Serial.print commands to output the data to your serial monitor and when you are satisfied the data is good and reliable then start thinking about sending data to Blynk and your UI.

Richard

1 Like