diff --git a/MH-Z19B.ino b/MH-Z19B.ino index 308991b..b0d54d6 100644 --- a/MH-Z19B.ino +++ b/MH-Z19B.ino @@ -1,110 +1,35 @@ + + +#include +#include +#include "MHZ.h" + #define CO2_IN D2 #define MH_Z19_RX D7 #define MH_Z19_TX D6 -#include -SoftwareSerial co2Serial(MH_Z19_RX, MH_Z19_TX); // define MH-Z19 RX TX + +MHZ co2(MH_Z19_RX, MH_Z19_TX, CO2_IN, MHZ19B); unsigned long startTime = millis(); + void setup() { Serial.begin(9600); - co2Serial.begin(9600); pinMode(CO2_IN, INPUT); + delay(100); + Serial.println("MHZ 19B"); } + void loop() { Serial.print("Time from start: "); Serial.print((millis() - startTime) / 1000); Serial.println(" s"); - int ppm_uart = readCO2UART(); - int ppm_pwm = readCO2PWM(); + int ppm_uart = co2.readCO2UART(); + int ppm_pwm = co2.readCO2PWM(); Serial.println("\n------------------------------"); delay(5000); } - -int readCO2UART() { - 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..."); - 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 (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(" "); - } - 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); - } - - // 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); - - // status - byte status = response[5]; - Serial.print("Status? "); - Serial.println(status); - if (status == 0x40) { - Serial.println("Status OK"); - } - - return ppm_uart; -} - -byte getCheckSum(byte *packet) { - Serial.println("-- get checksum ---"); - byte i; - unsigned char checksum = 0; - for (i = 1; i < 8; i++) { - checksum += packet[i]; - } - checksum = 0xff - checksum; - checksum += 1; - return checksum; -} - -int readCO2PWM() { - Serial.println("-- read CO2 pwm ---"); - unsigned long th, tl, ppm_pwm = 0; - do { - th = pulseIn(CO2_IN, 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); - return ppm_pwm; -} diff --git a/MH-Z19B-LCD/MH-Z19B-LCD.ino b/MHZ.cpp similarity index 55% rename from MH-Z19B-LCD/MH-Z19B-LCD.ino rename to MHZ.cpp index 8fa2148..f3d61b4 100644 --- a/MH-Z19B-LCD/MH-Z19B-LCD.ino +++ b/MHZ.cpp @@ -1,69 +1,25 @@ +/* MHZ library -#include + By Tobias Schürg +*/ -// include the library code: -#include +#include "MHZ.h" -// initialize the library with the numbers of the interface pins -LiquidCrystal lcd(12, 11, 5, 4, 3, 2); -SoftwareSerial co2Serial(7, 6); // define MH-Z19 RX TX -unsigned long startTime = millis(); +MHZ:: MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type) + : co2Serial(rxpin, txpin) +{ + _rxpin = rxpin; + _txpin = txpin; + _pwmpin = pwmpin; + _type = type; -void setup() { - Serial.begin(9600); co2Serial.begin(9600); - pinMode(9, INPUT); - - // set up the LCD's number of columns and rows: - lcd.begin(16, 2); - lcd.print("CO2 heating up"); - - lcd.setCursor(4, 1); - lcd.print("seconds left"); - - for (int seconds = 180; seconds > 0; seconds--) { - lcd.setCursor(0, 1); - lcd.print(" "); // clear timer - lcd.setCursor(0, 1); - lcd.print(seconds); - delay(1000); - } - - lcd.clear(); - lcd.setCursor(0, 0); - lcd.print("CO2 Air Quality"); } -void loop() { - Serial.println("------------------------------"); - Serial.print("Time from start: "); - Serial.print((millis() - startTime) / 1000); - Serial.println(" s"); - int ppm_uart = readCO2UART(); - int ppm_pwm = readCO2PWM(); - // TODO: just clear second line? - lcd.clear(); - lcd.setCursor(0, 0); - lcd.print("CO2 Air Quality"); - - lcd.setCursor(0, 1); - lcd.print("PPM: "); - - lcd.setCursor(5, 1); - lcd.print(ppm_pwm); - - lcd.setCursor(10, 1); - lcd.print("|"); - - lcd.setCursor(12, 1); - lcd.print(ppm_uart); - - delay(5000); -} - -int readCO2UART() { +int MHZ::readCO2UART() { + Serial.println("-- read CO2 uart ---"); byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; byte response[9]; // for answer @@ -121,7 +77,8 @@ int readCO2UART() { return ppm_uart; } -byte getCheckSum(char *packet) { +byte MHZ::getCheckSum(byte *packet) { + Serial.println("-- get checksum ---"); byte i; unsigned char checksum = 0; for (i = 1; i < 8; i++) { @@ -132,10 +89,12 @@ byte getCheckSum(char *packet) { return checksum; } -int readCO2PWM() { +int MHZ::readCO2PWM() { + Serial.println("-- read CO2 pwm ---"); unsigned long th, tl, ppm_pwm = 0; do { - th = pulseIn(9, HIGH, 1004000) / 1000; + Serial.print("."); + th = pulseIn(_pwmpin, HIGH, 1004000) / 1000; tl = 1004 - th; ppm_pwm = 5000 * (th - 2) / (th + tl - 4); } while (th == 0); diff --git a/MHZ.h b/MHZ.h new file mode 100644 index 0000000..c31d398 --- /dev/null +++ b/MHZ.h @@ -0,0 +1,39 @@ +/* MHZ library + + By Tobias Schürg +*/ +#ifndef MHZ_H +#define MHZ_H + +#if ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif + + +// Define types of sensors. +#define MHZ14A 14 +#define MHZ19B 19 + + +#include + + +class MHZ { + public: + MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type); + + int readCO2UART(); + int readCO2PWM(); + + private: + uint8_t _rxpin, _txpin, _pwmpin, _type; + SoftwareSerial co2Serial; + byte getCheckSum(byte *packet); + +}; + + +#endif +