From 858d13c9a7d9e1abebd231e616d54cafbd709e98 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sat, 1 Jun 2024 22:14:34 +0200 Subject: [PATCH] simplify wifi and make it more generic --- include/devices.h | 1 - include/display.h | 6 +++--- include/wifi.h | 38 +++++++++++++++++++++----------------- src/esp_clock.cpp | 18 +++++++----------- 4 files changed, 31 insertions(+), 32 deletions(-) diff --git a/include/devices.h b/include/devices.h index 8d37578..8a28872 100644 --- a/include/devices.h +++ b/include/devices.h @@ -1,6 +1,5 @@ #pragma once -#define MAIN_DEVICE_ID "esp_clock" #define SENSOR_ID "bme280" #include "ha.h" diff --git a/include/display.h b/include/display.h index 036d4c1..ae2edf4 100644 --- a/include/display.h +++ b/include/display.h @@ -50,7 +50,7 @@ namespace Display { void set(uint8_t value) { current = value % (BRIGHTNESS_MAX + 1); clockDisplay.setBrightness(current); - brightnessChangedCallback(); + if (brightnessChangedCallback) brightnessChangedCallback(); } void change(bool increase) { @@ -87,7 +87,7 @@ namespace Display { } if (currentHour == 8) { Brightness::set(BRIGHTNESS_DAY); - Wifi::reconnect(); + Wifi::tReconnect.enable(); } if (currentHour == 17) { Brightness::set(BRIGHTNESS_NIGHT); @@ -142,7 +142,7 @@ namespace Display { void changeHourFormat24(bool format24) { hourFormat24 = format24; drawTime(); - hourFormatChangedCallback(); + if (hourFormatChangedCallback) hourFormatChangedCallback(); } void setup() { diff --git a/include/wifi.h b/include/wifi.h index c00d88d..f3f6361 100644 --- a/include/wifi.h +++ b/include/wifi.h @@ -7,23 +7,33 @@ namespace Wifi { WiFiEventHandler stationConnectedHandler; WiFiEventHandler stationDisconnectedHandler; - void reconnect(); - - Task tWifiReconnect(1 * TASK_MINUTE, TASK_FOREVER, reconnect, &ts); String currentSSID; String currentPsk; - void setup() { - stationConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& e) { - Serial.println("Reconnected to network."); - tWifiConnected.restart(); - tWifiReconnect.cancel(); + void printStatus(); + + Task tReconnect(1 * TASK_MINUTE, TASK_FOREVER, []{ + if (WiFi.status() != WL_CONNECTED) { + Serial.println("Reconnecting to WiFi netowrk..."); + WiFi.forceSleepWake(); + WiFi.begin(currentSSID.c_str(), currentPsk.c_str()); + } + }); + + void setup(Scheduler& ts, void(*onConnected)() = nullptr) { + ts.addTask(tReconnect); + + stationConnectedHandler = WiFi.onStationModeGotIP([onConnected](const WiFiEventStationModeGotIP& e) { + Serial.println("Connected to network."); + printStatus(); + tReconnect.cancel(); + if (onConnected) onConnected(); }); stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) { Serial.println("Disconnected from network."); - tWifiReconnect.restartDelayed(); + tReconnect.restartDelayed(); }); @@ -36,17 +46,11 @@ namespace Wifi { while (wifiMulti.run() != WL_CONNECTED) { delay(500); } - WiFi.setHostname("esp-clock"); + WiFi.setHostname(MAIN_DEVICE_ID); currentSSID = WiFi.SSID(); currentPsk = WiFi.psk(); - } - void reconnect() { - if (WiFi.status() != WL_CONNECTED) { - WiFi.forceSleepWake(); - WiFi.begin(currentSSID.c_str(), currentPsk.c_str()); - Serial.println("Reconnecting to WiFi netowrk..."); - } + tReconnect.enable(); } void disconnect() { diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index 1927bb9..f83af4e 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -1,18 +1,17 @@ #include void checkWifiCallback(); -void onWifiConnected(); void onButtonPressed(); void onLed(); void onButtonCallback(); #define MQTT_HOST IPAddress(192, 168, 5, 11) #define MQTT_PORT 1883 +#define MAIN_DEVICE_ID "esp-clock" #include Scheduler ts; Task tCheckWifi(5 * TASK_MINUTE, TASK_ONCE, checkWifiCallback, &ts); -Task tWifiConnected(TASK_IMMEDIATE, TASK_ONCE, onWifiConnected, &ts); void turnLed(bool on = true) { on ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH); @@ -52,6 +51,7 @@ Task tReadBme(TASK_MINUTE, TASK_FOREVER, []() { void setup() { Serial.begin(9600); + Serial.println(); pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH); @@ -66,7 +66,11 @@ void setup() { ); Devices::setup(); - Wifi::setup(); + Wifi::setup(ts, []{ + Ota::tLoop.enable(); + Mqtt::tReConnect.enable(); + Ntp::tUpdateTime.enable(); + }); pinMode(BUTTON, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(BUTTON), onButtonPressed, FALLING); @@ -79,14 +83,6 @@ void loop() { ts.execute(); } -void onWifiConnected() { - Serial.println("Wifi connected event"); - Wifi::printStatus(); - Ota::tLoop.enable(); - Mqtt::tReConnect.enable(); - Ntp::tUpdateTime.enable(); -} - void checkWifiCallback() { #if !WIFI_ALWAYS_ON Serial.println("Wifi connection timed out");