Hello All
Was just testing the MH-Z19 module and am having some issues.
Using UART to connect to esp8266
#define CO2_TX D6
#define CO2_RX D7`
also powered using 3V and Ground.
Got it setup fine and connected to blynk server…it says -1 for a minute then around 420 ppm which is probably about right but after about 5 minutes it just goes to 5000 ppm and stays there.
its a 5000 ppm sensor so Im not sure if this is a code/calculation error or faulty sensor or what…
here is the sketch im using
#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 - 3v
// 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[] = “redacted”; // 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, “xx”, “xx”); //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()
{
int co2_ppm = readCO2();
Blynk.virtualWrite(3, co2_ppm); // virtual pin
}
void loop()
{
Blynk.run();
timer.run();
}
any ideas?
Thanks!