Last active
January 22, 2019 09:03
-
-
Save didasy/c33a112d31445f08ea3b4a06a0d738cd to your computer and use it in GitHub Desktop.
Read MH-Z19 CO2 Sensor Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make sure to wait the sensor to heat up first before attempting to get CO2 concentration data for the first time (approx 3 minutes preheat time). | |
int readCO2() | |
{ | |
byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; | |
// command to ask for data | |
byte response[9]; // for answer | |
// co2Serial is a software serial instance | |
co2Serial.write(cmd, 9); //request PPM CO2 | |
// The serial stream can get out of sync. The response starts with 0xff, try to resync. | |
while (co2Serial.available() > 0 && (unsigned char)co2Serial.peek() != 0xFF) { | |
co2Serial.read(); | |
} | |
memset(response, 0, 9); | |
co2Serial.readBytes(response, 9); | |
if (response[1] != 0x86) | |
{ | |
Serial.println("Invalid response from co2 sensor!"); | |
return -1; | |
} | |
byte crc = 0; | |
for (int i = 1; i < 8; i++) { | |
crc += response[i]; | |
} | |
crc = 255 - crc + 1; | |
if (response[8] == crc) { | |
int responseHigh = (int) response[2]; | |
int responseLow = (int) response[3]; | |
int ppm = (256 * responseHigh) + responseLow; | |
return ppm; | |
} else { | |
Serial.println("CRC error!"); | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@TehreemEE
So you basically short Hd and Gnd pins for 7s outdoor.
arendst/Tasmota#3860