commit 405515f61c8ff13c390367bc874221342122aaba Author: Tobias Schürg Date: Sun Apr 22 11:36:51 2018 +0200 Initial commit diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7ace8ce --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Tobias Schürg + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..c9cfe55 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# MH-Z19B CO2 Module diff --git a/mhz19-2.ino b/mhz19-2.ino new file mode 100644 index 0000000..9262efe --- /dev/null +++ b/mhz19-2.ino @@ -0,0 +1,101 @@ + +#include +SoftwareSerial co2Serial(7, 6); // define MH-Z19 RX TX +unsigned long startTime = millis(); + +void setup() { + Serial.begin(9600); + co2Serial.begin(9600); + pinMode(9, INPUT); +} + +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(); + delay(5000); +} + +int readCO2UART() { + 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(char *packet) { + byte i; + unsigned char checksum = 0; + for (i = 1; i < 8; i++) { + checksum += packet[i]; + } + checksum = 0xff - checksum; + checksum += 1; + return checksum; +} + +int readCO2PWM() { + unsigned long th, tl, ppm_pwm = 0; + do { + th = pulseIn(9, 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; +}