-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code
128 lines (107 loc) · 3.04 KB
/
Code
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
#include <WiFi.h>
#include <time.h>
#include <Adafruit_NeoPixel.h>
#define PixelPin 26
int minutenpixel = 1;
int stundenpixel = 2;
int helligkeit = 150;
const char* ssid = "DEINWLANNAME";
const char* password = "DEINWLANPASSWORT";
const char* NTP_SERVER = "ch.pool.ntp.org";
const char* TZ_INFO = "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00"; // enter your time zone (https://remotemonitoringsystems.ca/time-zone-abbreviations.php)
tm timeinfo;
time_t now;
long unsigned lastNTPtime;
unsigned long lastEntryTime;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(72, PixelPin, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(115200);
Serial.println("\n\nNTP Time Test\n");
WiFi.begin(ssid, password);
int counter = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(200);
if (++counter > 100) ESP.restart();
Serial.print ( "." );
}
Serial.println("\n\nWiFi connected\n\n");
configTime(0, 0, NTP_SERVER);
// See https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv for Timezone codes for your region
setenv("TZ", TZ_INFO, 1);
if (getNTPtime(10)) { // wait up to 10sec to sync
} else {
Serial.println("Time not set");
ESP.restart();
}
showTime(timeinfo);
lastNTPtime = time(&now);
lastEntryTime = millis();
}
void loop() {
// getTimeReducedTraffic(3600);
getNTPtime(10);
showTime(timeinfo);
pixels.begin();
pixels.clear();
pixels.setPixelColor(minutenpixel, pixels.Color(helligkeit,helligkeit,helligkeit));
pixels.setPixelColor(stundenpixel, pixels.Color(helligkeit,helligkeit,helligkeit));
pixels.show();
delay(1000);
}
bool getNTPtime(int sec) {
{
uint32_t start = millis();
do {
time(&now);
localtime_r(&now, &timeinfo);
Serial.print(".");
delay(10);
} while (((millis() - start) <= (1000 * sec)) && (timeinfo.tm_year < (2016 - 1900)));
if (timeinfo.tm_year <= (2016 - 1900)) return false; // the NTP call was not successful
Serial.print("now "); Serial.println(now);
char time_output[30];
strftime(time_output, 30, "%a %d-%m-%y %T", localtime(&now));
Serial.println(time_output);
Serial.println();
}
return true;
}
void showTime(tm localTime) {
Serial.print(localTime.tm_mday);
Serial.print('/');
Serial.print(localTime.tm_mon + 1);
Serial.print('/');
Serial.print(localTime.tm_year - 100);
Serial.print('-');
Serial.print(localTime.tm_hour);
Serial.print(':');
Serial.print(localTime.tm_min);
Serial.print(':');
Serial.print(localTime.tm_sec);
Serial.print(" Day of Week ");
if (localTime.tm_hour >= 12){
stundenpixel = localTime.tm_hour + 48;
}
if (localTime.tm_hour < 12){
stundenpixel = localTime.tm_hour + 60;
}
if (localTime.tm_min == 0){
minutenpixel = 60;
}
else{
minutenpixel = localTime.tm_min;
}
if (localTime.tm_hour >= 7){
if (localTime.tm_hour <= 18){
helligkeit = 150;
}
else {
helligkeit = 50;
}
}
else {
helligkeit = 50;
}
if (localTime.tm_wday == 0) Serial.println(7);
else Serial.println(localTime.tm_wday);
}