100 #ifdef MHZ19B_SMART_WARMING_UP
101 static int16_t _lastCO2 = -1;
106 if (_lastCO2 == -1) {
109 if (_lastCO2 != co2) {
163 _tLastReadCO2 = millis();
171 result = (rxBuffer[2] << 8) | rxBuffer[3];
194 if (versionLen < 5) {
199 memset(version, 0, 5);
207 for (uint8_t i = 0; i < 4; i++) {
209 version[i] = rxBuffer[i + 2];
262 result = (rxBuffer[4] << 8) | rxBuffer[5];
306 result = rxBuffer[7] & 0x01;
349 unsigned long tStart;
352 if (_serial ==
nullptr) {
357 txBuffer[8] = calcCRC(txBuffer);
360 while (_serial->available()) {
365 _serial->write(txBuffer,
sizeof(txBuffer));
371 memset(rxBuffer, 0,
sizeof(rxBuffer));
385 if ((rxBuffer[0] != 0xFF) || (rxBuffer[1] != cmd)) {
390 if (rxBuffer[8] != calcCRC(rxBuffer)) {
409 uint8_t ErriezMHZ19B::calcCRC(uint8_t *data)
414 for (uint8_t i = 1; i < 8; i++) {
MH-Z19B CO2 sensor library for Arduino.
#define MHZ19B_SERIAL_RX_BYTES
Fixed 9 Bytes response.
#define MHZ19B_CMD_GET_VERSION
Command get firmware version (NOT DOCUMENTED)
#define MHZ19B_CMD_GET_RANGE
Command get range detection (NOT DOCUMENTED)
#define MHZ19B_CMD_SET_RANGE
Command set detection range.
@ MHZ19B_RANGE_5000
Range 5000 ppm (Default)
@ MHZ19B_RANGE_2000
Range 2000 ppm.
#define MHZ19B_READ_INTERVAL_MS
Minimum response time between CO2 reads (EXPERIMENTALLY DEFINED)
@ MHZ19B_RESULT_ERR_TIMEOUT
Response timeout.
@ MHZ19B_RESULT_OK
Response OK.
@ MHZ19B_RESULT_ERR_CRC
Response CRC error.
@ MHZ19B_RESULT_ARGUMENT_ERROR
Response argument error.
@ MHZ19B_RESULT_ERROR
Response error.
#define MHZ19B_WARMING_UP_TIME_MS
3 minutes warming-up time after power-on before valid data returned
#define MHZ19B_CMD_READ_CO2
Command read CO2 concentration.
#define MHZ19B_CMD_GET_AUTO_CAL
Command get auto calibration status (NOT DOCUMENTED)
#define MHZ19B_CMD_CAL_ZERO_POINT
Command calibrate zero point at 400ppm.
#define MHZ19B_SERIAL_RX_TIMEOUT_MS
Response timeout between 15..120 ms at 9600 baud works reliable for all commands.
#define MHZ19B_CMD_SET_AUTO_CAL
Command set auto calibration on/off.
int8_t setAutoCalibration(bool calibrationOn)
Enable or disable automatic calibration.
int8_t sendCommand(uint8_t cmd, byte b3=0, byte b4=0, byte b5=0, byte b6=0, byte b7=0)
Send serial command to sensor and read response.
int8_t getAutoCalibration()
Get status automatic calibration (NOT DOCUMENTED)
int8_t startZeroCalibration()
Start Zero Point Calibration manually at 400ppm.
bool isWarmingUp()
Check if sensor is warming-up after power-on.
ErriezMHZ19B(Stream *serial)
Constructor with serial Stream.
bool detect()
Detect MHZ19B sensor.
int8_t setRange2000ppm()
Set CO2 range 2000 ppm.
bool isReady()
Check minimum interval between CO2 reads.
int16_t getRange()
Get CO2 range in PPM (NOT DOCUMENTED)
int8_t getVersion(char *version, uint8_t versionLen)
Get firmware version (NOT DOCUMENTED)
int8_t setRange5000ppm()
Set CO2 range 5000 ppm.
~ErriezMHZ19B()
Destructor.
int16_t readCO2()
Read CO2 from sensor.