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
#define MAIN_DEVICE_ID "esp_clock"
#define SENSOR_ID "bme280"
#include "ha.h"

View File

@ -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() {

View File

@ -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() {

View File

@ -1,18 +1,17 @@
#include <Arduino.h>
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 <TaskScheduler.h>
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");