NodeMCU ESP8266 ESP-12E v1.0 – Clock

By telleropnul, June 11, 2024

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
#include <NeoPixelBus.h>
#include <ESP8266WiFi.h>
#include <time.h>
 
const uint16_t PIXELS = 60;
const uint8_t PIN = 99;    //Ignored for ESP8266.  Uses RX  GPIO3
 
NeoPixelBus<NeoGrbFeature, NeoWs2812xMethod> strip(PIXELS, PIN);
 
const char* ssid = "........";
const char* password = "........";
 
//https://www.iana.org/time-zones
const char *TZstr = "AEST";
 
RgbColor red(100, 0, 0);
RgbColor green(0, 100, 0);
RgbColor blue(0, 0, 100);
RgbColor black(0);
RgbColor nightorange(40, 20, 0);
RgbColor nightred(40, 0, 0);
RgbColor nightgreen(0, 40, 0);
RgbColor nightblue(0, 0, 40);
 
void setup() {
  Serial.begin(115200);
  while (!Serial); // wait for serial attach
  Serial.flush();
 
  strip.Begin();
  strip.SetPixelColor(0, red);
  strip.SetPixelColor(20, blue);
  strip.SetPixelColor(40, green);
 
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      delay(1000);
  }
  //configTime(TZstr, "pool.ntp.org");
  configTime(10*3600, 0, "pool.ntp.org"); // AEST = UTC+10, no DST
   while (!time(nullptr)) {
     delay(1000);
  }
}
 
void loop() {
  time_t now = time(nullptr);
  struct tm *ptm = localtime(&now);
  Serial.print(ptm->tm_hour);
  Serial.print(":");
  Serial.print(ptm->tm_min);
  Serial.print(":");
  Serial.print(ptm->tm_sec);
  Serial.println();
  unsigned char hourpos = ( (ptm->tm_hour) %12) * 5;
  // To let the hour hand creep to the next hour as the minutes progress,
  // for every 12 minutes offset the hourpos by 1 (= 1 LED).
  if (ptm->tm_min >= 12) hourpos=hourpos+1;
  if (ptm->tm_min >= 24) hourpos=hourpos+1;
  if (ptm->tm_min >= 36) hourpos=hourpos+1;
  if (ptm->tm_min >= 48) hourpos=hourpos+1;
  if (ptm->tm_hour < 18) {
    //Daytime
    strip.ClearTo(black);
    strip.SetPixelColor( (59+hourpos) %60 %60, green );
    strip.SetPixelColor( hourpos %60, green );
    strip.SetPixelColor( (hourpos+1) %60, green );
    strip.SetPixelColor(ptm->tm_min%60, blue);
    strip.SetPixelColor(ptm->tm_sec, red);
  } else {
    //Nighttime
    strip.ClearTo(nightorange);
    strip.SetPixelColor( (59+hourpos) %60 %60, nightgreen );
    strip.SetPixelColor( hourpos %60, nightgreen );
    strip.SetPixelColor( (hourpos+1) %60, nightgreen );
    strip.SetPixelColor(ptm->tm_min%60, nightblue );
    //strip.SetPixelColor(ptm->tm_sec, nightred);
  }
  strip.Show(); 
  delay(500);
}