Repository navigation
Expand file tree
/
Copy pathclock-controller.ino
More file actions
401 lines (358 loc) · 12.9 KB
/
Copy pathclock-controller.ino
File metadata and controls
401 lines (358 loc) · 12.9 KB
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
/**
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <Preferences.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <Timezone.h> // https://github.com/JChristensen/Timezone
#include <TimeLib.h> // https://playground.arduino.cc/Code/Time
#include "SSD1306Wire.h" // SSD1306 OLED: https://github.com/ThingPulse/esp8266-oled-ssd1306
#include "images.h" // Include custom images
// WIFI configuration
#define WIFI_SSID "myssid"
#define WIFI_KEY "mypass"
// NTP server name or IP, sync interval in seconds
static const char ntpServerName[] = "cz.pool.ntp.org";
#define NTP_SYNC_INTERVAL 300
// Screensaver to save OLED
#define SCREENSAVER_TIMER 600
// Time Zone (DST) settings, change to your country
TimeChangeRule CEST = { "CEST", Last, Sun, Mar, 2, 120 }; // Central European Summer Time
TimeChangeRule CET = { "CET ", Last, Sun, Oct, 3, 60 }; // Central European Standard Time
Timezone ClockTZ(CEST, CET);
// motor controller CH0 pins
#define PIN_CH00 12
#define PIN_CH01 13
// button, using toch mode
#define PIN_INIT 15
#define TOUCH_THRESHOLD 5
// display configuration
#define DISP_I2C 0x3c
#define DISP_SDA 5
#define DISP_SCL 4
unsigned int localPort = 8888;
WiFiUDP udp;
static const char hname[] = "esp-clock-controller";
/*
Slave clock configuration
I been not able to find specification for this clock, so experimentally
If impulse is > 200ms clock sometime fails to move the arrow on next switch
IMPULSE_WAIT is used in the INIT mode and if slave catching up master
*/
#define IMPULSE_ON 150
#define IMPULSE_WAIT 850
// Initialize the OLED display using Wire library
SSD1306Wire display(DISP_I2C, DISP_SDA, DISP_SCL);
short state = 0;
int8_t show_impulse = 0;
time_t last_ntp_sync = 0;
time_t oled_activate = 0;
time_t last_t = 0;
char console_text[256];
Preferences preferences;
void setup() {
/* Read slave clock state from the EEPROM using Preferences lib.
We have 12 * 60 (720) possible values
in the clock and also we need to keep last used polarity as a sign
This mean that valid values are from -721 to +721 excluding 0
*/
preferences.begin("clock", false);
state = preferences.getShort("state", -1);
log_i("Booting... Initial state is: %d", state);
if (state < -721 || state > 721 || state == 0) {
state = 1; // init clock on 12:00
preferences.putShort("state", state);
}
pinMode(PIN_CH00, OUTPUT); // init GPIO to control clock motor
pinMode(PIN_CH01, OUTPUT);
display.init();
display.flipScreenVertically();
int initState = touchRead(PIN_INIT);
// if init mode is on - state is set to 12:00 and pin must be unplugged when
// slave is displaying this value
if (initState <= TOUCH_THRESHOLD) {
log_i("Clock init mode started");
display.clear();
display.setFont(ArialMT_Plain_16);
display.drawStringMaxWidth(0, 0, 128,
"Init mode");
display.setFont(ArialMT_Plain_10);
display.drawStringMaxWidth(0, 18, 128,
"Set clock to 12:00 and unplug when ready");
display.display();
state = 1;
while (initState > TOUCH_THRESHOLD) {
if (state > 0) { // move clock arrow once a second, save last polarity
state = 1;
digitalWrite(PIN_CH00, HIGH);
digitalWrite(PIN_CH01, LOW);
} else {
state = -1;
digitalWrite(PIN_CH00, LOW);
digitalWrite(PIN_CH01, HIGH);
}
// invert polarity on next run
state = state * -1;
delay(IMPULSE_ON);
digitalWrite(PIN_CH00, LOW);
digitalWrite(PIN_CH01, LOW);
delay(IMPULSE_WAIT);
preferences.putShort("state", state);
initState = touchRead(PIN_INIT);
}
display.clear();
}
// workaround for the ESP32 SDK bug, see
// https://github.com/espressif/arduino-esp32/issues/2537#issuecomment-508558849
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
// set hostname
WiFi.setHostname(hname);
WiFi.onEvent(onWiFiEvent);
WiFi.setAutoReconnect(true);
WiFi.setSleep(false);
// connect to wifi
sprintf(console_text, "Connecting to wifi (%s)", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_KEY);
display.clear();
display.setFont(ArialMT_Plain_10);
display.drawStringMaxWidth(0, 0, 128,
console_text);
display.display();
unsigned long wifiStart = millis();
while (WiFi.status() != WL_CONNECTED && millis() - wifiStart < 30000) {
delay(10);
}
if (WiFi.status() != WL_CONNECTED) {
log_w("WiFi connect timed out, retrying in loop");
} else {
log_i("Connected, IP address: %s", WiFi.localIP().toString().c_str());
}
log_i("Starting UDP...");
udp.begin(localPort);
log_i("Waiting for sync");
display.drawStringMaxWidth(0, 10, 128,
"Waiting for NTP sync");
display.display();
setSyncProvider(getNtpTime);
setSyncInterval(NTP_SYNC_INTERVAL); // sync with NTP
unsigned long ntpStart = millis();
while (timeStatus() == timeNotSet && millis() - ntpStart < 30000) {
delay(10);
}
if (timeStatus() == timeNotSet) {
log_w("NTP sync timed out, retrying in loop");
}
oled_activate = now();
touchAttachInterrupt(PIN_INIT, buttonCallback, TOUCH_THRESHOLD);
display.clear();
}
void onWiFiEvent(WiFiEvent_t event) {
switch (event) {
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
log_i("WiFi lost, reconnecting...");
WiFi.reconnect();
break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
log_i("WiFi connected, IP: %s", WiFi.localIP().toString().c_str());
break;
default:
break;
}
}
void buttonCallback() {
// disable screensaver if active and update screen
if(now() - oled_activate > SCREENSAVER_TIMER) {
last_t = 0;
oled_activate = now();
}
}
/*-------- Move arrow and update state ----------*/
void fixState(short curr_state) {
char buf[16], buf2[16];
log_i("changing state from %d [%s] to %d [%s])", state, formatState(abs(state), buf, 16), curr_state, formatState(curr_state, buf2, 16));
// The slave motor can only advance forward. If the clock is up to 5 minutes
// ahead of NTP, do nothing and let real time catch up instead of a 12h rewind.
if (abs(state) > curr_state && (abs(state) - curr_state) <= 5) {
log_i("Clock is ahead of NTP for %d minutes, ignoring", (int)(abs(state) - curr_state));
return;
}
if (state > 0) {
state++;
if (state >= 721) state = 1;
show_impulse = 1;
digitalWrite(PIN_CH00, HIGH);
digitalWrite(PIN_CH01, LOW);
} else {
state--;
if (state <= -721) state = -1;
show_impulse = -1;
digitalWrite(PIN_CH00, LOW);
digitalWrite(PIN_CH01, HIGH);
}
updateScreen();
// invert polarity on the next run
state = state * -1;
delay(IMPULSE_ON);
digitalWrite(PIN_CH00, LOW);
digitalWrite(PIN_CH01, LOW);
show_impulse = 0;
updateScreen();
preferences.putShort("state", state);
}
// convert state variable to the human-readable format
char * formatState(int mystate, char * buf, int bufsize) {
mystate--;
snprintf(buf, bufsize, "%2d:%02d", (mystate / 60) ? mystate / 60 : 12, mystate - (mystate / 60) * 60);
return buf;
}
void updateScreen() {
char buf[16];
char wifi[32];
char timenow[16];
display.clear();
time_t utc = now();
// screensaver activated
if(utc - oled_activate > SCREENSAVER_TIMER) {
// draw random pixel
display.setPixel(random(display.getWidth()),random(display.getHeight()));
delay(25);
display.setBrightness(100);
display.display();
return;
}
display.setBrightness(255);
display.setFont(ArialMT_Plain_10);
if (WiFi.status() != WL_CONNECTED) {
strcpy(wifi, "wifi: n/a");
oled_activate = now(); // turn on screen if wifi is n/a
} else {
snprintf(wifi, sizeof(wifi), "wifi: %s", WiFi.SSID().c_str());
}
display.drawStringMaxWidth(0, 0, 128, wifi);
time_t local_t = ClockTZ.toLocal(utc);
// show NTP time
snprintf(timenow, sizeof(timenow), "%d:%02d:%02d", hour(local_t), minute(local_t), second(local_t));
display.setFont(ArialMT_Plain_16);
display.drawString(2, 25, timenow);
display.drawLine(75, 15, 75, display.getHeight());
char * statenow = formatState(abs(state), buf, 16);
// show state of the slave clock
display.drawString(85, 25, statenow);
// show DST if active
if (ClockTZ.locIsDST(local_t)) {
display.setFont(ArialMT_Plain_10);
display.drawString(2, 50, "DST");
}
// show NTP status text if we had any reply in the sync interval*1.5
if (utc - last_ntp_sync < NTP_SYNC_INTERVAL * 1.5) {
display.setFont(ArialMT_Plain_10);
display.drawString(30, 50, "NTP");
} else { // turn on screen of NTP is missing
oled_activate = now();
}
if (show_impulse) {
if (show_impulse > 0) display.drawXbm(90, 50, 16, 8, polarity_a);
if (show_impulse < 0) display.drawXbm(90, 50, 16, 8, polarity_b);
}
display.display();
}
/*-------- Main loop ----------*/
void loop() {
static unsigned long wifi_lost_at = 0;
if (WiFi.status() != WL_CONNECTED) {
if (wifi_lost_at == 0) {
wifi_lost_at = millis();
} else if (millis() - wifi_lost_at > 30000) {
log_i("WiFi stuck, forcing reconnect");
WiFi.disconnect();
delay(100);
WiFi.begin(WIFI_SSID, WIFI_KEY);
wifi_lost_at = millis();
}
} else {
wifi_lost_at = 0;
}
time_t utc = now();
time_t local_t = ClockTZ.toLocal(utc);
int hour_12 = hour(local_t);
if (hour_12 >= 12) hour_12 -= 12;
// current 12h time in minutes, starting from 1
short curr_state = hour_12 * 60 + minute(local_t) + 1;
if (timeStatus() != timeNotSet && curr_state != abs(state)) {
fixState(curr_state);
delay(IMPULSE_WAIT); // cool down device :)
}
if (utc != last_t) { // update screen
updateScreen();
last_t = now();
}
}
/*-------- 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() {
IPAddress ntpServerIP; // NTP server's ip address
while (udp.parsePacket() > 0); // discard any previously received packets
log_i("Transmit NTP Request");
// get a random server from the pool
if (!WiFi.hostByName(ntpServerName, ntpServerIP)) {
log_w("DNS lookup for %s failed", ntpServerName);
return 0;
}
log_i("%s:%s", ntpServerName, ntpServerIP.toString().c_str());
sendNTPpacket(ntpServerIP);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
log_i("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];
last_ntp_sync = secsSince1900 - 2208988800UL;
return secsSince1900 - 2208988800UL;
}
}
log_i("No NTP Response :-(");
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();
}