create the library
This commit is contained in:
parent
f8aa7dfabd
commit
79d16c0098
21
library.json
Normal file
21
library.json
Normal 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": "*"
|
||||
}
|
||||
12
src/ota.h
12
src/ota.h
@ -6,19 +6,23 @@ namespace Ota {
|
||||
Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true);
|
||||
|
||||
void setup() {
|
||||
ArduinoOTA.onStart([]() {
|
||||
ArduinoOTA.onStart(
|
||||
[]() {
|
||||
Serial.println("Starting OTA");
|
||||
Mqtt::publishCleanupConfig();
|
||||
delay(2000);
|
||||
Mqtt::disconnect();
|
||||
});
|
||||
ArduinoOTA.onEnd([]() {
|
||||
ArduinoOTA.onEnd(
|
||||
[]() {
|
||||
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)));
|
||||
});
|
||||
ArduinoOTA.onError([](ota_error_t error) {
|
||||
ArduinoOTA.onError(
|
||||
[](ota_error_t error) {
|
||||
Serial.printf("Error[%u]: ", error);
|
||||
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
|
||||
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
|
||||
|
||||
18
src/wifi.h
18
src/wifi.h
@ -14,7 +14,8 @@ namespace Wifi {
|
||||
|
||||
void printStatus();
|
||||
|
||||
Task tReconnect(1 * TASK_MINUTE, TASK_FOREVER, []{
|
||||
Task tReconnect(1 * TASK_MINUTE, TASK_FOREVER,
|
||||
[] {
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.println("Reconnecting to WiFi netowrk...");
|
||||
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...");
|
||||
if (wifiMulti.run() == WL_CONNECTED) tInitialConnect.disable();
|
||||
}, nullptr, false, nullptr, []{
|
||||
}, nullptr, false, nullptr,
|
||||
[] {
|
||||
currentSSID = WiFi.SSID();
|
||||
currentPsk = WiFi.psk();
|
||||
tReconnect.enable();
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
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.");
|
||||
printStatus();
|
||||
tReconnect.cancel();
|
||||
if (onConnected) onConnected();
|
||||
});
|
||||
|
||||
stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) {
|
||||
stationDisconnectedHandler = WiFi.onStationModeDisconnected(
|
||||
[](const WiFiEventStationModeDisconnected& e) {
|
||||
Serial.println("Disconnected from network.");
|
||||
tReconnect.restartDelayed();
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user