-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsntp.ts
261 lines (215 loc) · 6.59 KB
/
sntp.ts
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
/*******************************************************************************
* Functions for Simple Network Time Protocol.
*
* Company: Cytron Technologies Sdn Bhd
* Website: http://www.cytron.io
* Email: [email protected]
*******************************************************************************/
// NTP server.
const NTP_SERVER_URL = "pool.ntp.org"
namespace esp8266 {
// Flag to indicate whether the SNTP time is initialized.
let internetTimeInitialized = false
// Flag to indicate whether the SNTP time is updated successfully.
let internetTimeUpdated = false
// Time and date.
let year = 0, month = 0, day = 0, weekday = 0, hour = 0, minute = 0, second = 0
/**
* Return the year.
*/
//% subcategory="Internet Time"
//% weight=30
//% blockGap=8
//% blockId=esp8266_get_year
//% block="year"
export function getYear(): number {
return year
}
/**
* Return the month.
*/
//% subcategory="Internet Time"
//% weight=29
//% blockGap=8
//% blockId=esp8266_get_month
//% block="month"
export function getMonth(): number {
return month
}
/**
* Return the day.
*/
//% subcategory="Internet Time"
//% weight=28
//% blockGap=8
//% blockId=esp8266_get_day
//% block="day"
export function getDay(): number {
return day
}
/**
* Return the day of week.
*/
//% subcategory="Internet Time"
//% weight=27
//% blockGap=8
//% blockId=esp8266_get_weekday
//% block="day of week"
export function getWeekday(): number {
return weekday
}
/**
* Return the hour.
*/
//% subcategory="Internet Time"
//% weight=26
//% blockGap=8
//% blockId=esp8266_get_hour
//% block="hour"
export function getHour(): number {
return hour
}
/**
* Return the minute.
*/
//% subcategory="Internet Time"
//% weight=25
//% blockGap=8
//% blockId=esp8266_get_minute
//% block="minute"
export function getMinute(): number {
return minute
}
/**
* Return the second.
*/
//% subcategory="Internet Time"
//% weight=24
//% blockGap=40
//% blockId=esp8266_get_second
//% block="second"
export function getSecond(): number {
return second
}
/**
* Return true if the internet time is initialzed successfully.
*/
//% subcategory="Internet Time"
//% weight=23
//% blockGap=8
//% blockId=esp8266_is_internet_time_initialized
//% block="internet time initialized"
export function isInternetTimeInitialized(): boolean {
return internetTimeInitialized
}
/**
* Initialize the internet time.
* @param timezone Timezone. eg: 8
*/
//% subcategory="Internet Time"
//% weight=22
//% blockGap=40
//% blockId=esp8266_init_internet_time
//% block="initialize internet time at timezone %timezone"
//% timezone.min=-11 timezone.max=13
export function initInternetTime(timezone: number) {
// Reset the flags.
internetTimeInitialized = false
internetTimeUpdated = false
// Make sure the WiFi is connected.
if (isWifiConnected() == false) return
// Enable the SNTP and set the timezone. Return if failed.
if (sendCommand("AT+CIPSNTPCFG=1," + timezone + ",\"" + NTP_SERVER_URL + "\"", "OK", 500) == false) return
internetTimeInitialized = true
return
}
/**
* Return true if the internet time is updated successfully.
*/
//% subcategory="Internet Time"
//% weight=21
//% blockGap=8
//% blockId=esp8266_is_internet_time_updated
//% block="internet time updated"
export function isInternetTimeUpdated(): boolean {
return internetTimeUpdated
}
/**
* Update the internet time.
* @param timezone Timezone. eg: 8
*/
//% subcategory="Internet Time"
//% weight=20
//% blockGap=8
//% blockId=esp8266_update_internet_time
//% block="update internet time"
export function updateInternetTime() {
// Reset the flag.
internetTimeUpdated = false
// Make sure the WiFi is connected.
if (isWifiConnected() == false) return
// Make sure it's initialized.
if (internetTimeInitialized == false) return
// Wait until we get a valid time update.
let responseArray
let timestamp = input.runningTime()
while (true) {
// Timeout after 10 seconds.
if (input.runningTime() - timestamp > 20000) {
return
}
// Get the time.
sendCommand("AT+CIPSNTPTIME?")
let response = getResponse("+CIPSNTPTIME:", 2000)
if (response == "") return
// Fill up the time and date accordingly.
response = response.slice(response.indexOf(":") + 1)
responseArray = response.split(" ")
// Remove the preceeding " " for each field.
while (responseArray.removeElement(""));
// If the year is still 1970, means it's not updated yet.
if (responseArray[4] != "1970") {
break
}
basic.pause(100)
}
// Day of week.
switch (responseArray[0]) {
case "Mon": weekday = 1; break
case "Tue": weekday = 2; break
case "Wed": weekday = 3; break
case "Thu": weekday = 4; break
case "Fri": weekday = 5; break
case "Sat": weekday = 6; break
case "Sun": weekday = 7; break
}
// Month.
switch (responseArray[1]) {
case "Jan": month = 1; break
case "Feb": month = 2; break
case "Mar": month = 3; break
case "Apr": month = 4; break
case "May": month = 5; break
case "Jun": month = 6; break
case "Jul": month = 7; break
case "Aug": month = 8; break
case "Sep": month = 9; break
case "Oct": month = 10; break
case "Nov": month = 11; break
case "Dec": month = 12; break
}
// Day.
day = parseInt(responseArray[2])
// Time.
let timeArray = responseArray[3].split(":")
hour = parseInt(timeArray[0])
minute = parseInt(timeArray[1])
second = parseInt(timeArray[2])
// Year.
year = parseInt(responseArray[4])
// Wait until OK is received.
if (getResponse("OK") == "") return
internetTimeUpdated = true
return
}
}