forked from samm-git/clock-controller-esp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock-controller.ino
292 lines (240 loc) · 9.75 KB
/
clock-controller.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
// This is a modified version of the clock controller for use with a
// regular old NodeMCU ESP32. No display or anything fancy.
/**
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 "credentials.h" // Wifi
// NTP server name or IP, sync interval in seconds
static const char ntpServerName[] = "us.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 usPST = { "PST", Second, Sun, Mar, 2, -420 }; // US Pacific Time
TimeChangeRule usPDT = { "PDT ", First, Sun, Nov, 2, -480 }; // US Pacific Time
Timezone ClockTZ(usPST, usPDT);
#define PIN_CH00 12 // motor controller
#define PIN_CH01 13 // motor controller
#define PIN_INIT 14 // button pin
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 200
#define IMPULSE_WAIT 800
short clockState = 0;
time_t last_ntp_sync = 0;
char console_text[256];
Preferences preferences;
long debounceDelay = 80; // the debounce eelay
long lastDebounceTime = 0; // the last time the output pin was toggled
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
*/
Serial.begin(115200);
Serial.println();
Serial.println("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*");
Serial.println("Serial Begin OK");
pinMode(PIN_CH00, OUTPUT); // init GPIO to control clock motor
pinMode(PIN_CH01, OUTPUT);
pinMode(PIN_INIT, INPUT_PULLUP); // clock reset to 12:00 button
preferences.begin("clockState", false);
clockState = preferences.getShort("clockState", false);
Serial.print("Booting... Initial state is: ");
Serial.println(clockState);
if (clockState < -721 || clockState > 721 || clockState == 0) { // if state is silly
clockState = 1; // init clock on 12:00
preferences.putShort("clockState", clockState); // and store state
}
int buttonState = digitalRead(PIN_INIT);
Serial.print("***Init state: ");
Serial.println(buttonState);
// if button is pressed while booting, state is set to 12:00 and
// button must be released when clock is displaying this value.
// State values are inverted because INPUT_PULLUP
if (buttonState == 1) {
Serial.println("Init LOW. Skipping clock reset mode.");
}
if (buttonState == 0) {
Serial.println("Init HIGH. Going into clock reset mode.");
clockState = 1;
}
while (buttonState == 0) {
// move minute hand once per second
digitalWrite(PIN_CH00, HIGH);
digitalWrite(PIN_CH01, LOW);
delay(IMPULSE_ON);
Serial.println("tick");
digitalWrite(PIN_CH00, LOW);
digitalWrite(PIN_CH01, LOW);
delay(IMPULSE_WAIT);
Serial.println("tock");
preferences.putShort("clockState", clockState);
buttonState = digitalRead(PIN_INIT);
}
// 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);
// connect to wifi
sprintf(console_text, "Connecting to wifi (%s)", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_KEY);
while (WiFi.status() != WL_CONNECTED) {
delay(10);
}
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP().toString().c_str());
Serial.println("Starting UDP...");
udp.begin(localPort);
Serial.println("Waiting for sync");
setSyncProvider(getNtpTime);
setSyncInterval(NTP_SYNC_INTERVAL); // sync with NTP
while (timeStatus() == timeNotSet) {
delay(10);
}
}
/*-------- Move minute hand and update state ----------*/
void updateState(short curr_state) {
// do some fucking shit
char buf[16], buf2[16];
Serial.print("Changing state from: ");
Serial.print(clockState);
Serial.print(" -> ");
Serial.print(formatState(abs(clockState), buf, 16));
Serial.print(" to ");
Serial.print(curr_state);
Serial.print(" -> ");
Serial.println(formatState(curr_state, buf2, 16));
// this should never happens. If clock is behind NTP to up to 5m - do nothing, just wait
if (abs(clockState) > curr_state && (abs(clockState) - curr_state) <= 5) {
Serial.print("Clock is behind NTP for ");
Serial.print((int)(abs(clockState) - curr_state));
Serial.println(" minutes. Ignoring");
return;
}
clockState++;
if (clockState >= 721) clockState = 1;
preferences.putShort("clockState", clockState);
}
// 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 advanceClock() {
digitalWrite(PIN_CH00, HIGH);
digitalWrite(PIN_CH01, LOW);
Serial.println("Tick");
delay(IMPULSE_ON);
digitalWrite(PIN_CH00, LOW);
digitalWrite(PIN_CH01, LOW);
Serial.println("Tock");
}
/*-------- Main loop ----------*/
void loop() {
int buttonState = digitalRead(PIN_INIT);
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 (curr_state != abs(clockState)) {
updateState(curr_state);
advanceClock();
delay(IMPULSE_WAIT); // cool down device :)
}
if (buttonState == 0) {
// do some button things
Serial.println("Button pressed. Advancing clock one minute.");
advanceClock();
// this is a problem, because it makes the button feel like its stuttering
// the wait is reasonable if we're doing the initial incremental tick, but
// its not reasonable for reading buttons. next version will have another button that just
// closes the relay circuit instead of going thru the microcontroller.
delay(IMPULSE_WAIT);
}
}
/*-------- 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
Serial.println("Transmit NTP Request");
// get a random server from the pool
WiFi.hostByName(ntpServerName, ntpServerIP);
Serial.print(ntpServerName);
Serial.print(" / ");
Serial.println(ntpServerIP.toString().c_str());
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];
last_ntp_sync = secsSince1900 - 2208988800UL;
return secsSince1900 - 2208988800UL;
}
}
Serial.println("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 the time:
udp.beginPacket(address, 123); //NTP requests are to port 123
udp.write(packetBuffer, NTP_PACKET_SIZE);
udp.endPacket();
}