commit 7428b4cca951f3cc70690972fe675d0d85131a32 Author: Nicu Hodos Date: Wed Nov 4 00:27:50 2020 +0100 refactored into multiple files diff --git a/src/ota.h b/src/ota.h new file mode 100644 index 0000000..835b80d --- /dev/null +++ b/src/ota.h @@ -0,0 +1,22 @@ +#include + +void setupOTA() { + ArduinoOTA.onStart([]() { + Serial.println("Start"); + }); + ArduinoOTA.onEnd([]() { + Serial.println("\nEnd"); + }); + 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(); +} diff --git a/src/wifi.h b/src/wifi.h new file mode 100644 index 0000000..5b6ef5d --- /dev/null +++ b/src/wifi.h @@ -0,0 +1,59 @@ +#include +#include +#include + +class Wifi { + +private: + String currentSSID; + String currentPsk; + +public: + void setup() { + ESP8266WiFiMulti wifiMulti; + wifiMulti.addAP("IoT", "inT#0trsNgs"); + wifiMulti.addAP("Miracle", "adriana1978"); + + Serial.println("Connecting to WiFi netowrk."); + while (wifiMulti.run() != WL_CONNECTED) { + delay(500); + } + Serial.println("Connected to network."); + 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..."); + for (int i = 0; i < 4; i++) { + delay(1000); + } + } + } + + 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()); + + // 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"); + } +}; \ No newline at end of file