MH-z19 giving -1 value after some time

I'm trying make an mqtt co2 Sensor for the home assistant. And I got it to work, but only for a couple of hours. At some point it start giving me -1 values instead of the proper co2 value. What could this be?

Below my code:

#include <Arduino.h>
#include <Mhz19.h>
#include <SoftwareSerial.h>

SoftwareSerial softwareSerial(D3, D4);
Mhz19 sensor;


#include <ESP8266WiFi.h>
#include <PubSubClient.h>
 
const char* ssid = "ID";
const char* password = "pw";
const char* MQTT_BROKER = "192.168.1.25";//"test.mosquitto.org";
int mqttPort = 1883;
const char* mqttUser ="mqtt-user"; 
const char* mqttPassword ="pw"; 

WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi() {
  delay(100);
  // Connect to Wi-Fi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  randomSeed(micros());
  
  Serial.println("");
  Serial.println("Connected to Wi-Fi");
}

void reconnect() {
  
  // Loop until connected
  while (!client.connected()) {
    Serial.print("Connecting to MQTT...");
    if (client.connect("Test1", mqttUser, mqttPassword )) {
      Serial.println("Connected to MQTT");  
    } else {
      Serial.print("Failed MQTT connection with state: ");
      Serial.println(client.state());
      // Re-try in 3 seconds
      delay(3000);
    }
  }
  
  // Once connected, publish an announcement, and subscribe
  client.publish("sensor/test", "Hello from ...");
  client.subscribe("sensor/test");
}





void setup() {
  Serial.begin(115200);
  setup_wifi();   // Connect to Wi-Fi network
  client.setServer(MQTT_BROKER, mqttPort);
  //client.setCallback(callback);

  reconnect();    // Connect to MQTT broker

  
  softwareSerial.begin(9600);

  sensor.begin(&softwareSerial);
  sensor.setMeasuringRange(Mhz19MeasuringRange::Ppm_5000);
  sensor.enableAutoBaseCalibration();

  Serial.println("Preheating...");  // Preheating, 3 minutes
  while (!sensor.isReady()) {
    delay(50);
  }

  Serial.println("Sensor Ready...");
}

void loop() {
  if (!client.connected()) { reconnect(); }
  
  //client.publish("Time", ctime(&now));
  
  
  auto carbonDioxide = sensor.getCarbonDioxide();
  if (carbonDioxide >= 0) {
    Serial.println(String(carbonDioxide) + " ppm");
  }
  char co2String[8];
  dtostrf(carbonDioxide, 1,0, co2String);
  //Serial.println(co2String);
  
  client.publish("Floating/climate/CO2", co2String);

  delay(2000);
}

Custom Lithium Ion Battery Pack Manufacturer
I also tried flashing it via ESPHome, but then I run into issues from UART. The chip doesn't work propperly as long as the sensor is connected to it.

You could test if the sensor becomes "un-ready":

auto carbonDioxide = sensor.getCarbonDioxide();
  if (carbonDioxide >= 0) {
    Serial.println(String(carbonDioxide) + " ppm");
  } else if (!sensor.isReady()) carbonDioxide = -2;

That would give a reading of -2 if the sensor is not ready, meaning that you would have to wait for it to become ready again. If the sensor is ready and still reading -1, you could try to make a complete re-initialization of the SoftwareSerial and the sensor in order to revive it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.