refactored into multiple files

This commit is contained in:
Nicu Hodos 2020-11-04 00:27:50 +01:00
parent aabc35d090
commit 35440ecca0
5 changed files with 158 additions and 131 deletions

39
include/display.h Normal file
View 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
View 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
View 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
View 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");
}
};

View File

@ -1,63 +1,24 @@
#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);
#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");
}