uart - MH-z19b stop working after 999999 millis (NodeMCU v3 ESP8266 12F) - Stack Overflow

时间: 2025-01-06 admin 业界

everyone! I'm trying to make a CO2 sencor and it works well, until its runtime exceeds 999999 milliseconds. My wiring is TXD0->RX, RXD0->TX, GND->GND and V USB->Vin pinout on picture. I also have Oled display connected, but don't think that is the issue.

This is the last part of my logs:

Loop millis = 990469
�␁�␀␀␀␀␀zCO2 (ppm): 731
Loop millis = 994562
�␁�␀␀␀␀␀zCO2 (ppm): 728
Loop millis = 998660
�␁�␀␀␀␀␀zCO2 (ppm): 727
Loop millis = 1002753
�␁�␀␀␀␀␀z!Error: Timed out waiting for response
CO2 (ppm): 0
Loop millis = 1007313
�␁�␀␀␀␀␀z!Error: Timed out waiting for response
CO2 (ppm): 0
Loop millis = 1011873
�␁�␀␀␀␀␀z!Error: Timed out waiting for response

And here is my code:

#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSerif9pt7b.h>
#include "MHZ19.h"

#define RX_PIN 3 
#define TX_PIN 1                                          // Tx pin which the MHZ19 Rx pin is attached to
#define BAUDRATE 74880                                      // Device to MH-Z19 Serial baudrate (should not be changed)

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

MHZ19 myMHZ19;                                             // Constructor for library
#if defined(ESP32)
HardwareSerial mySerial(2);                                // On ESP32 we do not require the SoftwareSerial library, since we have 2 USARTS available
#else
#include <SoftwareSerial.h>                                //  Remove if using HardwareSerial or non-uno compatible device
SoftwareSerial mySerial(RX_PIN, TX_PIN);                   // (Uno example) create device to MH-Z19 serial
#endif

unsigned long getDataTimer = 0;

void setup()
{
    Serial.begin(9600);                                     // Device to serial monitor feedback

    if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
        Serial.println("SSD1306 allocation failed");
        for(;;);
      }
      delay(1000);
      display.setFont(&FreeSerif9pt7b);
      display.clearDisplay();
      display.setTextSize(1);             
      display.setTextColor(WHITE);        
      display.setCursor(0,30);             
      display.println("Meteostation");
      display.println("ON");
      display.display();

    myMHZ19.begin(Serial);                                // *Serial(Stream) reference must be passed to library begin().

    myMHZ19.autoCalibration(false);                              // Turn auto calibration ON (OFF autoCalibration(false))

    Serial.print("Setup");

    delay(1000);

  /*Callibration on 400 ppm
    delay(60000);
    myMHZ19.calibrate();
    delay(10000);*/

    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
    
}

void loop()
{

   if (millis() - getDataTimer >= 4000)
    {

        Serial.print("Loop millis = ");
        Serial.println(millis());
        
        int CO2;

        //note: getCO2() default is command "CO2 Unlimited". This returns the correct CO2 reading even
        //if below background CO2 levels or above range (useful to validate sensor). You can use the
        //usual documented command with getCO2(false) 

        CO2 = myMHZ19.getCO2();                             // Request CO2 (as ppm)

        Serial.print("CO2 (ppm): ");
        Serial.println(CO2);

        display.clearDisplay();      
        display.setCursor(0,15);
        display.setTextSize(1);               
        display.println("CO2 (ppm): ");
        display.println();
        display.setTextSize(2);  
        display.println(CO2);
        display.display();

        getDataTimer = millis();
    }
}

It doesn't bother me now, but I don't understand what these strange symbols are written in the logs "�␁�␀␀␀␀␀z".

Thanks for any help!

I've checked the wiring and found that MH-Z19b sensor was still receiving 5v power.

P.S. I'm confused, if I add this line Serial.println(millis() - getDataTimer); at the beginning of if statement (you can see below) -> this error is occured !Error: Timed out waiting for response from the start.

 if (millis() - getDataTimer >= 4000)
    {
        Serial.println(millis() - getDataTimer);  //added line

        Serial.print("Loop millis = ");
        Serial.println(millis());
        .
        .
        .

But, if add the same line at the end of if statement (like below) -> the code works in same way as before.

      if (millis() - getDataTimer >= 4000)
     {
        Serial.print("Loop millis = ");
        Serial.println(millis());
        .
        .
        .
        Serial.println(millis() - getDataTimer); //added line

        getDataTimer = millis();

        
    }

And as a result I can see that the interval (millis() - getDataTimer) increased on 500 ms after exceeding the 999999 value. May be this is the issue. (see logs below)

Loop millis = 994631
�␁�␀␀␀␀␀zCO2 (ppm): 994
4093
Loop millis = 998724
�␁�␀␀␀␀␀zCO2 (ppm): 986
4093
Loop millis = 1002817
�␁�␀␀␀␀␀z!Error: Timed out waiting for response
CO2 (ppm): 0
4560
Loop millis = 1007377
�␁�␀␀␀␀␀z!Error: Timed out waiting for response
CO2 (ppm): 0
4560

So, I think I have some misunderstanding of how does it works.

最新文章