mirror of
https://github.com/sigmasternchen/MH-Z-CO2-Sensors
synced 2025-03-15 06:38:55 +00:00
Added a debug mode and improved log messages.
This commit is contained in:
parent
ddef5ff84f
commit
29ea8e92bc
3 changed files with 97 additions and 47 deletions
24
MH-Z19B.ino
24
MH-Z19B.ino
|
@ -12,24 +12,30 @@
|
|||
|
||||
MHZ co2(MH_Z19_RX, MH_Z19_TX, CO2_IN, MHZ19B);
|
||||
|
||||
unsigned long startTime = millis();
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
pinMode(CO2_IN, INPUT);
|
||||
delay(100);
|
||||
Serial.println("MHZ 19B");
|
||||
|
||||
co2.setDebug(false);
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
Serial.print("Time from start: ");
|
||||
Serial.print((millis() - startTime) / 1000);
|
||||
Serial.println(" s");
|
||||
// Serial.print("\n----- Time from start: ");
|
||||
// Serial.print(millis() / 1000);
|
||||
// Serial.println(" s");
|
||||
int ppm_uart = co2.readCO2UART();
|
||||
int ppm_pwm = co2.readCO2PWM();
|
||||
// int ppm_pwm = co2.readCO2PWM();
|
||||
int temperature = co2.getLastTemperature();
|
||||
Serial.print("PPMuart: ");
|
||||
Serial.print(ppm_uart);
|
||||
// Serial.print(", PPMpwm: ");
|
||||
// Serial.print(ppm_pwm);
|
||||
Serial.print(", Temperature: ");
|
||||
Serial.println(temperature);
|
||||
|
||||
Serial.println("\n------------------------------");
|
||||
delay(5000);
|
||||
// Serial.println("\n------------------------------");
|
||||
// delay(3000);
|
||||
}
|
||||
|
|
112
MHZ.cpp
112
MHZ.cpp
|
@ -18,67 +18,105 @@ MHZ:: MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type)
|
|||
}
|
||||
|
||||
|
||||
void MHZ::setDebug(boolean enable) {
|
||||
debug = enable;
|
||||
if (debug) {
|
||||
Serial.println("MHZ: debug mode ENABLED");
|
||||
} else {
|
||||
Serial.println("MHZ: debug mode DISABLED");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int MHZ::readCO2UART() {
|
||||
Serial.println("-- read CO2 uart ---");
|
||||
if (debug) Serial.println("-- read CO2 uart ---");
|
||||
byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
|
||||
byte response[9]; // for answer
|
||||
|
||||
Serial.println("Sending CO2 request...");
|
||||
if (debug) Serial.print(" >> Sending CO2 request");
|
||||
co2Serial.write(cmd, 9); //request PPM CO2
|
||||
|
||||
// clear the buffer
|
||||
memset(response, 0, 9);
|
||||
int i = 0;
|
||||
|
||||
while (co2Serial.available() == 0) {
|
||||
// Serial.print("Waiting for response ");
|
||||
// Serial.print(i);
|
||||
// Serial.println(" s");
|
||||
delay(1000);
|
||||
i++;
|
||||
if (debug) Serial.print(".");
|
||||
delay(5); // wait a short moment to avoid false reading
|
||||
}
|
||||
if (debug) Serial.println();
|
||||
|
||||
// The serial stream can get out of sync. The response starts with 0xff, try to resync.
|
||||
// TODO: I think this is wrong any only happens during initialization
|
||||
boolean skip = false;
|
||||
while (co2Serial.available() > 0 && (unsigned char)co2Serial.peek() != 0xFF)
|
||||
{
|
||||
if (!skip) {
|
||||
Serial.print("MHZ: - skipping unexpected readings:");
|
||||
skip = true;
|
||||
}
|
||||
Serial.print(" ");
|
||||
Serial.print(co2Serial.peek(), HEX);
|
||||
co2Serial.read();
|
||||
}
|
||||
if (skip) Serial.println();
|
||||
|
||||
|
||||
if (co2Serial.available() > 0) {
|
||||
co2Serial.readBytes(response, 9);
|
||||
}
|
||||
// print out the response in hexa
|
||||
for (int i = 0; i < 9; i++) {
|
||||
Serial.print(String(response[i], HEX));
|
||||
Serial.print(" ");
|
||||
|
||||
if (debug) {
|
||||
// print out the response in hexa
|
||||
Serial.print(" << ");
|
||||
for (int i = 0; i < 9; i++) {
|
||||
Serial.print(response[i], HEX);
|
||||
Serial.print(" ");
|
||||
}
|
||||
Serial.println("");
|
||||
}
|
||||
Serial.println("");
|
||||
|
||||
// checksum
|
||||
byte check = getCheckSum(response);
|
||||
if (response[8] != check) {
|
||||
Serial.println("Checksum not OK!");
|
||||
Serial.print("Received: ");
|
||||
Serial.println(response[8]);
|
||||
Serial.print("Should be: ");
|
||||
Serial.println(check);
|
||||
Serial.println("MHZ: Checksum not OK!");
|
||||
Serial.print("MHZ: Received: ");
|
||||
Serial.println(response[8], HEX);
|
||||
Serial.print("MHZ: Should be: ");
|
||||
Serial.println(check, HEX);
|
||||
}
|
||||
|
||||
// ppm
|
||||
int ppm_uart = 256 * (int)response[2] + response[3];
|
||||
Serial.print("PPM UART: ");
|
||||
Serial.println(ppm_uart);
|
||||
|
||||
// temp
|
||||
byte temp = response[4] - 40;
|
||||
Serial.print("Temperature? ");
|
||||
Serial.println(temp);
|
||||
temperature = response[4] - 44; // - 40;
|
||||
|
||||
// status
|
||||
byte status = response[5];
|
||||
Serial.print("Status? ");
|
||||
Serial.println(status);
|
||||
if (status == 0x40) {
|
||||
Serial.println("Status OK");
|
||||
if (debug) {
|
||||
Serial.print(" # PPM UART: ");
|
||||
Serial.println(ppm_uart);
|
||||
Serial.print(" # Temperature? ");
|
||||
Serial.println(temperature);
|
||||
|
||||
// status
|
||||
// not sure about this...
|
||||
byte status = response[5];
|
||||
Serial.print(" Status? ");
|
||||
Serial.print(status);
|
||||
if (status == 0x40) {
|
||||
Serial.println(" - Status OK");
|
||||
} else {
|
||||
Serial.println(" ! Status not OK !");
|
||||
}
|
||||
}
|
||||
|
||||
return ppm_uart;
|
||||
}
|
||||
|
||||
uint8_t MHZ::getLastTemperature() {
|
||||
return temperature;
|
||||
}
|
||||
|
||||
byte MHZ::getCheckSum(byte *packet) {
|
||||
Serial.println("-- get checksum ---");
|
||||
if (debug) Serial.println(" getCheckSum()");
|
||||
byte i;
|
||||
unsigned char checksum = 0;
|
||||
for (i = 1; i < 8; i++) {
|
||||
|
@ -90,15 +128,17 @@ byte MHZ::getCheckSum(byte *packet) {
|
|||
}
|
||||
|
||||
int MHZ::readCO2PWM() {
|
||||
Serial.println("-- read CO2 pwm ---");
|
||||
if (debug) Serial.print("-- reading CO2 from pwm ");
|
||||
unsigned long th, tl, ppm_pwm = 0;
|
||||
do {
|
||||
Serial.print(".");
|
||||
if (debug) Serial.print(".");
|
||||
th = pulseIn(_pwmpin, HIGH, 1004000) / 1000;
|
||||
tl = 1004 - th;
|
||||
ppm_pwm = 5000 * (th - 2) / (th + tl - 4);
|
||||
} while (th == 0);
|
||||
Serial.print("PPM PWM: ");
|
||||
Serial.println(ppm_pwm);
|
||||
if (debug) {
|
||||
Serial.print("\n # PPM PWM: ");
|
||||
Serial.println(ppm_pwm);
|
||||
}
|
||||
return ppm_pwm;
|
||||
}
|
||||
|
|
8
MHZ.h
8
MHZ.h
|
@ -24,14 +24,18 @@ class MHZ {
|
|||
public:
|
||||
MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type);
|
||||
|
||||
void setDebug(boolean enable);
|
||||
|
||||
int readCO2UART();
|
||||
int readCO2PWM();
|
||||
uint8_t getLastTemperature();
|
||||
|
||||
private:
|
||||
uint8_t _rxpin, _txpin, _pwmpin, _type;
|
||||
uint8_t _rxpin, _txpin, _pwmpin, _type, temperature;
|
||||
boolean debug = false;
|
||||
|
||||
SoftwareSerial co2Serial;
|
||||
byte getCheckSum(byte *packet);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue