CO2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <SoftwareSerial.h>; SoftwareSerial mySerial(2, 3); // RX, TX byte request[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79}; unsigned char response[9]; void setup() { Serial.begin(9600); mySerial.begin(9600); } void loop() { mySerial.write(request, 9); memset(response, 0, 9); mySerial.readBytes(response, 9); int i; byte crc = 0; for (i = 1; i < 8; i++) crc+=response[i]; crc = 255 - crc; crc++; if ( !(response[0] == 0xFF && response[1] == 0x86 && response[8] == crc) ) { Serial.println("CRC error"); } else { unsigned int HLconcentration = (unsigned int) response[2]; unsigned int LLconcentration = (unsigned int) response[3]; unsigned int co2 = (256*HLconcentration) + LLconcentration; Serial.println(co2); for (i = 0; i < 9; i++) { Serial.print("0x"); Serial.print(response[i],HEX); Serial.print(" "); } Serial.println(" "); } delay(5000); } |
[collapse]