create the library

This commit is contained in:
Nicu Hodos 2024-06-04 10:23:17 +02:00
parent f8aa7dfabd
commit 79d16c0098
3 changed files with 119 additions and 88 deletions

21
library.json Normal file
View File

@ -0,0 +1,21 @@
{
"name": "Wifi",
"version": "1.0.0",
"description": "Helper classes for handling wifi connectiviy and OTA",
"repository":
{
"type": "git",
"url": "https://git.hodos.ro/libraries/wifi.git"
},
"authors":
[
{
"name": "Nicu Hodos",
"email": "nicu@hodos.ro",
"maintainer": true
}
],
"license": "MIT",
"frameworks": "arduino",
"platforms": "*"
}

View File

@ -6,19 +6,23 @@ namespace Ota {
Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true); Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true);
void setup() { void setup() {
ArduinoOTA.onStart([]() { ArduinoOTA.onStart(
[]() {
Serial.println("Starting OTA"); Serial.println("Starting OTA");
Mqtt::publishCleanupConfig(); Mqtt::publishCleanupConfig();
delay(2000); delay(2000);
Mqtt::disconnect(); Mqtt::disconnect();
}); });
ArduinoOTA.onEnd([]() { ArduinoOTA.onEnd(
[]() {
Serial.println("\nOTA Finished"); Serial.println("\nOTA Finished");
}); });
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { ArduinoOTA.onProgress(
[](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100))); Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
}); });
ArduinoOTA.onError([](ota_error_t error) { ArduinoOTA.onError(
[](ota_error_t error) {
Serial.printf("Error[%u]: ", error); Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");

View File

@ -14,7 +14,8 @@ namespace Wifi {
void printStatus(); void printStatus();
Task tReconnect(1 * TASK_MINUTE, TASK_FOREVER, []{ Task tReconnect(1 * TASK_MINUTE, TASK_FOREVER,
[] {
if (WiFi.status() != WL_CONNECTED) { if (WiFi.status() != WL_CONNECTED) {
Serial.println("Reconnecting to WiFi netowrk..."); Serial.println("Reconnecting to WiFi netowrk...");
WiFi.forceSleepWake(); WiFi.forceSleepWake();
@ -22,24 +23,29 @@ namespace Wifi {
} }
}); });
Task tInitialConnect(500 * TASK_MILLISECOND, 120, []{ Task tInitialConnect(500 * TASK_MILLISECOND, 120,
[] {
Serial.println("Finding WiFi netowrk..."); Serial.println("Finding WiFi netowrk...");
if (wifiMulti.run() == WL_CONNECTED) tInitialConnect.disable(); if (wifiMulti.run() == WL_CONNECTED) tInitialConnect.disable();
}, nullptr, false, nullptr, []{ }, nullptr, false, nullptr,
[] {
currentSSID = WiFi.SSID(); currentSSID = WiFi.SSID();
currentPsk = WiFi.psk(); currentPsk = WiFi.psk();
tReconnect.enable(); tReconnect.enable();
}); }
);
void setup(Scheduler& ts, void(*onConnected)() = nullptr) { void setup(Scheduler& ts, void(*onConnected)() = nullptr) {
stationConnectedHandler = WiFi.onStationModeGotIP([onConnected](const WiFiEventStationModeGotIP& e) { stationConnectedHandler = WiFi.onStationModeGotIP(
[onConnected](const WiFiEventStationModeGotIP& e) {
Serial.println("Connected to network."); Serial.println("Connected to network.");
printStatus(); printStatus();
tReconnect.cancel(); tReconnect.cancel();
if (onConnected) onConnected(); 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.");
tReconnect.restartDelayed(); tReconnect.restartDelayed();
}); });