181 lines
5.2 KiB
C++
181 lines
5.2 KiB
C++
//##########################################################################
|
|
// Includes and defines
|
|
//##########################################################################
|
|
|
|
#include <ESP8266WiFi.h>
|
|
#include <ESP8266WiFiMulti.h>
|
|
#include <NTPClient.h>
|
|
#include <ESP8266mDNS.h>
|
|
#include <WiFiUdp.h>
|
|
#include <ArduinoOTA.h>
|
|
#include <Adafruit_LEDBackpack.h> // Support for the Backpack FeatherWing
|
|
#include <Adafruit_GFX.h> // 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;
|
|
String currentSSID;
|
|
String currentPsk;
|
|
|
|
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.");
|
|
currentSSID = WiFi.SSID();
|
|
currentPsk = WiFi.psk();
|
|
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());
|
|
WiFi.forceSleepBegin();
|
|
} 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 == 0)) {
|
|
WiFi.forceSleepWake();
|
|
WiFi.begin(currentSSID.c_str(), currentPsk.c_str());
|
|
for (int i = 0; i < 4; i++) {
|
|
Serial.println("Reconnecting to WiFi netowrk...");
|
|
delay(1000);
|
|
seconds++;
|
|
}
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
shouldUpdate = true;
|
|
} else {
|
|
WiFi.forceSleepBegin();
|
|
}
|
|
printWiFiStatus();
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
if (minutes < 10) {
|
|
clockDisplay.writeDigitNum(3, 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([]() {
|
|
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();
|
|
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");
|
|
}
|