-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDPtime.ino
484 lines (361 loc) · 13.3 KB
/
UDPtime.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/**********************************************************************
Get NTP time for Patagonia (or any place...)
(c) 2017 gosouth.cl, Ricardo Timmermann
// http://support.ntp.org/bin/view/Servers/PublicTimeServer000277
// http://support.ntp.org/bin/view/Main/DocumentationIndex
// https://www.eecis.udel.edu/~mills/time.html#intro
Note: SoftwareSerial will not work for HIGHER BAUDRATES, so reprogram ESP8266 for 9600 baud.
This is done simple trough AT commandos (AT+CIOBAUD=9600) and using a simple terminal
and the 3.3V serial cable: IoT - First Test of ESP8266 WiFi Module · Hannes Lehmann
TODO:
* Jumper to IO13 for summer/winter time. Chile has no clear date to switch. Depends of politicians.
* Nicer code with arrays for I/O pins
**********************************************************************/
// #define SERIAL_RX_BUFFER_SIZE 256
// #define SERIAL_TX_BUFFER_SIZE 128
#include "ESP8266.h"
#include "TimerOne.h"
#define SSID "Quilaco"
#define PASSWORD ""
#define HOST_NAME "129.6.15.28" // time-a.nist.gov,
#define HOST_PORT (123)
#define MY_TIMEZONE (3600*3) // chilean time zone
#define TIME_SCAN (10*60000) // read TIME each 10 minutes
#define MINUTE 59500 // arduino millis in one minute, not exact
SoftwareSerial ESPserial(2, 3); // RX | TX
ESP8266 wifi(ESPserial); // wifi initializes SoftwareSerial()
#define NTP_PACKET_SIZE 48 // 48
byte NTPpacket[ NTP_PACKET_SIZE];
// == diplay defines =================================================
int digit1 = 7; //PWM Display pin 1
int digit2 = 6; //PWM Display pin 2
int digit3 = 5; //PWM Display pin 6
int digit4 = 4; //PWM Display pin 8
int segA = 8; //Display pin 14
int segB = 9; //Display pin 16
int segC = 10; //Display pin 13
int segD = 11; //Display pin 3
int segE = 12; //Display pin 5
int segF = A2; //Display pin 11
int segG = A0; //Display pin 15
#define DP A1 // decimal point
#define DIPSW_SUMMERTIME 13
void getNTPTime(void);
unsigned long epoch;
bool noDisturb;
volatile unsigned long tm, minits, NTP_scan;
int brightness = 100;
bool dpSecond = false;
// == interrupt settings ====================================================
void displayTime()
{
// Serial.println( epoch );
if( noDisturb ) return;
// == display the hour: minute =======================
int hour = (epoch % 86400L) / 3600;
int minutes = (epoch % 3600) / 60;
displayNumber( hour * 100 + minutes );
}
// == setup ==========================================================
//
// * Connect ESP8266 wifi and setup in single TCP/UDP
// * Build NTP time request packet
void setup(void)
{
Serial.begin(9600);
delay(1000);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
// == display =========================
pinMode( segA, OUTPUT );
pinMode( segB, OUTPUT );
pinMode( segC, OUTPUT );
pinMode( segD, OUTPUT );
pinMode( segE, OUTPUT );
pinMode( segF, OUTPUT );
pinMode( segG, OUTPUT );
pinMode( DP, OUTPUT );
pinMode( digit1, OUTPUT );
pinMode( digit2, OUTPUT );
pinMode( digit3, OUTPUT );
pinMode( digit4, OUTPUT );
pinMode( DIPSW_SUMMERTIME, INPUT_PULLUP );
Serial.println("== SETUP START ========\n\r");
Serial.println("FW Version:");
Serial.println(wifi.getVersion().c_str());
if (wifi.setOprToStationSoftAP()) Serial.print("to station + softap ok\r\n");
else Serial.print("to station + softap err\r\n");
// == connect to WLAN ============================
if (wifi.joinAP(SSID, PASSWORD)) {
Serial.print("IP:");
Serial.println( wifi.getLocalIP().c_str());
}
else Serial.print("Join AP failure\r\n");
// == disable MUX, only one TCP or UDP communication can be builded
if (wifi.disableMUX()) Serial.println("single ok\r\n");
else Serial.println("single err\r\n");
// == build NTP time request packet ==============================
// http://www.networksorcery.com/enp/protocol/ntp.htm#Poll_interval
memset(NTPpacket, 0, NTP_PACKET_SIZE);
NTPpacket[0] = 0b11100011; // LI, Version, Mode
NTPpacket[1] = 0; // Stratum, or type of clock
NTPpacket[2] = 6; // Polling Interval
NTPpacket[3] = 0xEC; // Peer Clock Precision
// .. 8 bytes of zero for Root Delay & Root Dispersion
NTPpacket[12] = 49;
NTPpacket[13] = 0x4E;
NTPpacket[14] = 49;
NTPpacket[15] = 52;
Serial.println("== SETUP END =========\n\r");
// == timer for display multiplexing ============
noDisturb = false;
Timer1.initialize( 20000 ); // 20 ms
Timer1.attachInterrupt( displayTime );
tm = millis();
NTP_scan = TIME_SCAN/20;
}
// == main loop ============================================================
//
//
// NTP allows open access for up to 20 queries per hour, I use 1 / minute = 60
void loop(void)
{
// == call NTP Time nly each 10 mins, needed only to synchronize my time.
/*
Serial.print( tm );
Serial.print( " - " );
Serial.println( millis() );
String s = wifi.getLocalIP();
Serial.println( s );
*/
// call only if we have wifi
if( millis() - tm > NTP_scan ) {
tm = millis();
getNTPTime();
}
// == increment minute in epoch, for when NPT is not called. Adjust clock around 60000
if( millis() - minits > MINUTE ) {
epoch += 60;
minits = millis();
}
brightness = 1024 - analogRead(A3);
// Serial.println( brightness );
brightness = max( 10, brightness );
dpSecond = !dpSecond;
delay( 1000 );
/*
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
Serial.print( hour ); // print the hour (86400 secs / day)
// print minutes
Serial.print(':');
if (minutes)
Serial.print('0');
Serial.print( minutes ); // print the minute (3600 secs / minute)
// print seconds
Serial.print(':');
if ((epoch % 60) < 10) Serial.print('0');
Serial.print(epoch % 60);
Serial.print(" = ");
Serial.println( hour * 100 + minutes );
*/
}
// == getNTPTime =======================================================
//
// get NTP time from a UTP host
//
#define SECS_70YEARS 2208988800UL
void getNTPTime()
{
noDisturb = true; // disable interrupt
uint8_t buffer[NTP_PACKET_SIZE] = {0}; // uint8_t
if (wifi.registerUDP(HOST_NAME, HOST_PORT)) Serial.print("register UDP ok\r\n");
else { Serial.print("register UDP err\r\n"); return; }
// == sent NTP time request packet
wifi.send( NTPpacket, NTP_PACKET_SIZE );
// == read from time server
uint32_t len = wifi.recv(buffer, NTP_PACKET_SIZE, 2000);
Serial.print("NTP_PACKET_SIZE = ");
Serial.println(len);
if( len == NTP_PACKET_SIZE ) {
NTP_scan = TIME_SCAN;
digitalWrite(LED_BUILTIN, HIGH);
// the timestamp starts at byte 40 and is 4 byte long
unsigned long highWord = word(buffer[40], buffer[41]);
unsigned long lowWord = word(buffer[42], buffer[43]);
// join the two words into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print("Seconds since Jan 1 1900 = ");
Serial.println(secsSince1900);
// now convert NTP time into Unix time
// Unix time starts on Jan 1 1970
// https://www.epochconverter.com
epoch = secsSince1900 - SECS_70YEARS;
epoch -= MY_TIMEZONE;
if( digitalRead( DIPSW_SUMMERTIME) )
epoch -= 3600;
}
else {
Serial.println("wrong NTP_PACKET_SIZE");
}
if (wifi.unregisterUDP()) Serial.print("unregister udp ok\r\n");
else Serial.print("unregister udp err\r\n");
digitalWrite(LED_BUILTIN, LOW);
noDisturb = false;
}
/*-----------------------------------------------------------------------
;
; Display number on 4x7 Segment display, SW multiplexed
;
;-----------------------------------------------------------------------*/
void displayNumber( int toDisplay ) {
#define DISPLAY_BRIGHTNESS 400
#define DIGIT_ON HIGH
#define DIGIT_OFF LOW
long beginTime = millis();
for( int digit=4 ; digit>0 ; digit-- ) {
digitalWrite(DP, LOW);
//Turn on a digit for a short amount of time
switch(digit) {
case 1:
digitalWrite(digit1, DIGIT_ON);
break;
case 2:
digitalWrite(digit2, DIGIT_ON);
if( dpSecond ) digitalWrite(DP, HIGH);
break;
case 3:
digitalWrite(digit3, DIGIT_ON);
break;
case 4:
digitalWrite(digit4, DIGIT_ON);
break;
}
//Turn on the right segments for this digit
showDigit(toDisplay % 10);
toDisplay /= 10;
//Display digit for fraction of a second (1us to 5000us, 500 is pretty good)
delayMicroseconds( brightness );
//Turn off all segments
showDigit(10);
//Turn off all digits
digitalWrite(digit1, DIGIT_OFF);
digitalWrite(digit2, DIGIT_OFF);
digitalWrite(digit3, DIGIT_OFF);
digitalWrite(digit4, DIGIT_OFF);
}
}
/*-----------------------------------------------------------------------
;
; Display a single selected digit
;
; could be done much nicer...
; If number == 10, then turn off number
;
;-----------------------------------------------------------------------*/
void showDigit(int number)
{
#define SEGMENT_ON HIGH
#define SEGMENT_OFF LOW
switch (number){
case 0:
digitalWrite(segA, SEGMENT_ON);
digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_ON);
digitalWrite(segE, SEGMENT_ON);
digitalWrite(segF, SEGMENT_ON);
digitalWrite(segG, SEGMENT_OFF);
break;
case 1:
digitalWrite(segA, SEGMENT_OFF);
digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_OFF);
digitalWrite(segE, SEGMENT_OFF);
digitalWrite(segF, SEGMENT_OFF);
digitalWrite(segG, SEGMENT_OFF);
break;
case 2:
digitalWrite(segA, SEGMENT_ON);
digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_OFF);
digitalWrite(segD, SEGMENT_ON);
digitalWrite(segE, SEGMENT_ON);
digitalWrite(segF, SEGMENT_OFF);
digitalWrite(segG, SEGMENT_ON);
break;
case 3:
digitalWrite(segA, SEGMENT_ON);
digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_ON);
digitalWrite(segE, SEGMENT_OFF);
digitalWrite(segF, SEGMENT_OFF);
digitalWrite(segG, SEGMENT_ON);
break;
case 4:
digitalWrite(segA, SEGMENT_OFF);
digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_OFF);
digitalWrite(segE, SEGMENT_OFF);
digitalWrite(segF, SEGMENT_ON);
digitalWrite(segG, SEGMENT_ON);
break;
case 5:
digitalWrite(segA, SEGMENT_ON);
digitalWrite(segB, SEGMENT_OFF);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_ON);
digitalWrite(segE, SEGMENT_OFF);
digitalWrite(segF, SEGMENT_ON);
digitalWrite(segG, SEGMENT_ON);
break;
case 6:
digitalWrite(segA, SEGMENT_ON);
digitalWrite(segB, SEGMENT_OFF);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_ON);
digitalWrite(segE, SEGMENT_ON);
digitalWrite(segF, SEGMENT_ON);
digitalWrite(segG, SEGMENT_ON);
break;
case 7:
digitalWrite(segA, SEGMENT_ON);
digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_OFF);
digitalWrite(segE, SEGMENT_OFF);
digitalWrite(segF, SEGMENT_OFF);
digitalWrite(segG, SEGMENT_OFF);
break;
case 8:
digitalWrite(segA, SEGMENT_ON);
digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_ON);
digitalWrite(segE, SEGMENT_ON);
digitalWrite(segF, SEGMENT_ON);
digitalWrite(segG, SEGMENT_ON);
break;
case 9:
digitalWrite(segA, SEGMENT_ON);
digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_ON);
digitalWrite(segE, SEGMENT_OFF);
digitalWrite(segF, SEGMENT_ON);
digitalWrite(segG, SEGMENT_ON);
break;
case 10:
digitalWrite(segA, SEGMENT_OFF);
digitalWrite(segB, SEGMENT_OFF);
digitalWrite(segC, SEGMENT_OFF);
digitalWrite(segD, SEGMENT_OFF);
digitalWrite(segE, SEGMENT_OFF);
digitalWrite(segF, SEGMENT_OFF);
digitalWrite(segG, SEGMENT_OFF);
break;
}
}