arduino:mh-z19b
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| arduino:mh-z19b [2020/03/02 21:35] – aistis | arduino:mh-z19b [2020/03/02 21:36] (current) – aistis | ||
|---|---|---|---|
| Line 9: | Line 9: | ||
| {{: | {{: | ||
| + | {{: | ||
| + | |||
| + | <code cpp> | ||
| + | #include < | ||
| + | SoftwareSerial co2Serial(7, | ||
| + | unsigned long startTime = millis(); | ||
| + | |||
| + | void setup() { | ||
| + | Serial.begin(115200); | ||
| + | co2Serial.begin(9600); | ||
| + | pinMode(9, INPUT); | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | int ppm_uart = readCO2UART(); | ||
| + | //int ppm_pwm = readCO2PWM(); | ||
| + | delay(1000); | ||
| + | } | ||
| + | |||
| + | int readCO2UART(){ | ||
| + | byte cmd[9] = {0xFF, | ||
| + | byte response[9]; | ||
| + | co2Serial.write(cmd, | ||
| + | |||
| + | // clear the buffer | ||
| + | memset(response, | ||
| + | int i = 0; | ||
| + | while (co2Serial.available() == 0) { | ||
| + | // Serial.print(" | ||
| + | // Serial.print(i); | ||
| + | // Serial.println(" | ||
| + | delay(100); | ||
| + | i++; | ||
| + | } | ||
| + | if (co2Serial.available() > 0) { | ||
| + | co2Serial.readBytes(response, | ||
| + | } | ||
| + | // print out the response in hexa | ||
| + | for (int i = 0; i < 9; i++) { | ||
| + | // | ||
| + | // | ||
| + | } | ||
| + | // | ||
| + | |||
| + | // checksum | ||
| + | byte check = getCheckSum(response); | ||
| + | if (response[8] != check) { | ||
| + | Serial.println(" | ||
| + | Serial.print(" | ||
| + | Serial.println(response[8]); | ||
| + | Serial.print(" | ||
| + | Serial.println(check); | ||
| + | } | ||
| + | |||
| + | // ppm | ||
| + | int ppm_uart = 256 * (int)response[2] + response[3]; | ||
| + | Serial.print(ppm_uart); | ||
| + | |||
| + | // temp | ||
| + | byte temp = response[4] - 40; | ||
| + | Serial.print("," | ||
| + | Serial.println(temp); | ||
| + | |||
| + | // status | ||
| + | byte status = response[5]; | ||
| + | // | ||
| + | if (status == 0x40) { | ||
| + | Serial.println(" | ||
| + | } | ||
| + | |||
| + | 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)/ | ||
| + | } while (th == 0); | ||
| + | Serial.print(" | ||
| + | Serial.println(ppm_pwm); | ||
| + | return ppm_pwm; | ||
| + | } | ||
| + | </ | ||
arduino/mh-z19b.1583177715.txt.gz · Last modified: by aistis
