Chat freely about anything...

User avatar
By fatal56ty
#89454 I have wemos D1 mini with ESP8266 and CO2 sensor MH-Z19b. When I upload sketch from PC, reading from sensor works fine, but when I turn off wemos and then on, reading from sensor not work. This happens regularly, after upload OK, after OFF/ON error. Im trying usb power and also external 5 V 2A power suplpy. Library softwareserial is from https://github.com/plerup/espsoftwareserial .

Part of code, where is reading from serial.

Code: Select all// D5 -> TX na MH-Z19B
// D6 -> RX na MH-Z19B
SoftwareSerial co2_senzor(D5, D6);

// measure CO2
int16_t zmeritKoncentraci() {
  // send 9B instruction with request co2 value
  uint8_t command[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
  co2_senzor.write(command, sizeof(command));

  // read 9B from serial
  uint8_t answer[9];
           
  co2_senzor.readBytes(answer, sizeof(answer));

  if ((answer[0] == 0xFF) && (answer[1] == 0x86)) {

    // checksum
    uint8_t ks = 0;
    for (uint8_t i = 1; i < 8; i++) {
      ks += answer[i];
    }
    ks = (255 - ks) + 1;

    if (answer[8] == ks) {
      uint16_t ppm = (answer[2] << 8) ^ answer[3];
      return ppm;
    }
    else {
      return -1;
    }
  }
  else {
    return -1;
  }
}


So, where could be problem? Can you help me with debug serial message- how can i serial.print answer in raw format?