-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp-01.LEDWallclock.ino
508 lines (470 loc) · 18.5 KB
/
esp-01.LEDWallclock.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
/*
LED Wall Clock
ESP-01 WS2812B with 60 LED (or more) on pin 2
include NeoPixelBus libary
Features:
- get time from NTP (instead of buying a RTC-Modul)
- use free pixels as Color light (MQTT /Clock/colorRGB/set)
- Timer (seconds max 24h) (MQTT /Clock/timer/set)
- Animation-Alarm (MQTT /Clock/effect/set)
Parts:
- PCB (60mm*40mm)
- DC DC Converter (set to 5V output) (e.g. for 60 LEDs this will be okay, for more LEDs take something bigger:
https://www.amazon.de/AZDelivery-LM2596S-Netzteil-Adapter-Arduino/dp/B07DP3JX2X/ref=sr_1_4?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&crid=37NAW85CZCOXT&keywords=dcdc+wandler&qid=1574410661&sprefix=dc+dc%2Caps%2C169&sr=8-4)
- Level Shifter (e.g. https://www.amazon.de/XCSOURCE®-Logisches-Konverter-Bi-Direktional-TE291/dp/B0148BLZGE/ref=sr_1_6?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&crid=3PXRUEETCBDR9&keywords=level+shifter&qid=1574411353&sprefix=level+%2Caps%2C182&sr=8-6)
- Resistor 470Ohm
- Capacitor 1000µF/6.3V
- DC Powersupply
- ESP8266-01
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WiFiUdp.h>
#include <TimeLib.h>
#include <NeoPixelBus.h>
#include <stdlib.h>
#include <NeoPixelAnimator.h>
// Wifi Settings
const char * ssid = "YOURSSDHERE"; // your network SSID (name)
const char * pass = "YORWIFIPWDHERE"; // your network password
// MQTT Settings
const char* MQTTServer = "YOURMQTTSERVERHERE";
// Hardware Settings
const uint16_t PixelCount = 60; // best effect with n*60 LEDs, but other numbers will work too
const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266
const uint16_t TimerLimit = 600;//timer shows only if left time is < TimerLimit
///NTP Settings
IPAddress timeServer(134, 95, 192, 172); // Uni Köln
const int timeZone = 1; // Central European Time
//const int timeZone = -5; // Eastern Standard Time (USA)
//const int timeZone = -4; // Eastern Daylight Time (USA)
//const int timeZone = -8; // Pacific Standard Time (USA)
//const int timeZone = -7; // Pacific Daylight Time (USA)
// Color Settings (leave to default)
#define colorSaturation 128
NeoPixelBus<NeoGrbFeature, NeoEsp8266Uart1Ws2812xMethod > strip(PixelCount, PixelPin);
RgbColor Csecond (map(255, 0, 255, 0, colorSaturation), map(0, 0, 255, 0, colorSaturation), map(0, 0, 255, 0, colorSaturation));
RgbColor Cminute (map(0, 0, 255, 0, colorSaturation), map(255, 0, 255, 0, colorSaturation), map(0, 0, 255, 0, colorSaturation));
RgbColor Chour (map(0, 0, 255, 0, colorSaturation), map(0, 0, 255, 0, colorSaturation), map(255, 0, 255, 0, colorSaturation));
RgbColor Csegment5 (map(10, 0, 255, 0, colorSaturation), map(10, 0, 255, 0, colorSaturation), map(10, 0, 255, 0, colorSaturation));
RgbColor Csegment15 (map(200, 0, 255, 0, colorSaturation), map(200, 0, 255, 0, colorSaturation), map(200, 0, 255, 0, colorSaturation));
RgbColor Ctimer (map(100, 0, 255, 0, colorSaturation), map(0, 0, 255, 0, colorSaturation), map(0, 0, 255, 0, colorSaturation));
RgbColor black(0);
///Animation Settings (leave to default)
const uint16_t AnimCount = PixelCount / 5 * 2 + 1; // we only need enough animations for the tail and one extra
const uint16_t PixelFadeDuration = 300; // third of a second
// one second divide by the number of pixels = loop once a second
const uint16_t NextPixelMoveDuration = 1000 / PixelCount; // how fast we move through the pixels
NeoGamma<NeoGammaTableMethod> colorGamma; // for any fade animations, best to correct gamma
const uint16_t AniTime = 10; //how long will the animation be shown (seconds)
//input RGB from mqtt
int inRed = 50; //0..255
int inGreen = 50; //0..255
int inBlue = 50; //0..255
//Hand positions
time_t EndTime = 0;
int RestTime = 0;
//when the digital clock was displayed
time_t prevDisplay = 0;
// the Wifi radio's status
int status = WL_IDLE_STATUS;
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
unsigned int localPort = 8888; // local port to listen for UDP packets
//NTP
WiFiClient net;
PubSubClient client(net);
void AnimationSelect (int value);
void connect() {
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
while (!client.connect("arduinoClient")) {
delay(1000);
}
client.subscribe("/Uhr/colorRGB/set");
client.subscribe("/Uhr/effect/set");
// client.subscribe("/Uhr/timer/set");
client.subscribe("/Uhr/alarm/set");
}
void callback(char* topic, byte* payload, unsigned int length) {
payload[length] = 0;
String Payload = String((char*)payload);
// change Color
if (strcmp(topic, "/Uhr/colorRGB/set") == 0) {
inRed = Payload.substring(0, Payload.indexOf(',')).toInt();
inGreen = Payload.substring(Payload.indexOf(',') + 1, Payload.lastIndexOf(',')).toInt();
inBlue = Payload.substring(Payload.lastIndexOf(',') + 1).toInt();
}
//Start Animation
if (strcmp(topic, "/Uhr/effect/set") == 0) {
int i = Payload.substring(0, length).toInt();
AnimationSelect(i);
}
//Start Timer
if (strcmp(topic, "/Uhr/timer/set") == 0) {
int i = Payload.substring(0, length).toInt();
EndTime = i + now();
}
//Set Alarm
if (strcmp(topic, "/Uhr/alarm/set") == 0) {
tmElements_t EndTime_e;
// * Example for Payload -> "2019-11-26T19:30:00"
if (1 == 2) { //(payload[10] != 'T') {
EndTime = now(); //timer canceled
} else {
//parse simpleTimeFormat to time_t
EndTime_e.Year = CalendarYrToTm(Payload.substring(0, Payload.indexOf('-')).toInt());
EndTime_e.Month = Payload.substring(Payload.indexOf('-') + 1, Payload.lastIndexOf('-')).toInt();
EndTime_e.Day = Payload.substring(Payload.lastIndexOf('-') + 1, Payload.indexOf('T')).toInt();
EndTime_e.Hour = Payload.substring(Payload.indexOf('T') + 1, Payload.indexOf(':')).toInt();
EndTime_e.Minute = Payload.substring(Payload.indexOf(':') + 1, Payload.lastIndexOf(':')).toInt();
EndTime_e.Second = Payload.substring(Payload.lastIndexOf(':') + 1,Payload.lastIndexOf(':') + 3).toInt();
EndTime = makeTime(EndTime_e);
}
}
}
///Animation
//taken from the examples at NeoPixelBus
struct MyAnimationState
{
RgbColor StartingColor;
RgbColor EndingColor;
uint16_t IndexPixel; // which pixel this animation is effecting
};
NeoPixelAnimator animations(AnimCount); // NeoPixel animation management object
MyAnimationState animationState[AnimCount];
uint16_t frontPixel = 0; // the front of the loop
RgbColor frontColor; // the color at the front of the loop
///
NeoPixelAnimator animations02(PixelCount); // NeoPixel animation management object
MyAnimationState animationState02[PixelCount];
///
const uint8_t AnimationChannels = 1; // we only need one as all the pixels are animated at once
NeoPixelAnimator animations03(AnimationChannels); // NeoPixel animation management object
MyAnimationState animationState03[AnimationChannels];
boolean fadeToColor = true; // general purpose variable used to store effect state
void SetRandomSeed(void)
{
uint32_t seed;
// random works best with a seed that can use 31 bits
// analogRead on a unconnected pin tends toward less than four bits
seed = analogRead(0);
delay(1);
for (int shifts = 3; shifts < 31; shifts += 3)
{
seed ^= analogRead(0) << shifts;
delay(1);
}
randomSeed(seed);
}
void FadeOutAnimUpdate(const AnimationParam & param)
{
// this gets called for each animation on every time step
// progress will start at 0.0 and end at 1.0
// we use the blend function on the RgbColor to mix
// color based on the progress given to us in the animation
RgbColor updatedColor = RgbColor::LinearBlend(
animationState[param.index].StartingColor,
animationState[param.index].EndingColor,
param.progress);
// apply the color to the strip
strip.SetPixelColor(animationState[param.index].IndexPixel,
colorGamma.Correct(updatedColor));
}
void LoopAnimUpdate(const AnimationParam & param)
{
// wait for this animation to complete,
// we are using it as a timer of sorts
if (param.state == AnimationState_Completed)
{
// done, time to restart this position tracking animation/timer
animations.RestartAnimation(param.index);
// pick the next pixel inline to start animating
//
frontPixel = (frontPixel + 1) % PixelCount; // increment and wrap
if (frontPixel == 0)
{
// we looped, lets pick a new front color
frontColor = HslColor(random(360) / 360.0f, 1.0f, 0.25f);
}
uint16_t indexAnim;
// do we have an animation available to use to animate the next front pixel?
// if you see skipping, then either you are going to fast or need to increase
// the number of animation channels
if (animations.NextAvailableAnimation(&indexAnim, 1))
{
animationState[indexAnim].StartingColor = frontColor;
animationState[indexAnim].EndingColor = RgbColor(0, 0, 0);
animationState[indexAnim].IndexPixel = frontPixel;
animations.StartAnimation(indexAnim, PixelFadeDuration, FadeOutAnimUpdate);
}
}
}
void BlendAnimUpdate02(const AnimationParam & param)
{
// this gets called for each animation on every time step
// progress will start at 0.0 and end at 1.0
// we use the blend function on the RgbColor to mix
// color based on the progress given to us in the animation
RgbColor updatedColor = RgbColor::LinearBlend(
animationState02[param.index].StartingColor,
animationState02[param.index].EndingColor,
param.progress);
// apply the color to the strip
strip.SetPixelColor(param.index, updatedColor);
}
void BlendAnimUpdate03(const AnimationParam & param)
{
// this gets called for each animation on every time step
// progress will start at 0.0 and end at 1.0
// we use the blend function on the RgbColor to mix
// color based on the progress given to us in the animation
RgbColor updatedColor = RgbColor::LinearBlend(
animationState03[param.index].StartingColor,
animationState03[param.index].EndingColor,
param.progress);
// apply the color to the strip
for (uint16_t pixel = 0; pixel < PixelCount; pixel++)
{
strip.SetPixelColor(pixel, updatedColor);
}
}
void PickRandom(float luminance)
{
// pick random count of pixels to animate
uint16_t count = random(PixelCount);
while (count > 0)
{
// pick a random pixel
uint16_t pixel = random(PixelCount);
// pick random time and random color
// we use HslColor object as it allows us to easily pick a color
// with the same saturation and luminance
uint16_t time = random(100, 400);
animationState02[pixel].StartingColor = strip.GetPixelColor(pixel);
animationState02[pixel].EndingColor = HslColor(random(360) / 360.0f, 1.0f, luminance);
animations02.StartAnimation(pixel, time, BlendAnimUpdate02);
count--;
}
}
void FadeInFadeOutRinseRepeat(float luminance)
{
if (fadeToColor)
{
// Fade upto a random color
// we use HslColor object as it allows us to easily pick a hue
// with the same saturation and luminance so the colors picked
// will have similiar overall brightness
RgbColor target = HslColor(random(360) / 360.0f, 1.0f, luminance);
uint16_t time = random(800, 2000);
animationState03[0].StartingColor = strip.GetPixelColor(0);
animationState03[0].EndingColor = target;
animations03.StartAnimation(0, time, BlendAnimUpdate03);
}
else
{
// fade to black
uint16_t time = random(600, 700);
animationState03[0].StartingColor = strip.GetPixelColor(0);
animationState03[0].EndingColor = RgbColor(0);
animations03.StartAnimation(0, time, BlendAnimUpdate03);
}
// toggle to the next effect state
fadeToColor = !fadeToColor;
}
void AnimationSelect (int value) {
switch (value) {
case 0 :
break;
case 1 :
client.publish("/Uhr/effect", "1");
strip.ClearTo(black);
animations.StartAnimation(0, NextPixelMoveDuration, LoopAnimUpdate);
int i = now();
while (i + AniTime > now()) {
animations.UpdateAnimations();
Serial.print(".");
strip.Show();
}
break;
/* Don't know what is wrong here, any help welcome
case 2 :
client.publish("/Uhr/effect", "2");
strip.ClearTo(black);
PickRandom(0.2f); // 0.0 = black, 0.25 is normal, 0.5 is bright
int i = now();
while (i + AniTime > now()) {
animations02.UpdateAnimations();
Serial.print(".");
strip.Show();
}
break;
case 3 :
client.publish("/Uhr/effect", "3");
strip.ClearTo(black);
FadeInFadeOutRinseRepeat(0.2f); // 0.0 = black, 0.25 is normal, 0.5 is bright
int i = now();
while (i + AniTime > now()) {
animations03.UpdateAnimations();
Serial.print(".");
strip.Show();
}
break;
*/ }
client.publish("/Uhr/effect", "0", true);
client.publish("/Uhr/effect/set", "0", true);
}
void setup()
{
strip.Begin();
strip.Show();
SetRandomSeed();
// attempt to connect to WiFi network
// We start by connecting to a WiFi network
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Udp.begin(localPort);
setSyncProvider(getNtpTime);
setSyncInterval(3600);
client.setServer(MQTTServer, 1883);
client.setCallback(callback);
connect();
}
void loop()
{
client.loop();
if (!client.connected()) {
connect();
}
if (now() != prevDisplay) { //update the display only if time has changed
prevDisplay = now();
digitalClockDisplay();
}
}
void ClockFrame(int Red, int Green, int Blue) {
RgbColor Cfill(Red, Green, Blue);
//delete hands and write all leds in selected color
strip.ClearTo(Cfill);
//publish to mqtt as feedback
char restate[12];
String i = (String(Red) + "," + String(Green) + "," + String(Blue));
i.toCharArray(restate, 12);
client.publish("/Uhr/colorRGB", restate);
}
int ClockSegments(RgbColor Cvalue5, RgbColor Cvalue15) {
//show 5-minute segments
for (int i = 0; i <= 11; i++) {
strip.SetPixelColor(i * (PixelCount / 60) * 5, Cvalue5);
}
//show 15-minute segments
for (int i = 0; i <= 3; i++) {
strip.SetPixelColor(i * (PixelCount / 60) * 15, Cvalue15);
}
return 1;
}
void ClockHands(RgbColor CvalueH, RgbColor CvalueM, RgbColor CvalueS) {
//show hour hand
int i = (hourFormat12() * 5) + (int)(minute() / 12 + 0.5);
if (i>59) i-=60;
strip.SetPixelColor(map(i, 0, 59, 0, PixelCount - 1), CvalueH);
//show minute hand
strip.SetPixelColor(map(minute(), 0, 59, 0, PixelCount - 1), CvalueM);
//show second hand
strip.SetPixelColor(map(second(), 0, 59, 0, PixelCount - 1), CvalueS);
}
void ClockTimer(time_t value, RgbColor Cvalue) {
if (value < now()) {
RestTime = 0;
} else {
RestTime = (int)value - (int)now();
}
if (RestTime > 0) {
char restate[24];
sprintf(restate, "%d", RestTime);
client.publish("/Uhr/timer", restate,false);
sprintf(restate, "%04d-%02d-%02dT%02d:%02d:%02d", year(value), month(value), day(value), hour(value), minute(value), second(value));
// client.publish("/Uhr/alarm", restate,false);
if ((RestTime >= 3600) && (RestTime <= TimerLimit)) {
// >1 hour
for (int i = 0; i < map(RestTime, 0, 24 * 3600, 0, PixelCount); i ++) {
strip.SetPixelColor(PixelCount - i, Cvalue);
}
}
if ((RestTime > 60) && (RestTime < 3600) && (RestTime <= TimerLimit)) {
// >1 Minute
for (int i = 0; i <= map(RestTime, 0, 3600, 0, PixelCount); i++) {
strip.SetPixelColor(i, Ctimer);
}
}
if ((RestTime <= 60) && (RestTime <= TimerLimit)) {
//< 1 Minute
for (int i = 0; i <= map(RestTime, 0, 60, 0, PixelCount); i++) {
strip.SetPixelColor(i, Ctimer);
}
}
}
}
void digitalClockDisplay() {
//Clock Frame alias Light
ClockFrame(inRed, inGreen, inBlue);
//Clock Segments, only shown at a minimum light level
if ((inRed > 10) || (inGreen > 10) || (inBlue > 10)) {
ClockSegments(Csegment5, Csegment15);
}
//Timer
ClockTimer(EndTime, Ctimer);
//Clock Hands, only shown if Light is switched on
if (inRed + inGreen + inBlue > 0) {
ClockHands(Chour, Cminute, Csecond);
}
strip.Show();
connect();
}
/*-------- NTP code ----------*/
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
time_t getNtpTime() {
while (Udp.parsePacket() > 0) ; // discard any previously received packets
sendNTPpacket(timeServer);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
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];
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
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();
}
/*-------- NTP code ----------*/