0

I'm trying make an mqtt co2 sensor for home assistant. And I got it to work, but only for a couple of hours. At some point it starts 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);
}

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.

hcheung
  • 3,377
  • 3
  • 11
  • 23
user1908460
  • 193
  • 1
  • 1
  • 9
  • 1
    I'm not familiar with the sensor, but I took a look at the source code of the library, `sensor.getCarbonDioxide()` will return -1 if it is `!isRead()` return false, see the [source code](https://github.com/malokhvii-eduard/arduino-mhz19/blob/master/src/Mhz19.cpp#L39-L43). – hcheung Sep 14 '22 at 00:21

1 Answers1

-1

The sensor may not be receiving enough current. Try connecting the sensor directly to a separate breadboard power supply. The sensor seems to have a warm up time. Try waiting for longer with the independent power supply connected. I hope this will solve your issue.

Thank You, Naveen PS

Naveen PS
  • 13
  • 5
  • None of these apply to the OP case as OP mentioned that it works for "a couple of hours". So definitely not related to power and boot up/warm up time. – hcheung Sep 16 '22 at 01:25