Skip to content

Instantly share code, notes, and snippets.

@ka-ka-xyz
Last active July 13, 2019 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ka-ka-xyz/e952bf55e65f77b842f9654ab2639d27 to your computer and use it in GitHub Desktop.
Save ka-ka-xyz/e952bf55e65f77b842f9654ab2639d27 to your computer and use it in GitHub Desktop.
#include <cppQueue.h>
#include <SoftwareSerial.h>
// Read data from MH-Z14 CO2 sensor.
// ref. https://www.openhacks.com/uploadsproductos/mh-z14_co2.pdf
// ref. https://gist.github.com/takashiski/3a1c1da6b9aac8863696d960660461f3
// RX: the port recieve bytes from sensor.
// connect to sensor port 19 (TX)
uint8_t co2sRx = 12;
// TX: the port send bytes to sensor
// connect to sensor port 18 (RX)
uint8_t co2sTx = 13;
SoftwareSerial co2Sensor(co2sRx, co2sTx);
byte co2sInitCmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
byte co2sInBuf[9] = {0,0,0,0,0,0,0,0,0};
uint8_t btRx = 8;
// TX: the port send bytes to sensor
// connect to sensor port 19 (RX)
uint8_t btTx = 9;
SoftwareSerial bt(btRx, btTx);
uint8_t interval = 10;
int16_t co2 = 0;
void setup() {
Serial.begin(9600);
Serial.println("start to setup.");
co2Sensor.begin(9600);
Serial.println("co2Sensor initialized");
}
void loop() {
co2Sensor.write(co2sInitCmd, 9);
co2Sensor.flush();
co2 = 0;
if (co2Sensor.available()) {
co2Sensor.readBytes(co2sInBuf,9);
co2 += co2sInBuf[2] <<8;
co2 += co2sInBuf[3];
Serial.println(co2);
} else {
Serial.println("co2Sensor is NOT available!");
}
delay(interval * 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment