diff --git a/include/display.h b/include/display.h new file mode 100644 index 0000000..d5250c3 --- /dev/null +++ b/include/display.h @@ -0,0 +1,39 @@ +#include // Support for the Backpack FeatherWing +#include // Adafruit's graphics library +#include +#include "ntp_time.h" + +#define DISPLAY_ADDRESS 0x70 +#define BRIGHTNESS 1 + +// Create display object +Adafruit_7segment clockDisplay = Adafruit_7segment(); + +void setupDisplay() { + clockDisplay.begin(DISPLAY_ADDRESS); + clockDisplay.setBrightness(BRIGHTNESS); +} + +void displayTime() { + int displayHour = hour(); + int displayMinute = minute(); + int displayValue = displayHour*100 + displayMinute; + + // 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); + if (displayMinute < 10) { + clockDisplay.writeDigitNum(3, 0); + } + } +} + +void displayColon(bool on) { + clockDisplay.drawColon(on); + clockDisplay.writeDisplay(); +} diff --git a/include/ntp_time.h b/include/ntp_time.h new file mode 100644 index 0000000..6c85dad --- /dev/null +++ b/include/ntp_time.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include + +WiFiUDP ntpUDP; +NTPClient timeClient(ntpUDP, "europe.pool.ntp.org"); + +// Central European Time (Frankfurt, Paris) +TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; // Central European Summer Time +TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60}; // Central European Standard Time +Timezone CE(CEST, CET); + +void setupTime() { + timeClient.begin(); +} + +time_t updateTime() { + if (timeClient.forceUpdate()) { + time_t newTime = CE.toLocal(timeClient.getEpochTime()); + setTime(newTime); + return newTime; + } else { + return 0; + } +} diff --git a/include/ota.h b/include/ota.h new file mode 100644 index 0000000..835b80d --- /dev/null +++ b/include/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/include/wifi.h b/include/wifi.h new file mode 100644 index 0000000..b140fca --- /dev/null +++ b/include/wifi.h @@ -0,0 +1,59 @@ +#include +#include +#include + +class Wifi { + +private: + String currentSSID; + String currentPsk; + +public: + void setup() { + ESP8266WiFiMulti wifiMulti; + wifiMulti.addAP("IoT", "***REMOVED***"); + wifiMulti.addAP("Miracle", "***REMOVED***"); + + 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 diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index b4e73ee..d132a5b 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -1,63 +1,24 @@ #include -#include -#include -#include -#include -#include -#include -#include // Support for the Backpack FeatherWing -#include // Adafruit's graphics library -#include -#include -#include - -#define DISPLAY_ADDRESS 0x70 -#define BRIGHTNESS 1 - -// Create display object -Adafruit_7segment clockDisplay = Adafruit_7segment(); - -// Central European Time (Frankfurt, Paris) -TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; // Central European Summer Time -TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60}; // Central European Standard Time -Timezone CE(CEST, CET); +#include "wifi.h" +#include "ota.h" +#include "ntp_time.h" +#include "display.h" int currentHour = -1; time_t timeAtStartup; -String currentSSID; -String currentPsk; - -WiFiUDP ntpUDP; -NTPClient timeClient(ntpUDP, "europe.pool.ntp.org"); - -void displayTime(); -void displayColon(bool on); -void setupOTA(); -void printWiFiStatus(); -time_t updateTime(); +Wifi wifi; void setup() { Serial.begin(9600); // Start the serial console - ESP8266WiFiMulti wifiMulti; - wifiMulti.addAP("IoT", "***REMOVED***"); - wifiMulti.addAP("Miracle", "***REMOVED***"); - - Serial.println("Connecting to WiFi netowrk."); - while (wifiMulti.run() != WL_CONNECTED) { - delay(500); - } - Serial.println("Connected to network."); - currentSSID = WiFi.SSID(); - currentPsk = WiFi.psk(); + wifi.setup(); setupOTA(); - clockDisplay.begin(DISPLAY_ADDRESS); - clockDisplay.setBrightness(BRIGHTNESS); + setupDisplay(); - timeClient.begin(); + setupTime(); timeAtStartup = updateTime(); } @@ -65,24 +26,15 @@ void loop() { ArduinoOTA.handle(); if ((currentHour != hour())) { - 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); - } - } - printWiFiStatus(); + wifi.reconnect(); + wifi.printStatus(); if (WiFi.status() == WL_CONNECTED) { if (time_t newTime = updateTime()) Serial.println(asctime(localtime(&newTime))); } currentHour = hour(); } if (WiFi.status() == WL_CONNECTED && difftime(now(), timeAtStartup) > 60) { - Serial.println("Disconnecting WiFi"); - WiFi.disconnect(); - WiFi.forceSleepBegin(); + wifi.disconnect(); } displayTime(); displayColon(true); @@ -90,75 +42,3 @@ void loop() { displayColon(false); delay(500); } - -void displayTime() { - int displayHour = hour(); - int displayMinute = minute(); - int displayValue = displayHour*100 + displayMinute; - - // 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); - if (displayMinute < 10) { - clockDisplay.writeDigitNum(3, 0); - } - } -} - -void displayColon(bool on) { - clockDisplay.drawColon(on); - clockDisplay.writeDisplay(); -} - -time_t updateTime() { - if (timeClient.forceUpdate()) { - time_t newTime = CE.toLocal(timeClient.getEpochTime()); - setTime(newTime); - return newTime; - } else { - return 0; - } -} - -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(); -} - -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"); -}