simplify wifi and make it more generic

This commit is contained in:
Nicu Hodos 2024-06-01 22:14:34 +02:00
parent 16647d1f88
commit 858d13c9a7
4 changed files with 31 additions and 32 deletions

View File

@ -1,6 +1,5 @@
#pragma once #pragma once
#define MAIN_DEVICE_ID "esp_clock"
#define SENSOR_ID "bme280" #define SENSOR_ID "bme280"
#include "ha.h" #include "ha.h"

View File

@ -50,7 +50,7 @@ namespace Display {
void set(uint8_t value) { void set(uint8_t value) {
current = value % (BRIGHTNESS_MAX + 1); current = value % (BRIGHTNESS_MAX + 1);
clockDisplay.setBrightness(current); clockDisplay.setBrightness(current);
brightnessChangedCallback(); if (brightnessChangedCallback) brightnessChangedCallback();
} }
void change(bool increase) { void change(bool increase) {
@ -87,7 +87,7 @@ namespace Display {
} }
if (currentHour == 8) { if (currentHour == 8) {
Brightness::set(BRIGHTNESS_DAY); Brightness::set(BRIGHTNESS_DAY);
Wifi::reconnect(); Wifi::tReconnect.enable();
} }
if (currentHour == 17) { if (currentHour == 17) {
Brightness::set(BRIGHTNESS_NIGHT); Brightness::set(BRIGHTNESS_NIGHT);
@ -142,7 +142,7 @@ namespace Display {
void changeHourFormat24(bool format24) { void changeHourFormat24(bool format24) {
hourFormat24 = format24; hourFormat24 = format24;
drawTime(); drawTime();
hourFormatChangedCallback(); if (hourFormatChangedCallback) hourFormatChangedCallback();
} }
void setup() { void setup() {

View File

@ -7,23 +7,33 @@ namespace Wifi {
WiFiEventHandler stationConnectedHandler; WiFiEventHandler stationConnectedHandler;
WiFiEventHandler stationDisconnectedHandler; WiFiEventHandler stationDisconnectedHandler;
void reconnect();
Task tWifiReconnect(1 * TASK_MINUTE, TASK_FOREVER, reconnect, &ts);
String currentSSID; String currentSSID;
String currentPsk; String currentPsk;
void setup() { void printStatus();
stationConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& e) {
Serial.println("Reconnected to network."); Task tReconnect(1 * TASK_MINUTE, TASK_FOREVER, []{
tWifiConnected.restart(); if (WiFi.status() != WL_CONNECTED) {
tWifiReconnect.cancel(); 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) { stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) {
Serial.println("Disconnected from network."); Serial.println("Disconnected from network.");
tWifiReconnect.restartDelayed(); tReconnect.restartDelayed();
}); });
@ -36,17 +46,11 @@ namespace Wifi {
while (wifiMulti.run() != WL_CONNECTED) { while (wifiMulti.run() != WL_CONNECTED) {
delay(500); delay(500);
} }
WiFi.setHostname("esp-clock"); WiFi.setHostname(MAIN_DEVICE_ID);
currentSSID = WiFi.SSID(); currentSSID = WiFi.SSID();
currentPsk = WiFi.psk(); currentPsk = WiFi.psk();
}
void reconnect() { tReconnect.enable();
if (WiFi.status() != WL_CONNECTED) {
WiFi.forceSleepWake();
WiFi.begin(currentSSID.c_str(), currentPsk.c_str());
Serial.println("Reconnecting to WiFi netowrk...");
}
} }
void disconnect() { void disconnect() {

View File

@ -1,18 +1,17 @@
#include <Arduino.h> #include <Arduino.h>
void checkWifiCallback(); void checkWifiCallback();
void onWifiConnected();
void onButtonPressed(); void onButtonPressed();
void onLed(); void onLed();
void onButtonCallback(); void onButtonCallback();
#define MQTT_HOST IPAddress(192, 168, 5, 11) #define MQTT_HOST IPAddress(192, 168, 5, 11)
#define MQTT_PORT 1883 #define MQTT_PORT 1883
#define MAIN_DEVICE_ID "esp-clock"
#include <TaskScheduler.h> #include <TaskScheduler.h>
Scheduler ts; Scheduler ts;
Task tCheckWifi(5 * TASK_MINUTE, TASK_ONCE, checkWifiCallback, &ts); Task tCheckWifi(5 * TASK_MINUTE, TASK_ONCE, checkWifiCallback, &ts);
Task tWifiConnected(TASK_IMMEDIATE, TASK_ONCE, onWifiConnected, &ts);
void turnLed(bool on = true) { void turnLed(bool on = true) {
on ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH); on ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH);
@ -52,6 +51,7 @@ Task tReadBme(TASK_MINUTE, TASK_FOREVER, []() {
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
Serial.println();
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); digitalWrite(LED_BUILTIN, HIGH);
@ -66,7 +66,11 @@ void setup() {
); );
Devices::setup(); Devices::setup();
Wifi::setup(); Wifi::setup(ts, []{
Ota::tLoop.enable();
Mqtt::tReConnect.enable();
Ntp::tUpdateTime.enable();
});
pinMode(BUTTON, INPUT_PULLUP); pinMode(BUTTON, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON), onButtonPressed, FALLING); attachInterrupt(digitalPinToInterrupt(BUTTON), onButtonPressed, FALLING);
@ -79,14 +83,6 @@ void loop() {
ts.execute(); ts.execute();
} }
void onWifiConnected() {
Serial.println("Wifi connected event");
Wifi::printStatus();
Ota::tLoop.enable();
Mqtt::tReConnect.enable();
Ntp::tUpdateTime.enable();
}
void checkWifiCallback() { void checkWifiCallback() {
#if !WIFI_ALWAYS_ON #if !WIFI_ALWAYS_ON
Serial.println("Wifi connection timed out"); Serial.println("Wifi connection timed out");