diff --git a/BigRoomMyS/platformio.ini b/BigRoomMyS/platformio.ini index 5adbf43..b6d361d 100644 --- a/BigRoomMyS/platformio.ini +++ b/BigRoomMyS/platformio.ini @@ -16,4 +16,5 @@ monitor_speed = 115200 lib_deps = MySensors @ ^2.3.2 wifwaf/MH-Z19 @ ^1.5.3 - enjoyneering/AHT10 @ ^1.1.0 \ No newline at end of file + enjoyneering/AHT10 @ ^1.1.0 + featherfly/SoftwareSerial @ ^1.0 \ No newline at end of file diff --git a/BigRoomMyS/src/main.cpp b/BigRoomMyS/src/main.cpp index e4d18f8..1319b70 100644 --- a/BigRoomMyS/src/main.cpp +++ b/BigRoomMyS/src/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include "MHZ19.h" #define MY_DEBUG #define MY_RADIO_RF24 @@ -12,12 +13,25 @@ #define MY_DEFAULT_TX_LED_PIN (6) // the PCB, on board LED #include +#include // Remove if using HardwareSerial or non-uno compatabile device -#define MOTION (8) +#define RX_PIN 8 +#define TX_PIN 7 +#define BAUDRATE 9600 + +#define MOTION (6) + +MHZ19 myMHZ19; +SoftwareSerial mySerial(RX_PIN, TX_PIN); + +uint8_t periodMin; +uint8_t periodMotion; +uint8_t curDelay; void presentation(); void sendDataI(MyMessage msg, uint32_t status); void sendDataF(MyMessage msg, float status); +void sendDataB(MyMessage msg, bool status); MyMessage msgTemp(0, V_TEMP); MyMessage msgHum(1, V_HUM); @@ -26,27 +40,85 @@ MyMessage msgCO2(3, V_VAR1); MyMessage msgLight(3, V_VAR2); MyMessage msgTimeWork(3, V_VAR3); MyMessage msgTimeMotion(3, V_VAR4); +MyMessage msgPeriod(3, V_VAR5); void setup() { pinMode(MOTION, INPUT_PULLUP); + mySerial.begin(BAUDRATE); // Uno example: Begin Stream with MHZ19 baudrate + myMHZ19.begin(mySerial); // *Important, Pass your Stream reference here + myMHZ19.autoCalibration(false); + +/* char myVersion[4]; + myMHZ19.getVersion(myVersion); + Serial.print("\nFirmware Version: "); + for(byte i = 0; i < 4; i++) + { + Serial.print(myVersion[i]); + if(i == 1) + Serial.print("."); + } + + Serial.println(""); + Serial.print("Range: "); + Serial.println(myMHZ19.getRange()); + Serial.print("Background CO2: "); + Serial.println(myMHZ19.getBackgroundCO2()); + Serial.print("Temperature Cal: "); + Serial.println(myMHZ19.getTempAdjustment()); + Serial.print("ABC Status: "); myMHZ19.getABC() ? Serial.println("ON") : Serial.println("OFF");*/ + periodMin = loadState(0); + if(periodMin == 0) periodMin = 1; + periodMotion = loadState(1); + if(periodMotion == 0) periodMotion = 60; } void loop() { static uint32_t cRun = millis(); + static uint8_t sec = 0; + static uint8_t minuts = 0; uint16_t LightLev = analogRead(A0); - if((cRun + 999) < millis()){ + if((cRun + 1000) <= millis()){ //Once per second cRun = millis(); - sendDataI(msgTimeWork, cRun); + sec++; + if(sec == 59){ + minuts++; + sec = 0; + } + if(minuts == periodMin){ + int CO2; // Buffer for CO2 + CO2 = myMHZ19.getCO2(); // Request CO2 (as ppm) + + if(myMHZ19.errorCode == RESULT_OK) // RESULT_OK is an alis for 1. Either can be used to confirm the response was OK. + { + Serial.println(CO2); + sendDataI(msgCO2, CO2); + wait(50); + } + sendDataI(msgTimeWork, cRun); + minuts = 0; + } } } void presentation() { - sendSketchInfo("MidRoom", "1.0"); - present(0, S_TEMP, "Temp"); - present(1, S_HUM, "Humid"); - present(2, S_MOTION, "Motion"); - present(3, S_CUSTOM, "CO2_Light_Time"); + sendSketchInfo(F("MidRoom"), F("1.0")); + present(0, S_TEMP, F("Temp")); + present(1, S_HUM, F("Humid")); + present(2, S_MOTION, F("Motion")); + present(3, S_CUSTOM, F("CO2_Light_Time")); +} + +void receive(const MyMessage &message) +{ + if((message.sensor == 3) && (message.type == V_VAR4)){ + periodMotion = message.getByte(); + saveState(1, periodMotion); + } + if((message.sensor == 3) && (message.type == V_VAR5)){ + periodMin = message.getByte(); + saveState(0, periodMin); + } } void sendDataI(MyMessage msg, uint32_t val) @@ -81,7 +153,7 @@ void sendDataF(MyMessage msg, float val) } } -void sendDataI(MyMessage msg, bool val) +void sendDataB(MyMessage msg, bool val) { bool send_data = false; uint8_t count = 0;