diff --git a/src/main.cpp b/src/main.cpp index 773312a..0658b53 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -98,19 +98,8 @@ void setup() flip.sendRaw(rawBuff, sizeof(rawBuff)); /* attempt to connect to Wifi network */ - Serial.print("Connecting to Wifi SSID "); - Serial.print(WIFI_SSID); WiFi.setHostname(HOSTNAME); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); - while (WiFi.status() != WL_CONNECTED) - { - Serial.print("!"); - Serial.print(WiFi.status()); - Serial.print("."); - delay(500); - } - Serial.print("\nWiFi connected. IP address: "); - Serial.println(WiFi.localIP()); /* get internet time */ Serial.print("Retrieving time: "); @@ -127,32 +116,70 @@ void setup() /* setup ArduinoOTA */ ArduinoOTA.setHostname(HOSTNAME); - ArduinoOTA.begin(); ArduinoOTA.onStart(onOTAStart); - - /* initialize and connect telegram bot */ - tele.init(); - - /* initialy print closed at the display */ - printClose(""); } void loop() { + static wl_status_t oldWifiStatus = WL_DISCONNECTED; unsigned long now = millis(); static unsigned long lastDisplayRefresh = now; static unsigned long lastTelegramRefresh = now; - /* OTA handling */ - ArduinoOTA.handle(); - - yield(); - - /* Telegram handling */ - if ((now - lastTelegramRefresh) > 5000) + if (WiFi.status() == WL_CONNECTED) { - lastTelegramRefresh = now; - tele.cyclic(now); + // if the Wifi is connected do all the internet stuff + if (WiFi.status() != oldWifiStatus) + { + /* we freshly reconnected so tell everybody that we are online */ + Serial.print("\nWiFi connected. IP address: "); + Serial.println(WiFi.localIP()); + + Serial.print("Retrieving time: "); + configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP + configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "pool.ntp.org"); + time_t now = time(nullptr); + while (now < 24 * 3600) + { + Serial.print("."); + delay(100); + now = time(nullptr); + } + Serial.println(now); + + ArduinoOTA.begin(); + + tele.init(); + + printClose(""); + + oldWifiStatus = WiFi.status(); + } + + /* handle the OTA update */ + ArduinoOTA.handle(); + + /* handle Telegram messages */ + if ((now - lastTelegramRefresh) > 5000) + { + lastTelegramRefresh = now; + tele.cyclic(now); + } + } + else + { + // if wifi is not connected wait for connection + if (WiFi.status() != oldWifiStatus) + { + /* we freshly disconnected so tell everybody that we are offline */ + Text = "Wifi \""; + Text += WIFI_SSID; + Text += "\" not connected"; + + ArduinoOTA.end(); + + oldWifiStatus = WiFi.status(); + } } /* FlipDot Handling */ @@ -203,6 +230,12 @@ void loop() // send canvas to display flip.sendCanvas(&canvas); } + + /* wait here if loop execution was faster then 200ms but yield minimum once */ + do + { + yield(); + } while ((millis() - now) < 200); } /* function to write text onto the canvas