From 25dcda2dfab514f15b78149126720dc4312da277 Mon Sep 17 00:00:00 2001 From: Nicolae Hodos Date: Mon, 6 Dec 2021 09:15:31 +0100 Subject: [PATCH] use events for Wifi --- include/display.h | 22 ++++++++++------- include/ir.h | 4 ++-- include/ntp_time.h | 3 +-- include/wifi.h | 22 +++++++++-------- src/esp_clock.cpp | 60 +++++++++++++++++++++++++++++----------------- 5 files changed, 66 insertions(+), 45 deletions(-) diff --git a/include/display.h b/include/display.h index 8cb2181..b70ab70 100644 --- a/include/display.h +++ b/include/display.h @@ -9,13 +9,10 @@ namespace Display { - void displayColon(); - uint8_t brightness = BRIGHTNESS; + int currentHour = -1; int currentMin = -1; - Task blinkColon(500, TASK_FOREVER, displayColon, &ts, true); - // Create display object Adafruit_7segment clockDisplay = Adafruit_7segment(); @@ -44,7 +41,6 @@ namespace Display { } void adjustBrightness() { - int currentHour = hour(); if (currentHour > 8 && currentHour < 17) { brightness = 11; } else { @@ -56,13 +52,21 @@ namespace Display { void displayColon() { static bool colonOn = false; - if (colonOn && currentMin != minute()) { - currentMin = minute(); - displayTime(); + if (colonOn) { + if (currentHour != hour()) { + currentHour = hour(); + Display::adjustBrightness(); + hourChanged.signal(); + if (currentHour == 8) dayChanged.signal(); + } + if (currentMin != minute()) { + currentMin = minute(); + displayTime(); + } } clockDisplay.drawColon(colonOn); clockDisplay.writeDisplay(); - + colonOn = !colonOn; } diff --git a/include/ir.h b/include/ir.h index 0894f48..0d534a7 100644 --- a/include/ir.h +++ b/include/ir.h @@ -70,14 +70,14 @@ namespace Ir { { case 0x9F: avrOn = false; - Ntp::timeAtStartup = now(); + Ntp::lastConnectedTime = now(); break; case 0xC4: case 0xD0: case 0xC0: avrOn = true; if (WiFi.status() == WL_DISCONNECTED) { - wifi.reconnect(); + Wifi::reconnect(); // connect on wifi connected mqttClient.connect(); } diff --git a/include/ntp_time.h b/include/ntp_time.h index 3380cb1..f92dbc1 100644 --- a/include/ntp_time.h +++ b/include/ntp_time.h @@ -8,7 +8,7 @@ namespace Ntp { WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "europe.pool.ntp.org"); - time_t timeAtStartup; + time_t lastConnectedTime; // Central European Time (Frankfurt, Paris) TimeChangeRule CEST = { "CEST", Last, Sun, Mar, 2, 120 }; // Central European Summer Time @@ -27,6 +27,5 @@ namespace Ntp { void setup() { timeClient.begin(); - timeAtStartup = updateTime(); } } \ No newline at end of file diff --git a/include/wifi.h b/include/wifi.h index 22699c5..80414fb 100644 --- a/include/wifi.h +++ b/include/wifi.h @@ -2,20 +2,25 @@ #include #include -class Wifi { +namespace Wifi { + + WiFiEventHandler stationConnectedHandler; -private: String currentSSID; String currentPsk; -public: void setup() { + stationConnectedHandler = WiFi.onStationModeConnected([](const WiFiEventStationModeConnected& e) { + Serial.println("Reconnected to network."); + wifiConnected.signal(); + }); + + WiFi.hostname("esp-clock"); + ESP8266WiFiMulti wifiMulti; wifiMulti.addAP("OpenWrt", "***REMOVED***"); - // wifiMulti.addAP("IoT", "***REMOVED***"); wifiMulti.addAP("Miracle", "***REMOVED***"); - WiFi.hostname("esp-clock"); - + Serial.println("Connecting to WiFi netowrk."); while (wifiMulti.run() != WL_CONNECTED) { delay(500); @@ -30,9 +35,6 @@ public: WiFi.forceSleepWake(); WiFi.begin(currentSSID.c_str(), currentPsk.c_str()); Serial.println("Reconnecting to WiFi netowrk..."); - for (int i = 0; i < 4; i++) { - delay(1000); - } } } @@ -58,4 +60,4 @@ public: Serial.print(rssi); Serial.println(" dBm"); } -} wifi; \ No newline at end of file +} diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index b08b246..41bb091 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -1,51 +1,67 @@ #include +#define _TASK_STATUS_REQUEST #include Scheduler ts; +StatusRequest hourChanged; +StatusRequest dayChanged; +StatusRequest wifiConnected; #include "wifi.h" -#include "ota.h" #include "ntp_time.h" +#include "ota.h" #include "display.h" #include "ir.h" #define STAY_CONNECTED_AFTER_BOOT 5*60 -int currentHour = -1; +void wifiConnectedCallback(); +void otaCallback(); + +Task tBlinkColon(500, TASK_FOREVER, Display::displayColon, &ts, true); +Task tOta(TASK_IMMEDIATE, TASK_FOREVER, otaCallback, &ts); +Task tWifiReconnect(Wifi::reconnect, &ts); +Task tWifiConnected(wifiConnectedCallback, &ts); void setup() { - Display::setup(); - Serial.begin(9600); - wifi.setup(); + Display::setup(); Ota::setup(); Ntp::setup(); - Display::adjustBrightness(); Ir::setup(); + + hourChanged.setWaiting(); + dayChanged.setWaiting(); + wifiConnected.setWaiting(); + tWifiReconnect.waitFor(&dayChanged); + tWifiConnected.waitFor(&wifiConnected); + + Wifi::setup(); } void loop() { Ir::loop(); - if ((currentHour != hour())) { - Display::adjustBrightness(); - wifi.reconnect(); - wifi.printStatus(); - if (WiFi.status() == WL_CONNECTED) { - if (time_t newTime = Ntp::updateTime()) Serial.println(asctime(localtime(&newTime))); - } - currentHour = hour(); - } - if (WiFi.status() == WL_CONNECTED) { - Ota::loop(); - if ((difftime(now(), Ntp::timeAtStartup) > STAY_CONNECTED_AFTER_BOOT) && !Ir::avrOn) { - wifi.disconnect(); - } - } - ts.execute(); } + +void wifiConnectedCallback() { + Wifi::printStatus(); + if (time_t newTime = Ntp::updateTime()) { + Serial.println(asctime(localtime(&newTime))); + Ntp::lastConnectedTime = newTime; + } + tOta.enable(); +} + +void otaCallback() { + Ota::loop(); + if ((difftime(now(), Ntp::lastConnectedTime) > STAY_CONNECTED_AFTER_BOOT) && !Ir::avrOn) { + Wifi::disconnect(); + tOta.disable(); + } +}