refactored into multiple files
This commit is contained in:
parent
aabc35d090
commit
35440ecca0
39
include/display.h
Normal file
39
include/display.h
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#include <Adafruit_LEDBackpack.h> // Support for the Backpack FeatherWing
|
||||||
|
#include <Adafruit_GFX.h> // Adafruit's graphics library
|
||||||
|
#include <SPI.h>
|
||||||
|
#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();
|
||||||
|
}
|
||||||
27
include/ntp_time.h
Normal file
27
include/ntp_time.h
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <NTPClient.h>
|
||||||
|
#include <WiFiUdp.h>
|
||||||
|
#include <Timezone.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
22
include/ota.h
Normal file
22
include/ota.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#include <ArduinoOTA.h>
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
59
include/wifi.h
Normal file
59
include/wifi.h
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ESP8266WiFiMulti.h>
|
||||||
|
#include <ESP8266mDNS.h>
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -1,63 +1,24 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <ESP8266WiFi.h>
|
#include "wifi.h"
|
||||||
#include <ESP8266WiFiMulti.h>
|
#include "ota.h"
|
||||||
#include <NTPClient.h>
|
#include "ntp_time.h"
|
||||||
#include <ESP8266mDNS.h>
|
#include "display.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;
|
int currentHour = -1;
|
||||||
time_t timeAtStartup;
|
time_t timeAtStartup;
|
||||||
|
|
||||||
String currentSSID;
|
Wifi wifi;
|
||||||
String currentPsk;
|
|
||||||
|
|
||||||
WiFiUDP ntpUDP;
|
|
||||||
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org");
|
|
||||||
|
|
||||||
void displayTime();
|
|
||||||
void displayColon(bool on);
|
|
||||||
void setupOTA();
|
|
||||||
void printWiFiStatus();
|
|
||||||
time_t updateTime();
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600); // Start the serial console
|
Serial.begin(9600); // Start the serial console
|
||||||
|
|
||||||
ESP8266WiFiMulti wifiMulti;
|
wifi.setup();
|
||||||
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();
|
|
||||||
|
|
||||||
setupOTA();
|
setupOTA();
|
||||||
|
|
||||||
clockDisplay.begin(DISPLAY_ADDRESS);
|
setupDisplay();
|
||||||
clockDisplay.setBrightness(BRIGHTNESS);
|
|
||||||
|
|
||||||
timeClient.begin();
|
setupTime();
|
||||||
timeAtStartup = updateTime();
|
timeAtStartup = updateTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,24 +26,15 @@ void loop() {
|
|||||||
ArduinoOTA.handle();
|
ArduinoOTA.handle();
|
||||||
|
|
||||||
if ((currentHour != hour())) {
|
if ((currentHour != hour())) {
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
wifi.reconnect();
|
||||||
WiFi.forceSleepWake();
|
wifi.printStatus();
|
||||||
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) {
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
if (time_t newTime = updateTime()) Serial.println(asctime(localtime(&newTime)));
|
if (time_t newTime = updateTime()) Serial.println(asctime(localtime(&newTime)));
|
||||||
}
|
}
|
||||||
currentHour = hour();
|
currentHour = hour();
|
||||||
}
|
}
|
||||||
if (WiFi.status() == WL_CONNECTED && difftime(now(), timeAtStartup) > 60) {
|
if (WiFi.status() == WL_CONNECTED && difftime(now(), timeAtStartup) > 60) {
|
||||||
Serial.println("Disconnecting WiFi");
|
wifi.disconnect();
|
||||||
WiFi.disconnect();
|
|
||||||
WiFi.forceSleepBegin();
|
|
||||||
}
|
}
|
||||||
displayTime();
|
displayTime();
|
||||||
displayColon(true);
|
displayColon(true);
|
||||||
@ -90,75 +42,3 @@ void loop() {
|
|||||||
displayColon(false);
|
displayColon(false);
|
||||||
delay(500);
|
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");
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user