From 854b4a60b9c8e49d1e807b7405e0d57eefb00529 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sun, 5 Dec 2021 11:13:58 +0100 Subject: [PATCH] move Ir logic into its own loop --- include/ir.h | 62 ++++++++++++++++++++++++++++++++++++++++------- include/wifi.h | 2 +- platformio.ini | 6 ++++- src/esp_clock.cpp | 44 +++------------------------------ 4 files changed, 62 insertions(+), 52 deletions(-) diff --git a/include/ir.h b/include/ir.h index 97a6056..6e38967 100644 --- a/include/ir.h +++ b/include/ir.h @@ -5,7 +5,7 @@ #include #include -#define IR_INPUT_PIN 12 +#define IR_INPUT_PIN D6 #define MQTT_HOST IPAddress(192, 168, 5, 138) #define MQTT_PORT 1883 @@ -14,20 +14,13 @@ namespace Ir { IRrecv irrecv(IR_INPUT_PIN); decode_results results; + bool avrOn = false; AsyncMqttClient mqttClient; std::queue commands; uint8_t lastCommand = 0x9F; - void setup() { - mqttClient.setServer(MQTT_HOST, MQTT_PORT); - Serial.println("Connecting to MQTT..."); - mqttClient.connect(); - - irrecv.enableIRIn(); // Start the receiver - } - void publishCommand() { if (!commands.empty() && mqttClient.connected()) { char message[32]; @@ -59,6 +52,57 @@ namespace Ir { uint8_t getCurrentCommand() { return commands.empty() ? 0 : commands.front(); } + + void setup() { + mqttClient.setServer(MQTT_HOST, MQTT_PORT); + Serial.println("Connecting to MQTT..."); + mqttClient.connect(); + + irrecv.enableIRIn(); // Start the receiver + } + + void loop() { + if (readCommand()) { + Display::displayValue(lastCommand); + Display::displayColon(false); + delay(1000); + switch (lastCommand) + { + case 0x9F: + avrOn = false; + Ntp::timeAtStartup = now(); + break; + case 0xC4: + case 0xD0: + case 0xC0: + avrOn = true; + if (WiFi.status() == WL_DISCONNECTED) { + wifi.reconnect(); + // connect on wifi connected + mqttClient.connect(); + } + break; + default: + break; + } + } + if (!avrOn && getCurrentCommand() == 0xC7) { + Display::changeBrightness(true); + commands.pop(); + } + if (!avrOn && getCurrentCommand() == 0xC8) { + Display::changeBrightness(false); + commands.pop(); + } + publishCommand(); + } } +#else +namespace Ir { + bool avrOn = false; + + void setup() {} + void loop() {} +} #endif \ No newline at end of file diff --git a/include/wifi.h b/include/wifi.h index 1c9933c..09d4575 100644 --- a/include/wifi.h +++ b/include/wifi.h @@ -57,4 +57,4 @@ public: Serial.print(rssi); Serial.println(" dBm"); } -}; \ No newline at end of file +} wifi; \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index d01ed35..84cbd60 100644 --- a/platformio.ini +++ b/platformio.ini @@ -20,10 +20,14 @@ lib_deps = jchristensen/Timezone@^1.2.4 ottowinter/AsyncMqttClient-esphome@^0.8.5 crankyoldgit/IRremoteESP8266@^2.7.18 -build_flags = -D IR=0 + +[env:dev_mode] +build_flags = -D IR=1 [env:laptop_home] +build_flags = -D IR=0 [env:ota_home] +build_flags = -D IR=0 upload_port = 192.168.5.191 upload_protocol = espota diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index b282f88..b78a3f1 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -8,9 +8,6 @@ #define STAY_CONNECTED_AFTER_BOOT 5*60 int currentHour = -1; -bool avrOn = false; - -Wifi wifi; void setup() { Display::setup(); @@ -24,47 +21,12 @@ void setup() { Ntp::setup(); Display::adjustBrightness(); -#if IR Ir::setup(); -#endif } void loop() { -#if IR - if (Ir::readCommand()) { - Display::displayValue(Ir::lastCommand); - Display::displayColon(false); - delay(1000); - switch (Ir::lastCommand) - { - case 0x9F: - avrOn = false; - Ntp::timeAtStartup = now(); - break; - case 0xC4: - case 0xD0: - case 0xC0: - avrOn = true; - if (WiFi.status() == WL_DISCONNECTED) { - wifi.reconnect(); - // connect on wifi connected - Ir::mqttClient.connect(); - } - break; - default: - break; - } - } - if (!avrOn && Ir::getCurrentCommand() == 0xC7) { - Display::changeBrightness(true); - Ir::commands.pop(); - } - if (!avrOn && Ir::getCurrentCommand() == 0xC8) { - Display::changeBrightness(false); - Ir::commands.pop(); - } - Ir::publishCommand(); -#endif + Ir::loop(); + if ((currentHour != hour())) { Display::adjustBrightness(); wifi.reconnect(); @@ -76,7 +38,7 @@ void loop() { } if (WiFi.status() == WL_CONNECTED) { Ota::loop(); - if ((difftime(now(), Ntp::timeAtStartup) > STAY_CONNECTED_AFTER_BOOT) && !avrOn) { + if ((difftime(now(), Ntp::timeAtStartup) > STAY_CONNECTED_AFTER_BOOT) && !Ir::avrOn) { wifi.disconnect(); } }