forked from skx/esp8266
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d1-ntp-clock.ino
269 lines (219 loc) · 5.24 KB
/
d1-ntp-clock.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//
// NTP-Based Clock - https://steve.fi/Hardware/
//
// This is a simple program which uses WiFi & an 4x7-segment display
// to show the current time, complete with blinking ":".
//
// Steve
// --
//
//
// WiFi & over the air updates
//
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
//
// For dealing with NTP & the clock.
//
#include "NTPClient.h"
//
// The display-interface
//
#include "TM1637.h"
//
// WiFi setup.
//
#include "WiFiManager.h"
//
// Debug messages over the serial console.
//
#include "debug.h"
//
// The name of this project.
//
// Used for:
// Access-Point name, in config-mode
// OTA name.
//
#define PROJECT_NAME "NTP-CLOCK"
//
// The timezone - comment out to stay at GMT.
//
#define TIME_ZONE (+3)
//
// NTP client, and UDP socket it uses.
//
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
//
// Pin definitions for TM1637 and can be changed to other ports
//
#define CLK D3
#define DIO D2
TM1637 tm1637(CLK, DIO);
//
// Called just before the date/time is updated via NTP
//
void on_before_ntp()
{
DEBUG_LOG("Updating date & time");
}
//
// Called just after the date/time is updated via NTP
//
void on_after_ntp()
{
DEBUG_LOG("Updated NTP client\n");
}
//
// This function is called when the device is powered-on.
//
void setup()
{
// Enable our serial port.
Serial.begin(115200);
// initialize the display
tm1637.init();
// We want to see ":" between the digits.
tm1637.point(true);
//
// Set the intensity - valid choices include:
//
// BRIGHT_DARKEST = 0
// BRIGHT_TYPICAL = 2
// BRIGHTEST = 7
//
tm1637.set(BRIGHT_DARKEST);
//
// Handle WiFi setup
//
WiFiManager wifiManager;
wifiManager.autoConnect(PROJECT_NAME);
//
// Ensure our NTP-client is ready.
//
timeClient.begin();
//
// Configure the callbacks.
//
timeClient.on_before_update(on_before_ntp);
timeClient.on_after_update(on_after_ntp);
//
// Setup the timezone & update-interval.
//
timeClient.setTimeOffset(TIME_ZONE * (60 * 60));
timeClient.setUpdateInterval(300 * 1000);
//
// The final step is to allow over the air updates
//
// This is documented here:
// https://randomnerdtutorials.com/esp8266-ota-updates-with-arduino-ide-over-the-air/
//
// Hostname defaults to esp8266-[ChipID]
//
ArduinoOTA.setHostname(PROJECT_NAME);
ArduinoOTA.onStart([]()
{
DEBUG_LOG("OTA Start\n");
});
ArduinoOTA.onEnd([]()
{
DEBUG_LOG("OTA End\n");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total)
{
char buf[32];
memset(buf, '\0', sizeof(buf));
snprintf(buf, sizeof(buf) - 1, "Upgrade - %02u%%\n", (progress / (total / 100)));
DEBUG_LOG(buf);
});
ArduinoOTA.onError([](ota_error_t error)
{
DEBUG_LOG("Error - ");
if (error == OTA_AUTH_ERROR)
DEBUG_LOG("Auth Failed\n");
else if (error == OTA_BEGIN_ERROR)
DEBUG_LOG("Begin Failed\n");
else if (error == OTA_CONNECT_ERROR)
DEBUG_LOG("Connect Failed\n");
else if (error == OTA_RECEIVE_ERROR)
DEBUG_LOG("Receive Failed\n");
else if (error == OTA_END_ERROR)
DEBUG_LOG("End Failed\n");
});
//
// Ensure the OTA process is running & listening.
//
ArduinoOTA.begin();
}
//
// This function is called continously, and is responsible
// for flashing the ":", and otherwise updating the display.
//
// We rely on the background NTP-updates to actually make sure
// that that works.
//
void loop()
{
static char buf[10] = { '\0' };
static char prev[10] = { '\0' };
static long last_read = 0;
static bool flash = true;
//
// Resync the clock?
//
timeClient.update();
//
// Handle any pending over the air updates.
//
ArduinoOTA.handle();
//
// Get the current hour/min
//
int cur_hour = timeClient.getHours();
int cur_min = timeClient.getMinutes();
//
// Format them in a useful way.
//
sprintf(buf, "%02d%02d", cur_hour, cur_min);
//
// If the current "hourmin" is different to
// that we displayed last loop ..
//
if (strcmp(buf, prev) != 0)
{
// Update the display
tm1637.display(0, buf[0] - '0');
tm1637.display(1, buf[1] - '0');
tm1637.display(2, buf[2] - '0');
tm1637.display(3, buf[3] - '0');
// And cache it
strcpy(prev , buf);
}
//
// The preceeding piece of code would
// have ensured the display only updated
// when the hour/min changed.
//
// However note that we nuke the cached
// value every half-second - solely so we can
// blink the ":".
//
// Sigh
long now = millis();
if ((last_read == 0) ||
(abs(now - last_read) > 500))
{
// Invert the "show :" flag
flash = !flash;
// Apply it.
tm1637.point(flash);
//
// Note that the ":" won't redraw unless/until you update.
// So we'll force that to happen by removing the cached
// value here.
//
memset(prev, '\0', sizeof(prev));
last_read = now;
}
}