diff --git a/ambient7-arduino/arduino-ide-version/ambient7-mcu/ambient7-mcu.ino b/ambient7-arduino/arduino-ide-version/ambient7-mcu/ambient7-mcu.ino index b3e3874..05c0b4e 100644 --- a/ambient7-arduino/arduino-ide-version/ambient7-mcu/ambient7-mcu.ino +++ b/ambient7-arduino/arduino-ide-version/ambient7-mcu/ambient7-mcu.ino @@ -1,10 +1,21 @@ #include +const byte CO2_RX = D6; /* MCU UART RX = sensor TX */ +const byte CO2_TX = D7; /* MCU UART TX = sensor RX */ + +SoftwareSerial co2Serial(CO2_RX, CO2_TX); + unsigned long startTime = millis(); void setup() { - Serial.begin(9600); - pinMode(D3, INPUT); + Serial.begin(115200); + Serial.println("Setup: "); + Serial.print("CO2 RX Pin: "); + Serial.println(CO2_RX); + Serial.print("CO2 TX Pin: "); + Serial.println(CO2_TX); + + co2Serial.begin(9600); } void loop() { @@ -12,19 +23,77 @@ void loop() { Serial.print("Time from start: "); Serial.print((millis() - startTime) / 1000); Serial.println(" s"); - int ppm_pwm = readCO2PWM(); + + int ppm_uart = readCO2UART(); + delay(5000); } -int readCO2PWM() { - unsigned long th, tl, ppm_pwm = 0; - do { - th = pulseIn(D3, HIGH, 1004000) / 1000; - Serial.println("Step"); - 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; +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) { + delay(1000); + i++; + } + + if (co2Serial.available() > 0) { + co2Serial.readBytes(response, 9); + } + + // print out the response in hex + 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) { + byte i; + byte checksum = 0; + for (i = 1; i < 8; i++) { + checksum += packet[i]; + } + checksum = 0xff - checksum; + checksum += 1; + return checksum; } diff --git a/ambient7-arduino/arduino-ide-version/t1.cpp b/ambient7-arduino/arduino-ide-version/t1.cpp deleted file mode 100644 index 6ecb2a9..0000000 --- a/ambient7-arduino/arduino-ide-version/t1.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include - -unsigned long startTime = millis(); - -void setup() { - Serial.begin(9600); - co2Serial.begin(9600); - pinMode(D3, INPUT); -} - -void loop() { - Serial.println("------------------------------"); - Serial.print("Time from start: "); - Serial.print((millis() - startTime) / 1000); - Serial.println(" s"); - int ppm_pwm = readCO2PWM(); - delay(5000); -} - -int readCO2PWM() { - unsigned long th, tl, ppm_pwm = 0; - do { - th = pulseIn(D3, 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; -}