-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.ts
263 lines (217 loc) · 7.95 KB
/
main.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
262
263
/*******************************************************************************
* MakeCode extension for ESP8266 Wifi module.
*
* Company: Cytron Technologies Sdn Bhd
* Website: http://www.cytron.io
* Email: [email protected]
*******************************************************************************/
/**
* Blocks for ESP8266 WiFi module.
*/
//% weight=10 color=#ff8000 icon="\uf1eb" block="ESP8266 WiFi"
namespace esp8266 {
// Flag to indicate whether the ESP8266 was initialized successfully.
let esp8266Initialized = false
// Buffer for data received from UART.
let rxData = ""
/**
* Send AT command and wait for response.
* Return true if expected response is received.
* @param command The AT command without the CRLF.
* @param expected_response Wait for this response.
* @param timeout Timeout in milliseconds.
*/
//% blockHidden=true
//% blockId=esp8266_send_command
export function sendCommand(command: string, expected_response: string = null, timeout: number = 100): boolean {
// Wait a while from previous command.
basic.pause(10)
// Flush the Rx buffer.
serial.readString()
rxData = ""
// Send the command and end with "\r\n".
serial.writeString(command + "\r\n")
// Don't check if expected response is not specified.
if (expected_response == null) {
return true
}
// Wait and verify the response.
let result = false
let timestamp = input.runningTime()
while (true) {
// Timeout.
if (input.runningTime() - timestamp > timeout) {
result = false
break
}
// Read until the end of the line.
rxData += serial.readString()
if (rxData.includes("\r\n")) {
// Check if expected response received.
if (rxData.slice(0, rxData.indexOf("\r\n")).includes(expected_response)) {
result = true
break
}
// If we expected "OK" but "ERROR" is received, do not wait for timeout.
if (expected_response == "OK") {
if (rxData.slice(0, rxData.indexOf("\r\n")).includes("ERROR")) {
result = false
break
}
}
// Trim the Rx data before loop again.
rxData = rxData.slice(rxData.indexOf("\r\n") + 2)
}
}
return result
}
/**
* Get the specific response from ESP8266.
* Return the line start with the specific response.
* @param command The specific response we want to get.
* @param timeout Timeout in milliseconds.
*/
//% blockHidden=true
//% blockId=esp8266_get_response
export function getResponse(response: string, timeout: number = 100): string {
let responseLine = ""
let timestamp = input.runningTime()
while (true) {
// Timeout.
if (input.runningTime() - timestamp > timeout) {
// Check if expected response received in case no CRLF received.
if (rxData.includes(response)) {
responseLine = rxData
}
break
}
// Read until the end of the line.
rxData += serial.readString()
if (rxData.includes("\r\n")) {
// Check if expected response received.
if (rxData.slice(0, rxData.indexOf("\r\n")).includes(response)) {
responseLine = rxData.slice(0, rxData.indexOf("\r\n"))
// Trim the Rx data for next call.
rxData = rxData.slice(rxData.indexOf("\r\n") + 2)
break
}
// Trim the Rx data before loop again.
rxData = rxData.slice(rxData.indexOf("\r\n") + 2)
}
}
return responseLine
}
/**
* Format the encoding of special characters in the url.
* @param url The url that we want to format.
*/
//% blockHidden=true
//% blockId=esp8266_format_url
export function formatUrl(url: string): string {
url = url.replaceAll("%", "%25")
url = url.replaceAll(" ", "%20")
url = url.replaceAll("!", "%21")
url = url.replaceAll("\"", "%22")
url = url.replaceAll("#", "%23")
url = url.replaceAll("$", "%24")
url = url.replaceAll("&", "%26")
url = url.replaceAll("'", "%27")
url = url.replaceAll("(", "%28")
url = url.replaceAll(")", "%29")
url = url.replaceAll("*", "%2A")
url = url.replaceAll("+", "%2B")
url = url.replaceAll(",", "%2C")
url = url.replaceAll("-", "%2D")
url = url.replaceAll(".", "%2E")
url = url.replaceAll("/", "%2F")
url = url.replaceAll(":", "%3A")
url = url.replaceAll(";", "%3B")
url = url.replaceAll("<", "%3C")
url = url.replaceAll("=", "%3D")
url = url.replaceAll(">", "%3E")
url = url.replaceAll("?", "%3F")
url = url.replaceAll("@", "%40")
url = url.replaceAll("[", "%5B")
url = url.replaceAll("\\", "%5C")
url = url.replaceAll("]", "%5D")
url = url.replaceAll("^", "%5E")
url = url.replaceAll("_", "%5F")
url = url.replaceAll("`", "%60")
url = url.replaceAll("{", "%7B")
url = url.replaceAll("|", "%7C")
url = url.replaceAll("}", "%7D")
url = url.replaceAll("~", "%7E")
return url
}
/**
* Return true if the ESP8266 is already initialized.
*/
//% weight=30
//% blockGap=8
//% blockId=esp8266_is_esp8266_initialized
//% block="ESP8266 initialized"
export function isESP8266Initialized(): boolean {
return esp8266Initialized
}
/**
* Initialize the ESP8266.
* @param tx Tx pin of micro:bit. eg: SerialPin.P16
* @param rx Rx pin of micro:bit. eg: SerialPin.P15
* @param baudrate UART baudrate. eg: BaudRate.BaudRate115200
*/
//% weight=29
//% blockGap=40
//% blockId=esp8266_init
//% block="initialize ESP8266: Tx %tx Rx %rx Baudrate %baudrate"
export function init(tx: SerialPin, rx: SerialPin, baudrate: BaudRate) {
// Redirect the serial port.
serial.redirect(tx, rx, baudrate)
serial.setTxBufferSize(128)
serial.setRxBufferSize(128)
// Reset the flag.
esp8266Initialized = false
// Restore the ESP8266 factory settings.
if (sendCommand("AT+RESTORE", "ready", 5000) == false) return
// Turn off echo.
if (sendCommand("ATE0", "OK") == false) return
// Initialized successfully.
// Set the flag.
esp8266Initialized = true
}
/**
* Return true if the ESP8266 is connected to WiFi router.
*/
//% weight=28
//% blockGap=8
//% blockId=esp8266_is_wifi_connected
//% block="WiFi connected"
export function isWifiConnected(): boolean {
// Get the connection status.
sendCommand("AT+CIPSTATUS")
let status = getResponse("STATUS:", 1000)
// Wait until OK is received.
getResponse("OK")
// Return the WiFi status.
if ((status == "") || status.includes("STATUS:5")) {
return false
}
else {
return true
}
}
/**
* Connect to WiFi router.
* @param ssid Your WiFi SSID.
* @param password Your WiFi password.
*/
//% weight=27
//% blockGap=8
//% blockId=esp8266_connect_wifi
//% block="connect to WiFi: SSID %ssid Password %password"
export function connectWiFi(ssid: string, password: string) {
// Set to station mode.
sendCommand("AT+CWMODE=1", "OK")
// Connect to WiFi router.
sendCommand("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"", "OK", 20000)
}
}