-
Notifications
You must be signed in to change notification settings - Fork 6
/
word-clock.ino
524 lines (457 loc) · 17.5 KB
/
word-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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/****************************************
* Word clock
*
* Fork of code from https://bitbucket.org/vdham/wordclock/
* Dutch clockface
*
* +5V
* ^
* WEMOS D1 mini V2 |
* +-----------------+ |
* +-----------+3V3 5V+-----+------+ POS
* | | | Power jack
* | | GND+-----+------+ NEG
* +++ | | | ^
* | | LDR | | | |
* | | | | v --- 1000uF
* +++ | | GND ---
* | | | +5V |
* +++ | | ^ v
* | | 1k8 | | | +--------------
* | | | | +--+ +5V
* +++ | | +---+ |
* | | +---+ +-----+ DIN WS2812b LED strip
* +-----------+A0 | +---+ | 95 LEDs
* | +-----------------+ +--+ GND
* +++ | +--------------
* | | 820 v
* | | GND
* +++
* |
* v
* GND
*
*
*/
#define FASTLED_ESP8266_RAW_PIN_ORDER
#define FASTLED_ALLOW_INTERRUPTS 0
#include "FastLED.h"
#include <FS.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <TimeLib.h>
#include <ESP8266HTTPClient.h>
#include <vector>
// Use time or LDR light sensor for auto brightness adjustment.
//#define TIME_BRIGHTNESS
#define LDR_BRIGHTNESS
const char* OTApass = "wordclock-OTA";
#define R_VALUE 0
#define G_VALUE 255
#define B_VALUE 0
#define MIN_BRIGHTNESS 65
#define MAX_BRIGHTNESS 255
#define BRIGHTNESS 255 // legacy, keep at 255
int lastBrightness = MIN_BRIGHTNESS;
int dayHour = 8; // Start increasing brightness
int nightHour = 22; // Start decreasing brightness
#define SYNC_INTERVAL 1200
#define NUM_LEDS 95
#define DATA_PIN 4 // D2 Pin on Wemos mini
#define LDR_PIN A0
#define LDR_DARK 10
#define LDR_LIGHT 200
const int timeZone = 1; // Central European Time
bool autoDST = true;
IPAddress timeServerIP; // time.nist.gov NTP server address
const char* ntpServerName = "nl.pool.ntp.org";
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
WiFiUDP Udp;
unsigned int localPort = 8888;
time_t getNtpTime();
void sendNTPpacket(IPAddress &address);
unsigned long lastdisplayupdate = 0;
CRGB leds[NUM_LEDS];
uint8_t targetlevels[NUM_LEDS];
uint8_t currentlevels[NUM_LEDS];
void rainbow();
bool isDST(int d, int m, int y);
bool isDSTSwitchDay(int d, int m, int y);
void updateBrightness();
int timeBrightness();
void setup() {
pinMode(LDR_PIN, INPUT);
Serial.begin(115200);
WiFiManager wifiManager;
String ssid = "WordClock-" + String(ESP.getChipId());
wifiManager.autoConnect(ssid.c_str());
Serial.println("Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Starting UDP");
Udp.begin(localPort);
ArduinoOTA.setPassword(OTApass);
ArduinoOTA.onStart([]() {
Serial.println("Start");
for(int i=0;i<NUM_LEDS;i++) {
targetlevels[i] = 0;
currentlevels[i] = 0;
leds[i] = CRGB::Black;
}
leds[0] = CRGB::Red;
FastLED.show();
});
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");
ESP.restart();
});
ArduinoOTA.begin();
LEDS.addLeds<NEOPIXEL,DATA_PIN>(leds,NUM_LEDS);
LEDS.setBrightness(87);
rainbow();
LEDS.setBrightness(MIN_BRIGHTNESS);
for(int i=0;i<NUM_LEDS;i++) {
targetlevels[i] = 0;
currentlevels[i] = 0;
leds[i] = CRGB::Black;
}
FastLED.show();
setSyncProvider(getNtpTime);
setSyncInterval(SYNC_INTERVAL);
}
void rainbow() {
uint8_t gHue = 0;
while (gHue < 255) {
EVERY_N_MILLISECONDS(20) {gHue++;}
fill_rainbow(leds, NUM_LEDS, gHue, 1);
FastLED.delay(1000/30); // 30FPS
}
}
/*
HET IS X UUR
HET IS VIJF OVER X
HET IS TIEN OVER X
HET IS KWART OVER X
HET IS TIEN VOOR HALF (X+1)
HET IS VIJF VOOR HALF (X+1)
HET IS HALF (X+1)
HET IS VIJF OVER HALF (X+1)
HET IS TIEN OVER HALF (X+1)
HET IS KWART VOOR (X+1)
HET IS TIEN VOOR (X+1)
HET IS VIJF VOOR (X+1)
HET IS (X+1) UUR
...
*/
#define HETIS 0
#define VIJF 13
#define TIEN 14
#define KWART 15
#define VOOR 16
#define OVER 17
#define HALF 18
#define UUR 19
std::vector<std::vector<int>> ledsbyword = {
{84,85,86, 89,90}, // HET IS
{4,5,6}, // een
{47,48,49,50}, // twee
{43,42,41,40}, // drie
{23,22,21,20}, // vier
{7,8,9,10}, // vijf
{51,52,53}, // zes
{25,26,27,28,29}, // zeven
{44,45,46,47}, // acht
{29,30,31,32,33}, // negen
{37,36,35,34}, // tien
{40,39,38}, // elf
{19,18,17,16,15,14},// twaalf
{82,81,80,79}, // VIJF
{78,77,76,75}, // TIEN
{64,65,66,67,68}, // KWART
{63,62,61,60}, // VOOR
{70,71,72,73}, // OVER
{58,57,56,55}, // HALF
{11,12,13} // UUR
};
void loop() {
// put your main code here, to run repeatedly:
ArduinoOTA.handle();
// Serial.println("loop");
Serial.print(analogRead(LDR_PIN));
Serial.print(", ");
Serial.println(lastBrightness);
// only update clock every 50ms
if(millis()-lastdisplayupdate > 50) {
lastdisplayupdate = millis();
}else{
return;
}
#ifdef TIME_BRIGHTNESS
LEDS.setBrightness(timeBrightness());
#endif
#ifdef LDR_BRIGHTNESS
updateBrightness();
#endif
// if not connected, then show waiting animation
if(timeStatus() == timeNotSet) {
// show initialisation animation
Serial.println("time not yet known");
for(int i=0;i<NUM_LEDS;++i){ //blank rest
leds[i] = CRGB::Black;
}
float phase = ((float)(millis()%2000)) / 1000;
if(phase > 1) phase = 2.0f-phase;
for(int i=0;i<4;++i){ // the scanner moves from 0 to(inc) 5, but only 1..4 are actually shown on the four leds
float intensity = abs((float)(i-1)/3-phase);
intensity = sqrt(intensity);
leds[i] = CRGB(255-(255*intensity),0,0);
}
FastLED.show();
return;
}
time_t t = now();
// calculate target brightnesses:
int current_hourword = hour();
if(current_hourword>12) current_hourword = current_hourword - 12; // 12 hour clock, where 12 stays 12 and 13 becomes one
if(current_hourword==0) current_hourword = 12; // 0 is also called 12
int next_hourword = hour()+1;
if(next_hourword>12) next_hourword = next_hourword - 12; // 12 hour clock, where 12 stays 12 and 13 becomes one
if(next_hourword==0) next_hourword = 12; // 0 is also called 12
for(int i=0;i<NUM_LEDS;++i) {
targetlevels[i] = 0;
}
for(int l : ledsbyword[HETIS]) { targetlevels[l] = 255; }
switch((minute()%60)/5) {
case 0:
for(int l : ledsbyword[current_hourword]) { targetlevels[l] = 255; }
for(int l : ledsbyword[UUR]) { targetlevels[l] = 255; }
break;
case 1:
for(int l : ledsbyword[VIJF]) { targetlevels[l] = 255; }
for(int l : ledsbyword[OVER]) { targetlevels[l] = 255; }
for(int l : ledsbyword[current_hourword]) { targetlevels[l] = 255; }
break;
case 2:
for(int l : ledsbyword[TIEN]) { targetlevels[l] = 255; }
for(int l : ledsbyword[OVER]) { targetlevels[l] = 255; }
for(int l : ledsbyword[current_hourword]) { targetlevels[l] = 255; }
break;
case 3:
for(int l : ledsbyword[KWART]) { targetlevels[l] = 255; }
for(int l : ledsbyword[OVER]) { targetlevels[l] = 255; }
for(int l : ledsbyword[current_hourword]) { targetlevels[l] = 255; }
break;
case 4:
for(int l : ledsbyword[TIEN]) { targetlevels[l] = 255; }
for(int l : ledsbyword[VOOR]) { targetlevels[l] = 255; }
for(int l : ledsbyword[HALF]) { targetlevels[l] = 255; }
for(int l : ledsbyword[next_hourword]) { targetlevels[l] = 255; }
break;
case 5:
for(int l : ledsbyword[VIJF]) { targetlevels[l] = 255; }
for(int l : ledsbyword[VOOR]) { targetlevels[l] = 255; }
for(int l : ledsbyword[HALF]) { targetlevels[l] = 255; }
for(int l : ledsbyword[next_hourword]) { targetlevels[l] = 255; }
break;
case 6:
for(int l : ledsbyword[HALF]) { targetlevels[l] = 255; }
for(int l : ledsbyword[next_hourword]) { targetlevels[l] = 255; }
break;
case 7:
for(int l : ledsbyword[VIJF]) { targetlevels[l] = 255; }
for(int l : ledsbyword[OVER]) { targetlevels[l] = 255; }
for(int l : ledsbyword[HALF]) { targetlevels[l] = 255; }
for(int l : ledsbyword[next_hourword]) { targetlevels[l] = 255; }
break;
case 8:
for(int l : ledsbyword[TIEN]) { targetlevels[l] = 255; }
for(int l : ledsbyword[OVER]) { targetlevels[l] = 255; }
for(int l : ledsbyword[HALF]) { targetlevels[l] = 255; }
for(int l : ledsbyword[next_hourword]) { targetlevels[l] = 255; }
break;
case 9:
for(int l : ledsbyword[KWART]) { targetlevels[l] = 255; }
for(int l : ledsbyword[VOOR]) { targetlevels[l] = 255; }
for(int l : ledsbyword[next_hourword]) { targetlevels[l] = 255; }
break;
case 10:
for(int l : ledsbyword[TIEN]) { targetlevels[l] = 255; }
for(int l : ledsbyword[VOOR]) { targetlevels[l] = 255; }
for(int l : ledsbyword[next_hourword]) { targetlevels[l] = 255; }
break;
case 11:
for(int l : ledsbyword[VIJF]) { targetlevels[l] = 255; }
for(int l : ledsbyword[VOOR]) { targetlevels[l] = 255; }
for(int l : ledsbyword[next_hourword]) { targetlevels[l] = 255; }
break;
}
// the minute leds at the bottom:
for(int i=4-(minute()%5);i<4;++i) {
targetlevels[i] = 255;
}
int speed = 4;
// move current brightness towards target brightness:
for(int i=0;i<NUM_LEDS;++i) {
if(currentlevels[i] < targetlevels[i]) {
currentlevels[i] = std::min(BRIGHTNESS,currentlevels[i]+speed);
}
if(currentlevels[i] > targetlevels[i]) {
currentlevels[i] = std::max(0,currentlevels[i]-speed);
}
// output the value to led: according to the function x^2/255 to compensate for the perceived brightness of leds which is not linear
leds[i] = CRGB(
currentlevels[i]*currentlevels[i]*R_VALUE/65025,
currentlevels[i]*currentlevels[i]*G_VALUE/65025,
currentlevels[i]*currentlevels[i]*B_VALUE/65025);
}
// Update LEDs
FastLED.show();
}
/*-------- NTP code ----------*/
time_t getNtpTime()
{
IPAddress ntpServerIP; // NTP server's ip address
while (Udp.parsePacket() > 0) ; // discard any previously received packets
Serial.println("Transmit NTP Request");
// get a random server from the pool
WiFi.hostByName(ntpServerName, ntpServerIP);
Serial.print(ntpServerName);
Serial.print(": ");
Serial.println(ntpServerIP);
sendNTPpacket(ntpServerIP);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
Serial.println("Receive NTP Response");
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
// New time in seconds since Jan 1, 1970
unsigned long newTime = secsSince1900 - 2208988800UL +
timeZone * SECS_PER_HOUR;
// Auto DST
if (autoDST) {
if (isDSTSwitchDay(day(newTime), month(newTime), year(newTime))) {
if (month(newTime) == 3 && hour(newTime) >= 2) {
newTime += SECS_PER_HOUR;
} else if (month(newTime) == 10 && hour(newTime) < 2) {
newTime += SECS_PER_HOUR;
}
} else if (isDST(day(newTime), month(newTime), year(newTime))) {
newTime += SECS_PER_HOUR;
}
}
setSyncInterval(SYNC_INTERVAL);
return newTime;
}
}
Serial.println("No NTP Response :-(");
// Retry soon
setSyncInterval(10);
return 0; // return 0 if unable to get the time
}
// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
// Check if Daylight saving time (DST) applies
// Northern Hemisphere - +1 hour between March and October
bool isDST(int d, int m, int y){
bool dst = false;
dst = (m > 3 && m < 10); // October-March
if (m == 3){
// Last sunday of March
dst = (d >= ((31 - (5 * y /4 + 4) % 7)));
}else if (m == 10){
// Last sunday of October
dst = (d < ((31 - (5 * y /4 + 1) % 7)));
}
return dst;
}
bool isDSTSwitchDay(int d, int m, int y){
bool dst = false;
if (m == 3){
// Last sunday of March
dst = (d == ((31 - (5 * y /4 + 4) % 7)));
}else if (m == 10){
// Last sunday of October
dst = (d == ((31 - (5 * y /4 + 1) % 7)));
}
return dst;
}
int readAvgAnalog(int pin, byte numReadings, int readingDelay){
int readingsTotal = 0;
for (int i = 0; i < numReadings; i++) {
readingsTotal += analogRead(pin);
delay(readingDelay);
}
return readingsTotal / numReadings;
}
void updateBrightness(){
int brightness = map(readAvgAnalog(LDR_PIN,50,2), LDR_DARK, LDR_LIGHT, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
brightness = constrain(brightness, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
// Smooth brightness change
int difference = abs(brightness - lastBrightness);
if (brightness > lastBrightness) {
brightness = lastBrightness + (1 + 30 * difference / 189 );
}else if (brightness < lastBrightness) {
brightness = lastBrightness - (1 + 30 * difference / 189 );
}
lastBrightness = brightness;
LEDS.setBrightness(brightness);
}
int timeBrightness() {
if (hour() > dayHour && hour() < nightHour) {
return MAX_BRIGHTNESS;
} else if (hour() < dayHour || hour() > nightHour) {
return MIN_BRIGHTNESS;
} else if (hour() == dayHour) {
return constrain(
map(minute(), 0, 29, MIN_BRIGHTNESS, MAX_BRIGHTNESS),
MIN_BRIGHTNESS, MAX_BRIGHTNESS);
} else if (hour() == nightHour) {
return constrain(
map(minute(), 0, 29, MAX_BRIGHTNESS, MIN_BRIGHTNESS),
MIN_BRIGHTNESS, MAX_BRIGHTNESS);
}
}