diff --git a/src/main.cpp b/src/main.cpp index 7fe6603..1ffc107 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,32 +19,41 @@ bool testHTTP() { bool uplinkWorks() { return testHTTP(); } void setup() { - WiFi.begin(WIFI_NAME, WIFI_PASSWORD); Serial.begin(9600); - - Serial.print("Connecting to WiFi"); - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - Serial.println(); - - Serial.print("Connected, IP address: "); - Serial.println(WiFi.localIP()); - pinMode(LED_BUILTIN, OUTPUT); pinMode(RELAY, OUTPUT); } -void loop() { +void testInternet() { if (uplinkWorks()) { - Serial.println("Network is up"); + Serial.println("Internet is up"); digitalWrite(LED_BUILTIN, HIGH); digitalWrite(RELAY, HIGH); } else { - Serial.println("Network is down"); + Serial.println("Internet is down"); digitalWrite(LED_BUILTIN, LOW); digitalWrite(RELAY, LOW); } +} + +void ensureWiFiConnection() { + wl_status_t status = WiFi.status(); + if(status == WL_CONNECTED) return; + else { + Serial.print("WiFi is disconnected, now connecting"); + WiFi.begin(WIFI_NAME, WIFI_PASSWORD); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(); + + Serial.print("Connected, IP address: "); + Serial.println(WiFi.localIP()); + } +} +void loop() { + ensureWiFiConnection(); + testInternet(); delay(5000); // TODO: cleaner interval between tests }