Co2 sensor:MH-Z19, Blynk, ESP

Hi i have CO2 sensor:,mh-z19. I need sending data to app Blynk. But this sketch no working.?
And sketch for thingspek from: https://github.com/asvavilov/iot-climate is working good.
Thanks.


#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

#include <Wire.h>
#include <SoftwareSerial.h>;


// MH-Z19:
// power - 5v
// D6 - TX sensor, D7 - RX sensor
#define CO2_TX D6
#define CO2_RX D7

SoftwareSerial co2Serial(CO2_TX, CO2_RX);


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";  // Put your Auth Token here. (see Step 3 above)

SimpleTimer timer;

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
   Blynk.begin(auth, "", ""); //insert here your SSID and password
 
  // Setup a function to be called every second
  timer.setInterval(10000L, sendUptime);

  co2Serial.begin(9600);
  co2Serial.flush();
}


int readCO2()
{
  byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
  char response[9];

  co2Serial.write(cmd, 9); //request PPM CO2
  co2Serial.readBytes(response, 9);


  byte crc = 0;
  for (int i = 1; i < 8; i++)
  {
    crc += response[i];
  }
  crc = 255 - crc;
  crc++;

  if (response[8] != crc)
  {
    Serial.println("Wrong crc from co2 sensor!");
    return -1;
  }
  if (response[0] != 0xFF)
  {
    Serial.println("Wrong starting byte from co2 sensor!");
    return -1;
  }
  if (response[1] != 0x86)
  {
    Serial.println("Wrong command from co2 sensor!");
    return -1;
  }

  int responseHigh = (int) response[2];
  int responseLow = (int) response[3];
  int ppm = (256 * responseHigh) + responseLow;
  return ppm;
}

void sendUptime()
{
  float co2_ppm = co2Serial.readCO2();

  Blynk.virtualWrite(1, co2_ppm); // virtual pin 
  
}

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

error:


blynk_co2.ino: In function 'void sendUptime()':
blynk_co2:80: error: 'class SoftwareSerial' has no member named 'readCO2'
blynk_co2:82: error: 'ppm' was not declared in this scope
'class SoftwareSerial' has no member named 'readCO2'

@Jiri_Bam in the original sketch it includes this line:

int co2_ppm = readCO2();

You decided to change it to this buggy line:

float co2_ppm = co2Serial.readCO2();

Change it back and the code will compile.

For reference the original sketch and yours also have:

#include <SoftwareSerial.h>;

include statements are not terminated with ; but most compilers will handle it. Remove the ;

So YES now is sketch working. And SoftwareSerial i no remove.
But in blynk is CO2 exploring: 998.0000 i want 998 without .0000

THanks.

#define BLYNK_PRINT Serial    
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

#include <Wire.h>
#include <SoftwareSerial.h>;

// MH-Z19:
// power - 5v
// D6 - TX sensor, D7 - RX sensor
#define CO2_TX D6
#define CO2_RX D7

SoftwareSerial co2Serial(CO2_TX, CO2_RX);

char auth[] = "";  // Put your Auth Token here. (see Step 3 above)

SimpleTimer timer;

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
   Blynk.begin(auth, "", ""); //insert here your SSID and password
 
  // Setup a function to be called every second
  timer.setInterval(10000L, sendUptime);

  co2Serial.begin(9600);
  co2Serial.flush();
}
int readCO2()
{

  byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
  char response[9];

  co2Serial.write(cmd, 9); //request PPM CO2
  co2Serial.readBytes(response, 9);
  
  byte crc = 0;
  for (int i = 1; i < 8; i++)
  {
    crc += response[i];
  }
  crc = 255 - crc;
  crc++;

  if (response[8] != crc)
  {
    Serial.println("Wrong crc from co2 sensor!");
    return -1;
  }
  if (response[0] != 0xFF)
  {
    Serial.println("Wrong starting byte from co2 sensor!");
    return -1;
  }
  if (response[1] != 0x86)
  {
    Serial.println("Wrong command from co2 sensor!");
    return -1;
  }
  int responseHigh = (int) response[2];
  int responseLow = (int) response[3];
  int ppm = (256 * responseHigh) + responseLow;
  return ppm;
}

void sendUptime()
{
  float co2_ppm = readCO2();
  Blynk.virtualWrite(1, co2_ppm); // virtual pin 
}
void loop()
{
  Blynk.run();
  timer.run();
}

Spend a few days reading the docs and it will all become clear to you.

I dont know but in Gauge it is good: 998. And in Labeled Value are exploring 998.0000…
Thanks.

Because you declare it as float.

float co2_ppm = readCO2();

Change to int

1 Like