-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDS3231_ntpsync.ino
223 lines (191 loc) · 6.85 KB
/
DS3231_ntpsync.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
// ---------------------------------------------------------
// NTP SETUP for DS3231 modules and ESP8266
// 23/03/2019 by M.Campinoti - IU5HKU
// Simple utility to automatically set a DS3231 clock module
// ---------------------------------------------------------
//
// How to connect the two boards:
// ---------------------------------------------------------
// DS3231 VCC GND SDA SCL
// ESP8266 5+ GND D2 D1
// ---------------------------------------------------------
//
// Hardware Requirements
// ---------------------
// This firmware must be run on an ESP8266 compatible board
// testde on Wemos D1 Mini Lite
//
// Required Libraries
// ------------------
// DS3231 (Library by Eric Ayars v1.0.2)
// Time (Library Manager v1.5)
// Wire (Arduino Standard Library v1.0)
// NTPtimeESP (https://github.com/SensorsIot/NTPtimeESP)
// ESP8266WiFi (v1.0)
//
// License
// -------
// 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 <TimeLib.h>
#include <NTPtimeESP.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <DS3231.h>
#include <Wire.h>
DS3231 Clock;
const char* ssid = "YourAPName"; //SSID of your Wifi network: Change this
const char* password = "YourWiFiPassword"; //Wi-Fi Password: Change this
//**** NTP Server to use
const char* NTP_Server = "ntp1.inrim.it"; //italian national institute for measures
//**** Your time zone UTC related (floating point number)
#define TIME_ZONE 1.0f
NTPtime NTPch(NTP_Server);
strDateTime dateTime;
//****************************************************
//* SYNCRONIZE SYSTEM TIME with NTP SERVERS
//****************************************************
#define SEND_INTV 10
#define RECV_TIMEOUT 10
// --------------------------------------
// epochUnixNTP set the UNIX time
// number of seconds sice Jan 1 1970
// --------------------------------------
time_t epochUnixNTP()
{
Serial.println(">>>> TimeSync function called <<<<");
//**** BIG ISSUE: in case of poor connection, we risk to remain in this loop forever
NTPch.setSendInterval(SEND_INTV);
NTPch.setRecvTimeout(RECV_TIMEOUT);
do
{
dateTime = NTPch.getNTPtime(TIME_ZONE, 1);
delay(1);
}
while(!dateTime.valid);
Clock.setSecond(dateTime.second);
Clock.setMinute(dateTime.minute);
Clock.setClockMode(false); // set to 24h
//setClockMode(true); // set to 12h
Clock.setHour(dateTime.hour);
Clock.setYear(dateTime.year-2000);
Clock.setMonth(dateTime.month);
Clock.setDate(dateTime.day);
Clock.setDoW(dateTime.dayofWeek);
Serial.println(">>>> DS3231 sync'ed with NTP time <<<<");
Serial.print("NTP time : ");
NTPch.printDateTime(dateTime);
//set system time
setTime(dateTime.hour,dateTime.minute,dateTime.second,dateTime.day,dateTime.month,dateTime.year);
return 0;
}
//***********************************************************************
//* SSIDCONNECT
//***********************************************************************
void ssidConnect()
{
Serial.println(ssid);
Serial.println(password);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(700);
Serial.print(".");
}
Serial.println();
Serial.print(F("Connected to "));
Serial.println(ssid);
Serial.print(F("IP address: "));
Serial.println(WiFi.localIP());
}
//***********************************************************************
//* SETUP
//***********************************************************************
void setup()
{
bool i2c_found;
Serial.begin(9600); while (!Serial);
Serial.println("COM setup successful.");
delay(10);
WiFi.mode(WIFI_STA);
// connect to WiFi network
ssidConnect();
// Start the I2C interface
Wire.begin();
// Set time sync provider
setSyncProvider(epochUnixNTP); //set function to call when sync required
setSyncInterval(60); //every 60 seconds
//In the same moment you retrieve the time from the library, that function will look up whether it is time to sync the time.
//If you don't retrieve the time or if you do, but the syninterval has not yet passed, nothing will happen.
// display the time
Serial.print("DS3231 time: ");
PrintDS3231Time();
}
//***********************************************************************
//* Print DS3231 time
//***********************************************************************
bool Century=false;
bool h12;
bool PM;
void PrintDS3231Time(){
Serial.print((Clock.getYear()+2000), DEC);
Serial.print('-');
// then the month
Serial.print(Clock.getMonth(Century), DEC);
Serial.print('-');
// then the date
Serial.print(Clock.getDate(), DEC);
Serial.print('-');
// and the day of the week
Serial.print(Clock.getDoW(), DEC);
Serial.print(' ');
// Finally the hour, minute, and second
Serial.print(Clock.getHour(h12, PM), DEC);
Serial.print("H ");
Serial.print(Clock.getMinute(), DEC);
Serial.print("M ");
Serial.print(Clock.getSecond(), DEC);
Serial.print("S ");
// Add AM/PM indicator
if (h12) {
if (PM) {
Serial.print(" PM ");
} else {
Serial.print(" AM ");
}
} else {
Serial.print(" (24h) ");
}
// Display the temperature
Serial.print("T=");
Serial.print(Clock.getTemperature(), 2);
// Tell whether the time is (likely to be) valid
if (Clock.oscillatorCheck()) {
Serial.print(" O+");
} else {
Serial.print(" O-");
}
Serial.print('\n');
Serial.print('\n');
}
//***********************************************************************
//* LOOP
//***********************************************************************
void loop() {
// nothing todo here
}