From be0107298665535c2145542fb6768eed3f7e071c Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Thu, 9 Dec 2021 12:29:21 +0100 Subject: [PATCH] fix wifi reconnection event --- include/display.h | 1 - include/ir.h | 2 +- include/mqtt.h | 5 +---- include/ntp_time.h | 1 + include/wifi.h | 5 ++--- src/esp_clock.cpp | 21 +++++++++------------ 6 files changed, 14 insertions(+), 21 deletions(-) diff --git a/include/display.h b/include/display.h index 82fc040..1af6622 100644 --- a/include/display.h +++ b/include/display.h @@ -60,7 +60,6 @@ namespace Display { if (currentHour != hour()) { currentHour = hour(); Display::adjustBrightness(); - hourChanged.signal(); if (currentHour == 8) Wifi::reconnect(); } if (currentMin != minute()) { diff --git a/include/ir.h b/include/ir.h index d21bdb8..ba6809e 100644 --- a/include/ir.h +++ b/include/ir.h @@ -49,7 +49,7 @@ namespace Ir { { case 0x9F: avrOn = false; - tCheckWifi.enable(); + tCheckWifi.enableDelayed(5*TASK_SECOND); Display::displayText("Off"); break; case 0x12: diff --git a/include/mqtt.h b/include/mqtt.h index d806042..6b210df 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -7,7 +7,7 @@ namespace Mqtt { void publishCommand(); - Task tPublish(TASK_SECOND, TASK_FOREVER, Mqtt::publishCommand, &ts); + Task tPublish(TASK_SECOND, TASK_FOREVER, publishCommand, &ts); AsyncMqttClient client; @@ -23,9 +23,6 @@ namespace Mqtt { void pop() { queue.pop(); } - uint8_t front() { - return queue.front(); - } } commands; void publishCommand() { diff --git a/include/ntp_time.h b/include/ntp_time.h index f92dbc1..34f20c8 100644 --- a/include/ntp_time.h +++ b/include/ntp_time.h @@ -27,5 +27,6 @@ namespace Ntp { void setup() { timeClient.begin(); + Serial.println("NTP setup"); } } \ No newline at end of file diff --git a/include/wifi.h b/include/wifi.h index 80414fb..ebc24bd 100644 --- a/include/wifi.h +++ b/include/wifi.h @@ -10,9 +10,9 @@ namespace Wifi { String currentPsk; void setup() { - stationConnectedHandler = WiFi.onStationModeConnected([](const WiFiEventStationModeConnected& e) { + stationConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& e) { Serial.println("Reconnected to network."); - wifiConnected.signal(); + tWifiConnected.restart(); }); WiFi.hostname("esp-clock"); @@ -25,7 +25,6 @@ namespace Wifi { while (wifiMulti.run() != WL_CONNECTED) { delay(500); } - Serial.println("Connected to network."); currentSSID = WiFi.SSID(); currentPsk = WiFi.psk(); } diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index 7f6d9cf..9d4298f 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -3,13 +3,11 @@ void checkWifiCallback(); void onWifiConnected(); -#define _TASK_STATUS_REQUEST +// #define _TASK_STATUS_REQUEST #include Scheduler ts; -StatusRequest hourChanged; -StatusRequest wifiConnected; Task tCheckWifi(5000, TASK_FOREVER, checkWifiCallback, &ts); -Task tWifiConnected(onWifiConnected, &ts); +Task tWifiConnected(TASK_IMMEDIATE, TASK_ONCE, onWifiConnected, &ts); #include "wifi.h" #include "display.h" @@ -19,9 +17,10 @@ Task tWifiConnected(onWifiConnected, &ts); #include "ota.h" #include "ir.h" -#define STAY_CONNECTED_AFTER_BOOT 30*60 +#define STAY_CONNECTED_AFTER_BOOT 15 void setup() { + Serial.begin(9600); Display::setup(); @@ -31,10 +30,6 @@ void setup() { Mqtt::setup(); Bmp::setup(); - hourChanged.setWaiting(); - wifiConnected.setWaiting(); - tWifiConnected.waitFor(&wifiConnected); - Wifi::setup(); } @@ -43,18 +38,20 @@ void loop() { } void onWifiConnected() { + Serial.println("Wifi connected event"); Wifi::printStatus(); + Ota::tLoop.enable(); + if (!Ir::avrOn) tCheckWifi.enable(); + Mqtt::client.connect(); if (time_t newTime = Ntp::updateTime()) { Serial.println(asctime(localtime(&newTime))); Ntp::lastConnectedTime = newTime; } - Ota::tLoop.enable(); - if (!Ir::avrOn) tCheckWifi.enable(); - Mqtt::client.connect(); } void checkWifiCallback() { if (difftime(now(), Ntp::lastConnectedTime) > STAY_CONNECTED_AFTER_BOOT) { + Serial.println("Wifi connection timed out"); Mqtt::client.disconnect(); Wifi::disconnect(); Ota::tLoop.disable();