diff --git a/library.json b/library.json new file mode 100644 index 0000000..e3a027b --- /dev/null +++ b/library.json @@ -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": "*" + } diff --git a/src/ota.h b/src/ota.h index 43b4c05..148c5ee 100644 --- a/src/ota.h +++ b/src/ota.h @@ -2,34 +2,38 @@ namespace Ota { - void loop(); - Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true); + void loop(); + Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true); - void setup() { - ArduinoOTA.onStart([]() { - Serial.println("Starting OTA"); - Mqtt::publishCleanupConfig(); - delay(2000); - Mqtt::disconnect(); - }); - ArduinoOTA.onEnd([]() { - Serial.println("\nOTA Finished"); - }); - ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { - Serial.printf("Progress: %u%%\r", (progress / (total / 100))); - }); - 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"); - else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); - else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); - else if (error == OTA_END_ERROR) Serial.println("End Failed"); - }); - ArduinoOTA.begin(); - } + void setup() { + ArduinoOTA.onStart( + []() { + Serial.println("Starting OTA"); + Mqtt::publishCleanupConfig(); + delay(2000); + Mqtt::disconnect(); + }); + ArduinoOTA.onEnd( + []() { + Serial.println("\nOTA Finished"); + }); + ArduinoOTA.onProgress( + [](unsigned int progress, unsigned int total) { + Serial.printf("Progress: %u%%\r", (progress / (total / 100))); + }); + 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"); + else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); + else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); + else if (error == OTA_END_ERROR) Serial.println("End Failed"); + }); + ArduinoOTA.begin(); + } - void loop() { - ArduinoOTA.handle(); - } + void loop() { + ArduinoOTA.handle(); + } } diff --git a/src/wifi.h b/src/wifi.h index e47dc8e..1b54e3c 100644 --- a/src/wifi.h +++ b/src/wifi.h @@ -5,76 +5,82 @@ namespace Wifi { - ESP8266WiFiMulti wifiMulti; - WiFiEventHandler stationConnectedHandler; - WiFiEventHandler stationDisconnectedHandler; - - String currentSSID; - String currentPsk; + ESP8266WiFiMulti wifiMulti; + WiFiEventHandler stationConnectedHandler; + WiFiEventHandler stationDisconnectedHandler; - void printStatus(); + String currentSSID; + String currentPsk; - 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 printStatus(); - Task tInitialConnect(500 * TASK_MILLISECOND, 120, []{ - Serial.println("Finding WiFi netowrk..."); - if (wifiMulti.run() == WL_CONNECTED) tInitialConnect.disable(); - }, nullptr, false, nullptr, []{ - currentSSID = WiFi.SSID(); - currentPsk = WiFi.psk(); - tReconnect.enable(); - }); + 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) { - stationConnectedHandler = WiFi.onStationModeGotIP([onConnected](const WiFiEventStationModeGotIP& e) { - Serial.println("Connected to network."); - printStatus(); - tReconnect.cancel(); - if (onConnected) onConnected(); - }); + Task tInitialConnect(500 * TASK_MILLISECOND, 120, + [] { + Serial.println("Finding WiFi netowrk..."); + if (wifiMulti.run() == WL_CONNECTED) tInitialConnect.disable(); + }, nullptr, false, nullptr, + [] { + currentSSID = WiFi.SSID(); + currentPsk = WiFi.psk(); + tReconnect.enable(); + } + ); - stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) { - Serial.println("Disconnected from network."); - tReconnect.restartDelayed(); - }); + void setup(Scheduler& ts, void(*onConnected)() = nullptr) { + 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."); + tReconnect.restartDelayed(); + }); - WiFi.setHostname(MAIN_DEVICE_ID); - for (uint32_t i = 0; i < sizeof(credentials) / sizeof(WifiCredentials); i++) { - wifiMulti.addAP(credentials[i].ssid, credentials[i].password); + WiFi.setHostname(MAIN_DEVICE_ID); + for (uint32_t i = 0; i < sizeof(credentials) / sizeof(WifiCredentials); i++) { + wifiMulti.addAP(credentials[i].ssid, credentials[i].password); + } + + ts.addTask(tInitialConnect); + ts.addTask(tReconnect); + tInitialConnect.enable(); } - ts.addTask(tInitialConnect); - ts.addTask(tReconnect); - tInitialConnect.enable(); - } + void disconnect() { + Serial.println("Disconnecting WiFi"); + WiFi.disconnect(); + WiFi.forceSleepBegin(); + } - void disconnect() { - Serial.println("Disconnecting WiFi"); - WiFi.disconnect(); - WiFi.forceSleepBegin(); - } + void printStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); - void printStatus() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); - // print your WiFi shield's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); - Serial.println(ip); - - // print the received signal strength: - long rssi = WiFi.RSSI(); - Serial.print("signal strength (RSSI):"); - Serial.print(rssi); - Serial.println(" dBm"); - } + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); + } }