//########################################################################## // Includes and defines //########################################################################## #include #include #include #include #include #include #include // Support for the Backpack FeatherWing #include // Adafruit's graphics library #define STASSID "Miracle" #define STAPSK "***REMOVED***" #define TIME_24_HOUR #define DISPLAY_ADDRESS 0x70 #define BRIGHTNESS 1 //########################################################################## // Globals //########################################################################## // Create display object Adafruit_7segment clockDisplay = Adafruit_7segment(); int hours = 0; // Track hours int minutes = 0; // Track minutes int seconds = 0; // Track seconds int tzOffset = +2; // Time zone offset (-4 = US Eastern time) #ifdef TIME_24_HOUR byte displayHours[24] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}; #else byte displayHours[24] = {12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; #endif bool blinkColon = false; // Track colon status to blink once/sec int status = WL_IDLE_STATUS; bool shouldUpdate = true; ESP8266WiFiMulti wifiMulti; WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", tzOffset*3600); void setup() { Serial.begin(9600); // Start the serial console wifiMulti.addAP("vulturul", "***REMOVED***"); wifiMulti.addAP("Miracle", "***REMOVED***"); Serial.println("Connecting to WiFi netowrk."); while (wifiMulti.run() != WL_CONNECTED) { delay(500); } Serial.println("Connected to network."); printWiFiStatus(); // Display WiFi status data setupOTA(); clockDisplay.begin(DISPLAY_ADDRESS); clockDisplay.setBrightness(BRIGHTNESS); timeClient.begin(); } void loop() { ArduinoOTA.handle(); if (shouldUpdate && WiFi.status() == WL_CONNECTED && timeClient.forceUpdate()) { shouldUpdate = false; hours = timeClient.getHours(); minutes = timeClient.getMinutes(); seconds = timeClient.getSeconds(); Serial.println(timeClient.getFormattedTime()); } else { incrementTime(); } displayTime(); delay(1000); } void incrementTime() { if (++seconds == 60) { seconds = 0; if (++minutes == 60) { minutes = 0; Serial.println("Minutes set to zero - should query NTP on next loop()"); if (++hours == 24) hours = 0; } if (((minutes % 5) == 0)) shouldUpdate = true; } } void displayTime() { int displayHour = displayHours[hours]; int displayValue = displayHour*100 + minutes; // Print the time on the display clockDisplay.print(displayValue, DEC); // Add zero padding when in 24 hour mode and it's midnight. // In this case the print function above won't have leading 0's // which can look confusing. Go in and explicitly add these zeros. if (displayHour == 0) { clockDisplay.writeDigitNum(1, 0); } // Blink the colon by flipping its value every loop iteration // (which happens every second). blinkColon = !blinkColon; clockDisplay.drawColon(blinkColon); // Now push out to the display the new values that were set above. clockDisplay.writeDisplay(); } void setupOTA() { ArduinoOTA.onStart([]() { String type; if (ArduinoOTA.getCommand() == U_FLASH) { type = "sketch"; } else { // U_FS type = "filesystem"; } // NOTE: if updating FS this would be the place to unmount FS using FS.end() Serial.println("Start updating " + type); }); 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(); Serial.println("Ready"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } void printWiFiStatus() { // 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"); }