This repository was archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlarm_ESP_Complex.ino
367 lines (278 loc) · 10.9 KB
/
Alarm_ESP_Complex.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
/****************************************************************************************************************************
Alarm_ESP_Complex.ino
For ESP8266, ESP32
DS323x_Generic Arduino library for DS3231/DS3232 Extremely Accurate I2C-Integrated RTC/TCXO/Crystal.
Based on and modified from Hideaki Tai's DS323x Library (https://github.com/hideakitai/DS323x)
Built by Khoi Hoang https://github.com/khoih-prog/DS323x_Generic
Licensed under MIT license
*****************************************************************************************************************************/
#include "defines.h"
#include <Timezone_Generic.h> // https://github.com/khoih-prog/Timezone_Generic
#include <DS323x_Generic.h> // https://github.com/khoih-prog/DS323x_Generic
DS323x rtc;
//////////////////////////////////////////
// US Eastern Time Zone (New York, Detroit)
TimeChangeRule myDST = {"EDT", Second, Sun, Mar, 2, -240}; //Daylight time = UTC - 4 hours
TimeChangeRule mySTD = {"EST", First, Sun, Nov, 2, -300}; //Standard time = UTC - 5 hours
Timezone *myTZ;
TimeChangeRule *tcr; //pointer to the time change rule, use to get TZ abbrev
//////////////////////////////////////////
char timeServer[] = "time.nist.gov"; // NTP server
unsigned int localPort = 2390; // local port to listen for UDP packets
const int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message
const int UDP_TIMEOUT = 2000; // timeout in miliseconds to wait for an UDP packet to arrive
byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
// send an NTP request to the time server at the given address
void sendNTPpacket(char *ntpSrv)
{
// 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(ntpSrv, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
bool gotCurrentTime = false;
void getNTPTime(void)
{
// Just get the correct time once
if (!gotCurrentTime)
{
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
if (Udp.parsePacket())
{
Serial.println(F("Packet received"));
// We've received a packet, read the data from it
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print(F("Seconds since Jan 1 1900 = "));
Serial.println(secsSince1900);
// now convert NTP time into everyday time:
Serial.print(F("Unix time = "));
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears;
// print Unix time:
Serial.println(epoch);
// Get the time_t from epoch
time_t epoch_t = epoch;
// set the system time to UTC
// warning: assumes that compileTime() returns US EDT
// adjust the following line accordingly if you're in another time zone
setTime(epoch_t);
timeval tv = { epoch_t, 0 };
settimeofday(&tv, nullptr);
// Update RTC
// Can use either one of these functions
// 1) DateTime(tmElements_t). Must create tmElements_t if not present
//tmElements_t tm;
//breakTime(epoch_t, tm);
//rtc.now( DateTime(tm) );
// 2) DateTime(year, month, day, hour, min, sec)
//rtc.now( DateTime(year(epoch_t), month(epoch_t), day(epoch_t), hour(epoch_t), minute(epoch_t), second(epoch_t) ) );
// 3) DateTime (time_t)
//rtc.now( DateTime(epoch_t) );
// 4) DateTime(unsigned long epoch). The best and easiest way
rtc.now( DateTime((uint32_t) epoch) );
// print the hour, minute and second:
Serial.print(F("The UTC time is ")); // UTC is the time at Greenwich Meridian (GMT)
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
Serial.print(':');
if (((epoch % 3600) / 60) < 10)
{
// In the first 10 minutes of each hour, we'll want a leading '0'
Serial.print('0');
}
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
Serial.print(':');
if ((epoch % 60) < 10)
{
// In the first 10 seconds of each minute, we'll want a leading '0'
Serial.print('0');
}
Serial.println(epoch % 60); // print the second
gotCurrentTime = true;
}
else
{
// wait ten seconds before asking for the time again
delay(10000);
}
}
}
//////////////////////////////////////////
// format and print a time_t value, with a time zone appended.
void printDateTime(time_t t, const char *tz)
{
char buf[32];
char m[4]; // temporary storage for month string (DateStrings.cpp uses shared buffer)
strcpy(m, monthShortStr(month(t)));
sprintf(buf, "%.2d:%.2d:%.2d %s %.2d %s %d %s",
hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t), tz);
Serial.println(buf);
}
//////////////////////////////////////////
void set_RTC_Alarm1(DateTime& alarmTime)
{
// set alarm1
rtc.format(DS323x::AlarmSel::A1, DS323x::Format::H12);
rtc.dydt(DS323x::AlarmSel::A1, DS323x::DYDT::DATE);
rtc.ampm(DS323x::AlarmSel::A1, DS323x::AMPM::RTC_PM);
//rtc.ampm(DS323x::AlarmSel::A1, DS323x::AMPM::AM);
rtc.weekday(DS323x::AlarmSel::A1, 1);
rtc.hour(DS323x::AlarmSel::A1, alarmTime.hour());
rtc.minute(DS323x::AlarmSel::A1, alarmTime.minute());
rtc.second(DS323x::AlarmSel::A1, alarmTime.second());
rtc.rate(DS323x::A1Rate::MATCH_SECOND);
//rtc.rate(DS323x::A1Rate::MATCH_SECOND_MINUTE_HOUR);
Serial.print(F("Alarm 1 is set to : "));
Serial.println(rtc.alarm(DS323x::AlarmSel::A1).timestamp(DateTime::TIMESTAMP_TIME));
Serial.print(F("Alarm 1 alarm rate : "));
Serial.println((uint8_t)rtc.rateA1());
// alarm flags must be cleard to get next alarm
if (rtc.hasAlarmed(DS323x::AlarmSel::A1))
rtc.clearAlarm(DS323x::AlarmSel::A1);
// enable alarm1
rtc.enableAlarm1(true);
}
void set_RTC_Alarm2(DateTime& alarmTime)
{
// set alarm2
rtc.format(DS323x::AlarmSel::A2, DS323x::Format::H24);
rtc.dydt(DS323x::AlarmSel::A2, DS323x::DYDT::DAY);
rtc.ampm(DS323x::AlarmSel::A2, DS323x::AMPM::RTC_PM);
//rtc.ampm(DS323x::AlarmSel::A2, DS323x::AMPM::PM);
rtc.day(DS323x::AlarmSel::A2, alarmTime.day());
rtc.hour(DS323x::AlarmSel::A2, alarmTime.hour());
rtc.minute(DS323x::AlarmSel::A2, alarmTime.minute());
//rtc.rate(DS323x::A2Rate::MATCH_MINUTE);
rtc.rate(DS323x::A2Rate::MATCH_MINUTE_HOUR);
Serial.print(F("Alarm 2 is set to : "));
Serial.println(rtc.alarm(DS323x::AlarmSel::A2).timestamp(DateTime::TIMESTAMP_TIME));
Serial.print(F("Alarm 2 alarm rate : "));
Serial.println((uint8_t)rtc.rateA2());
// alarm flags must be cleard to get next alarm
if (rtc.hasAlarmed(DS323x::AlarmSel::A2))
rtc.clearAlarm(DS323x::AlarmSel::A2);
// enable alarm2
rtc.enableAlarm2(true);
}
void setup()
{
Serial.begin(115200);
while (!Serial);
delay(200);
Serial.print(F("\nStart Alarm_ESP_Complex on "));
Serial.println(ARDUINO_BOARD);
Serial.println(TIMEZONE_GENERIC_VERSION);
Serial.println(DS323X_GENERIC_VERSION);
#if defined(PIN_WIRE_SDA)
// Arduino core, ESP8266, Adafruit
TZ_LOGWARN(F("Default DS323X pinout:"));
TZ_LOGWARN1(F("SDA:"), PIN_WIRE_SDA);
TZ_LOGWARN1(F("SCL:"), PIN_WIRE_SCL);
#elif defined(PIN_WIRE0_SDA)
// arduino-pico core
TZ_LOGWARN(F("Default DS323X pinout:"));
TZ_LOGWARN1(F("SDA:"), PIN_WIRE0_SDA);
TZ_LOGWARN1(F("SCL:"), PIN_WIRE0_SCL);
#elif defined(ESP32)
// ESP32
TZ_LOGWARN(F("Default DS323X pinout:"));
TZ_LOGWARN1(F("SDA:"), SDA);
TZ_LOGWARN1(F("SCL:"), SCL);
#endif
Wire.begin();
Serial.print(F("Connecting to "));
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(250);
}
// you're connected now, so print out the data
Serial.print(F("\nYou're connected to the network, IP = "));
Serial.println(WiFi.localIP());
myTZ = new Timezone(myDST, mySTD);
// Comment out for first time run to write TZ rule to filesystem
myTZ->writeRules(0); // write rules to address/offset 0
Udp.begin(localPort);
rtc.attach(Wire);
}
bool setAlarmDone = false;
void setAlarm(void)
{
// RTC is using UTC, so everything must use UTC, not local time
// Valid when RTC is already correct
DateTime currentTime = rtc.now();
time_t utc = currentTime.get_time_t();
// Alarm 1 time is 30s from now
DateTime alarm1 = DateTime(utc + 30);
// Alarm 2 time is start of next minute from now
DateTime alarm2 = DateTime(utc + 60);
set_RTC_Alarm1(alarm1);
set_RTC_Alarm2(alarm2);
setAlarmDone = true;
}
time_t timeNowUTC;
struct tm * timeInfo;
void loop()
{
// Get time from NTP once, then update RTC
// You certainly can make NTP check every hour/day to update RTC ti have better accuracy
getNTPTime();
if (!setAlarmDone && gotCurrentTime)
{
setAlarm();
}
static uint32_t prev_ms = millis();
if (millis() > prev_ms + 1000)
{
prev_ms = millis();
DateTime now = rtc.now();
Serial.println("============================");
time_t utc = now.get_time_t();
time_t local = myTZ->toLocal(utc, &tcr);
printDateTime(utc, "UTC");
printDateTime(local, tcr -> abbrev);
timeNowUTC = time(nullptr);
timeInfo = localtime(&timeNowUTC);
Serial.print("System Time UTC: ");
Serial.println(asctime(timeInfo));
// alarm flags must be cleard to get next alarm
if (rtc.hasAlarmed(DS323x::AlarmSel::A1))
{
Serial.println(F("Alarm 1 activated"));
rtc.clearAlarm(DS323x::AlarmSel::A1);
}
if (rtc.hasAlarmed(DS323x::AlarmSel::A2))
{
Serial.println(F("Alarm 2 activated"));
rtc.clearAlarm(DS323x::AlarmSel::A2);
}
}
}