150 lines
4.0 KiB
C++
150 lines
4.0 KiB
C++
#include <Arduino.h>
|
|
#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
|
|
#include <SPI.h>
|
|
#include <Timezone.h>
|
|
#include <time.h>
|
|
|
|
#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);
|
|
|
|
int currentHour = -1;
|
|
|
|
String currentSSID;
|
|
String currentPsk;
|
|
|
|
WiFiUDP ntpUDP;
|
|
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org");
|
|
|
|
void displayTime();
|
|
void displayColon(bool on);
|
|
void setupOTA();
|
|
void printWiFiStatus();
|
|
|
|
void setup() {
|
|
Serial.begin(9600); // Start the serial console
|
|
|
|
ESP8266WiFiMulti wifiMulti;
|
|
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();
|
|
|
|
setupOTA();
|
|
|
|
clockDisplay.begin(DISPLAY_ADDRESS);
|
|
clockDisplay.setBrightness(BRIGHTNESS);
|
|
|
|
timeClient.begin();
|
|
}
|
|
|
|
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();
|
|
if (WiFi.status() == WL_CONNECTED && timeClient.forceUpdate()) {
|
|
time_t newTime = CE.toLocal(timeClient.getEpochTime());
|
|
setTime(newTime);
|
|
Serial.println(asctime(localtime(&newTime)));
|
|
}
|
|
WiFi.forceSleepBegin();
|
|
currentHour = hour();
|
|
}
|
|
displayTime();
|
|
displayColon(true);
|
|
delay(500);
|
|
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();
|
|
}
|
|
|
|
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");
|
|
}
|