-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntp_clock_controller.ino
136 lines (121 loc) · 2.64 KB
/
ntp_clock_controller.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <WiFi.h>
#include "time.h"
#include "config.h"
// select board DOIT ESP32 DEVKITV1
// make sure config.h contains your wifi password and SSID
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASSWORD;
const char* ntpServer = "pool.ntp.org";
const char* TZ_INFO = "CET-1CEST,M3.5.0/2,M10.5.0/3";
struct tm timeinfo;
int PIN_A = 12;
int PIN_B = 13;
int PIN_EN = 14;
int lastMinute = 0;
int lastHour = 0;
int clockState = PIN_A;
void connectWifi(){
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
for( int timeOut = 0; timeOut < 100; timeOut++){
if(WiFi.status() != WL_CONNECTED) {
Serial.println(" CONNECTED");
break;
}
delay(500);
Serial.print(".");
}
}
void disconnectWifi(){
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
void getCurrentTime(){
configTime(0, 0, ntpServer);
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
setenv("TZ", TZ_INFO, 1);
tzset();
}
void printCurrentTime(){
char strftime_buf[64];
time_t now;
time(&now);
localtime_r(&now, &timeinfo);
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
Serial.printf("Current time: %s\n", strftime_buf);
}
bool checkMinuteHasPassed(){
int currentMinute = timeinfo.tm_min;
if(currentMinute != lastMinute){
lastMinute = currentMinute;
return true;
}
return false;
}
bool checkHourHasPassed(){
int currentHour = timeinfo.tm_hour;
if(currentHour != lastHour){
lastHour = currentHour;
return true;
}
return false;
}
bool checkTimeIsSet(){
int currentYear = timeinfo.tm_year;
if(currentYear == 70) {
return false;
}
return true;
}
void advanceClock(){
digitalWrite(PIN_EN, HIGH);
if(clockState == PIN_A){
clockState = PIN_B;
} else {
clockState = PIN_A;
}
digitalWrite(clockState, HIGH);
delay(1500);
}
void disableOutput(){
digitalWrite(PIN_A, LOW);
digitalWrite(PIN_B, LOW);
digitalWrite(PIN_EN, LOW);
}
void refreshTime() {
connectWifi();
getCurrentTime();
disconnectWifi();
}
void setup()
{
Serial.begin(115200);
delay(1000);
refreshTime();
pinMode(PIN_A, OUTPUT);
pinMode(PIN_B, OUTPUT);
pinMode(PIN_EN, OUTPUT);
advanceClock();
}
void loop()
{
if(checkMinuteHasPassed()) {
Serial.println("Advancing clock");
advanceClock();
}
if(checkHourHasPassed()) {
Serial.println("Connecting to WiFi and refreshing time");
refreshTime();
}
if( !checkTimeIsSet() ) {
Serial.println("Year is 1970, time not set");
Serial.println("Reconnecting to time server");
refreshTime();
}
disableOutput();
printCurrentTime();
delay(1000);
}