-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
570 lines (523 loc) · 19.4 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
enum HttpMethod {
GET,
POST,
PUT,
HEAD,
DELETE,
PATCH,
OPTIONS,
CONNECT,
TRACE
}
enum ESPMODE {
STA,
AP,
AP_STA
}
/**
* esp-01 tool
*/
//% weight=6 color=#2699BF icon="\uf110" block="esp-01 tool"
namespace esp01 {
let NEWLINE: string = "\u000D\u000A"
let myMethods: string[] = ["GET", "POST", "PUT", "HEAD", "DELETE", "PATCH", "OPTIONS", "CONNECT", "TRACE"]
let sendSerialTimeout: number = 1000
let serial_str: string = ""
let client_ID: string = "192.168.4.2"
let LED_status: number = 0x0
// getResponse
function get_response(): string {
let result: boolean = false
let response: string = ""
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
response += serial.readString()
if (response.length > 200) {
response = response.substr(response.length - 200)
}
})
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), () => { })
return response
}
//
function wait_for_response(str: string): boolean {
return get_response().includes(str)
}
/**
* Unicode NewLine chars
*/
//% weight=92
//% group="Wifi Common"
//% blockId="esp01_crlf" block="CRLF"
export function newline(): string {
return NEWLINE
}
/**
* Send AT command.
* @param cmd AT command, eg: "AT"
* @param sleepTime Sleep time, eg: 1000
*/
//% weight=91
//% group="Wifi Common"
//% blockId="esp01_send_at" block="send AT command %cmd and wait %sleepTime ms"
export function sendATCommand(cmd: string, sleepTime: number = 1000): void {
serial.writeString(cmd + NEWLINE)
if (sleepTime > 0) basic.pause(sleepTime)
}
/**
* Setup ESP-01
* @param TXPin TXPin, eg: SerialPin.P1
* @param RXPin RXPin, eg: SerialPin.P2
*/
//% weight=100
//% group="Wifi Common"
//% blockId="esp01_init_uart" block="Setup ESP-01, TXPin:%TXPin , RXPin:%RXPin"
export function initUART(TXPin: SerialPin = SerialPin.P1, RXPin: SerialPin = SerialPin.P2): void {
serial.redirect(TXPin, RXPin, BaudRate.BaudRate115200)
}
/**
* Set ESP-01 mode
* @param mode mode, eg: ESPMODE.STA
*/
//% weight=100
//% group="Wifi Common"
//% blockId="esp01_setMode" block="Set ESP-01 mode: %mode"
export function setMode(mode: ESPMODE = ESPMODE.STA): void {
// WIFI mode = Station mode (client) + AP Server:
sendATCommand("AT+CWMODE=" + (mode + 1), 5000)
// Restart module:
sendATCommand("AT+RST", 2000)
}
/**
* Connect to WiFi Router.
* @param wifiRouter wifi Router, eg: "myHomeWifi"
* @param password Password, eg: "password"
*/
//% weight=99
//% group="Wifi Router Suite"
//% blockId="esp01_connect_wifi" block="connect to WiFi Router %wifiRouter, %password"
export function connectToWiFiRouter(wifiRouter: string, password: string): boolean {
// Connect to AP:
sendATCommand("AT+CWJAP=\"" + wifiRouter + "\",\"" + password + "\"", 0)
let result: boolean = wait_for_response("OK")
//AT+CIFSR//get ip address
//AT+CIPMUX=0//set single connection
//AT+CIPMODE=1//enable wifi transfer
//AT+CIPSTART="TCP","192.168.4.1",8899//connect tcp connection to ap
//AT+CIPSEND//send command
//>
//ABCDEFG//send data
return result
}
/**
* Disconnect from WiFi Router.
*/
//% weight=98
//% group="Wifi Router Suite"
//% blockId="esp01_disconnect_wifi" block="disconnect from WiFi Router"
export function disconnectFromWiFiRouter(): void {
// Disconnect from AP:
sendATCommand("AT+CWQAP", 6000)
}
/**
* send TCP
* @param destIP destination IP address, eg: 192.168.4.2
* @param destPort destination port, eg: 1000
*/
//% weight=80
//% group="send"
//% blockId="esp01_sendTCP" block="send TCP, host: %destIP|destPort: %port"
//% inlineInputMode=inline
export function sendTCP(destIP: string, destPort: number): void {
sendATCommand("AT+CIPSTART=\"TCP\",\"" + destIP + "\"," + destPort + "")
}
/**
* send UDP
* @param PC_UDPServer_IP destination IP address, eg: 192.168.4.2
* @param PC_UDPServer_Port PC UDPServer Port, eg: 5000
* @param destPort destination port, eg: 1000
*/
//% weight=80
//% group="send"
//% blockId="esp01_sendUDP" block="send UDP, PC UDPServer IP: %PC_UDPServer_IP|PC UDPServer Port: %PC_UDPServer_Port|dest Port: %destPort"
//% inlineInputMode=inline
export function sendUDP(PC_UDPServer_IP: string, PC_UDPServer_Port: number, destPort: number): void {
sendATCommand("AT+CIPSTART=\"UDP\",\"+PC_UDPServer_IP+\"," + PC_UDPServer_Port + "," + destPort + ",0")
}
/**
* AP TCP Server
* @param ssid ssid, eg: ESP8266
* @param password AP server password, eg: 123456
*/
//% weight=80
//% group="AP"
//% blockId="esp01_AP_TCP" block="AP TCP Server, ssid: %ssid|password: %password"
//% inlineInputMode=inline
export function AP_TCP(ssid: string, password: string): void {
sendATCommand("AT+CWMODE=2")
// Restart module:
sendATCommand("AT+RST", 2000)
sendATCommand("AT+CWSAP=\"" + ssid + "\",\"" + password + "\",11,0")
sendATCommand("AT+CIPSERVER=1,8899")
//will send data
//AT+CIPSEND=0,11
}
/**
* STA TCP Server
* @param wifiRouter wifi Router, eg: myHomeWifi
* @param password AP server password, eg: 123456
*/
//% weight=80
//% group="STA"
//% blockId="esp01_STA_TCP" block="STA TCP Server, wifi router: %wifiRouter|password: %password"
//% inlineInputMode=inline
export function STA_TCP(wifiRouter: string, password: string): void {
sendATCommand("AT+CWMODE=1")
// Restart module:
sendATCommand("AT+RST", 2000)
sendATCommand("AT+CWLAP")
sendATCommand("AT+CWJAP=\"" + wifiRouter + "\",\"" + password + "\"")
sendATCommand("AT+CIFSR")
sendATCommand("AT+CIPMUX=1")
sendATCommand("AT+CIPSERVER=1,8899")
//will send data
//AT+CIPSEND=0,11
}
/**
* STA TCP Client
* @param wifiRouter wifi Router, eg: myHomeWifi
* @param password AP server password, eg: 123456
*/
//% weight=80
//% group="STA"
//% blockId="esp01_STA_TCPClient" block="STA TCP Client, wifi router: %wifiRouter|password: %password"
//% inlineInputMode=inline
export function STA_TCPClient(wifiRouter: string, password: string): void {
sendATCommand("AT+CWMODE=1")
// Restart module:
sendATCommand("AT+RST", 2000)
sendATCommand("AT+CWLAP")
sendATCommand("AT+CWJAP=\"" + wifiRouter + "\",\"" + password + "\"")
sendATCommand("AT+CIFSR")
sendATCommand("AT+CIPMUX=0")
sendATCommand("AT+CIPMODE=1")
////connect to server
//sendATCommand("AT+CIPSTART=\"TCP\",\""+destIP+"\","+destPort+"")
//AT+CIPSTART="TCP","192.168.43.104",8899
//AT+CIPSEND
//data
//+++
}
/**
* STA UDP MultiConnections
* @param wifiRouter wifi Router, eg: myHomeWifi
* @param password AP server password, eg: 123456
*/
//% weight=80
//% group="AP"
//% blockId="esp01_STA_UDP_MultiConnections" block="STA UDP Multiple Connections, wifi router: %wifiRouter|password: %password"
//% inlineInputMode=inline
export function STA_UDP_MultiConnections(wifiRouter: string, password: string): void {
sendATCommand("AT+CWMODE=1")
// Restart module:
sendATCommand("AT+RST", 2000)
sendATCommand("AT+CWLAP")
sendATCommand("AT+CWJAP=\"" + wifiRouter + "\",\"" + password + "\"")
sendATCommand("AT+CIFSR")
sendATCommand("AT+CIPMUX=1")
//sendATCommand("AT+CIPSTART=0,\"UDP\",\"255.255.255.255\","+PC_UDPServer_Port+","+destPort+", 0")
//AT+CIPSTART=0,"UDP","255.255.255.255",50000,1000, 0
//will send data
//AT+CIPSEND=0,11
}
/**
* STA UDP AT
* @param wifiRouter wifi Router, eg: myHomeWifi
* @param password AP server password, eg: 123456
*/
//% weight=80
//% group="STA"
//% blockId="esp01_STA_UDP_AT" block="STA UDP AT, wifi router: %wifiRouter|password: %password"
//% inlineInputMode=inline
export function STA_UDP_AT(wifiRouter: string, password: string): void {
sendATCommand("AT+CWMODE=1")
// Restart module:
sendATCommand("AT+RST", 2000)
sendATCommand("AT+CWLAP")
sendATCommand("AT+CWJAP=\"" + wifiRouter + "\",\"" + password + "\"")
sendATCommand("AT+CIFSR")
sendATCommand("AT+CIPMUX=0")
sendATCommand("AT+CIPMODE=1")
//sendATCommand("AT+CIPSTART=\"UDP\",\"+PC_UDPServer_IP+\","+PC_UDPServer_Port+","+destPort+",0")
//AT+CIPSTART="UDP","192.168.43.104",5000,2000,0
//will send data
//AT+CIPSEND=0,11
}
/**
* AP UDP For 2 Modules
* @param ssid ssid, eg: ESP8266
* @param password AP server password, eg: 123456
*/
//% weight=80
//% group="AP"
//% blockId="esp01_AP_UDP_For2Modules" block="AP UDP for 2 modules, ssid: %ssid|password: %password"
//% inlineInputMode=inline
export function AP_UDP_For2Modules(ssid: string, password: string): void {
sendATCommand("AT+CWMODE=2")
// Restart module:
sendATCommand("AT+RST", 2000)
sendATCommand("AT+CWSAP=\"" + ssid + "\",\"" + password + "\",11,0")
sendATCommand("AT+CIPMUX=0")
sendATCommand("AT+CIPMODE=1")
//wait another module to send, then we can send
//AT+CIPSTART="UDP","192.168.4.2",333,333,0
//will send data
//AT+CIPSEND=0,11
}
/**
* STA UDP For 2 Modules
* @param ssid ssid, eg: ESP8266
* @param password AP server password, eg: 123456
*/
//% weight=80
//% group="STA"
//% blockId="esp01_STA_UDP_For2Modules" block="STA UDP for 2 modules, ssid: %ssid|password: %password"
//% inlineInputMode=inline
export function STA_UDP_For2Modules(ssid: string, password: string): void {
sendATCommand("AT+CWMODE=1")
// Restart module:
sendATCommand("AT+RST", 2000)
sendATCommand("AT+CWLAP")
sendATCommand("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"")
sendATCommand("AT+CIPMUX=0")
sendATCommand("AT+CIPMODE=1")
//AT+CIPSTART="UDP","192.168.4.1",333,333,0
//will send data
//AT+CIPSEND
}
/**
* AP TCP For 2 Modules
* @param ssid ssid, eg: ESP8266
* @param password AP server password, eg: 123456
*/
//% weight=80
//% group="AP"
//% blockId="esp01_AP_TCP_For2Modules" block="AP TCP for 2 modules, ssid: %ssid|password: %password"
//% inlineInputMode=inline
export function AP_TCP_For2Modules(ssid: string, password: string): void {
sendATCommand("AT+CWMODE=2")
// Restart module:
sendATCommand("AT+RST", 2000)
sendATCommand("AT+CWSAP=\"" + ssid + "\",\"" + password + "\",11,0")
sendATCommand("AT+CIPMUX=1")
sendATCommand("AT+CIPSERVER=1,8899")
//will send data
//AT+CIPSEND=0,11
}
/**
* STA TCP For 2 Modules
* @param ssid ssid, eg: ESP8266
* @param password AP server password, eg: 123456
*/
//% weight=80
//% group="STA"
//% blockId="esp01_STA_TCP_For2Modules" block="STA TCP for 2 modules, ssid: %ssid|password: %password"
//% inlineInputMode=inline
export function STA_TCP_For2Modules(ssid: string, password: string): void {
sendATCommand("AT+CWMODE=1")
// Restart module:
sendATCommand("AT+RST", 2000)
sendATCommand("AT+CWLAP")
sendATCommand("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"")
sendATCommand("AT+CIFSR")
sendATCommand("AT+CIPMUX=0")
sendATCommand("AT+CIPMODE=1")
//sendATCommand("AT+CIPSTART="TCP","192.168.4.1",8899")
//will send data
//AT+CIPSEND
}
/**
* STA_AP TCP For Cloud
* @param wifiRouter wifi Router, eg: ESP8266
* @param password STA_AP server password, eg: 123456
*/
//% weight=80
//% group="Wifi Common"
//% blockId="esp01_STA_AP_TCP_ForCloud" block="STA_AP TCP for cloud, wifiRouter: %wssid|password: %password"
//% inlineInputMode=inline
export function STA_AP_TCP_ForCloud(wifiRouter: string, password: string): void {
sendATCommand("AT+CWMODE=3")
// Restart module:
sendATCommand("AT+RST", 2000)
sendATCommand("AT+CWLAP")
sendATCommand("AT+CWJAP=\"" + wifiRouter + "\",\"" + password + "\"")
sendATCommand("AT+CIPMUX=0")
sendATCommand("AT+CIPMODE=1")
//sendATCommand("AT+CIPSTART="TCP","115.29.109.104",6602")
//will send data
//AT+CIPSEND
}
// generate HTML
// LED_status=0x23be, 0x23be=0000 0000 0010 0011 1011 1110
function getHTML(normal: boolean): string {
let LED_statusString: string = ""
let LED_buttonString: string = ""
let web_title: string = "ESP8266 (ESP-01) Wifi on BBC micro:bit"
let html: string = ""
html += "HTTP/1.1 200 OK\r\n" // HTTP response
html += "Content-Type: text/html\r\n"
html += "Connection: close\r\n\r\n"
html += "<!DOCTYPE html>"
html += "<html>"
html += "<head>"
html += "<link rel=\"icon\" href=\"data:,\">"
html += "<title>" + web_title + "</title>"
html += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
html += "</head>"
html += "<body>"
html += "<div style=\"text-align:center\">"
html += "<h1>" + web_title + "</h1>"
html += "<br>"
// generate status text
if (normal) {
for (let i = 0; i < 4; ++i) {
const index = 3 - i;
const a = LED_status >> index;
if ((a & 1) == 0) {
LED_statusString = "OFF"
LED_buttonString = "TURN IT ON"
}
else {
LED_statusString = "ON"
LED_buttonString = "TURN IT OFF"
}
html += "<h3>LED" + i + " STATUS: " + LED_statusString + "</h3>"
html += "<br>"
// generate buttons
html += "<input type=\"button\" onClick=\"window.location.href=\'LED" + (i + 1) + "\'\" value=\"" + LED_buttonString + "\">"
html += "<br>"
}
} else {
html += "<h3>ERROR: REQUEST NOT FOUND</h3>"
}
html += "<br>"
html += "<input type=\"button\" onClick=\"window.location.href=\'/'\" value=\"Home\">"
html += "</div>"
html += "</body>"
html += "</html>"
return html
}
/**
* Get AP Web Request.
*/
//% weight=96
//% group="Wifi Server Suite"
//% blockId="esp01_get_webrequest" block="get AP web request"
export function getAPWebRequest(): string {
let time: number = input.runningTime()
let getCommand: string = ""
while (true) {
// read and store 200 characters from serial port
serial_str += serial.readString()
if (serial_str.length > 200) {
serial_str = serial_str.substr(serial_str.length - 200)
}
if (serial_str.includes("+IPD") && serial_str.includes("HTTP")) {
// got a HTTP request
client_ID = serial_str.substr(serial_str.indexOf("IPD") + 4, 1)
let GET_pos: number = serial_str.indexOf("GET")
let HTTP_pos: number = serial_str.indexOf("HTTP")
getCommand = serial_str.substr(GET_pos + 5, (HTTP_pos - 1) - (GET_pos + 5))
break;
}
if (input.runningTime() - time > 30000) {
getCommand = "TIMEOUT"
break
}
}
let posLED = getCommand.indexOf("LED")
if (posLED != -1) {
let getNum = parseInt(getCommand.substr(3, getCommand.length - posLED - 1)) - 1
let a = LED_status >> getNum
if ((a & 1) == 0) {
LED_status = LED_status | (1 << getNum)
}
else {
LED_status = (LED_status & ~(1 << getNum))
}
}
return getCommand
}
/**
* Serve Web HTML.
* @param getSuccess getSuccess, eg: true
*/
//% weight=95
//% group="Wifi Server Suite"
//% blockId="esp01_serve_webhtml" block="serve Web HTML, success page: %getSuccess"
export function serveWebHTML(getSuccess: boolean = true): void {
// output HTML
let HTML_str: string = getHTML(getSuccess)
// send HTML to user
sendATCommand("AT+CIPSEND=" + client_ID + "," + (HTML_str.length + 2))
sendATCommand(HTML_str, 1000)
// close connection
sendATCommand("AT+CIPCLOSE=" + client_ID)
}
/**
* Send HTTP method.
* @param method HTTP method, eg: HttpMethod.GET
* @param host Host, eg: "www.baidu.com"
* @param port Port, eg: 80
* @param urlPath Path, eg: "/s?wd=something"
* @param headers Headers
* @param body Body
*/
//% weight=94
//% group="Wifi HTTP Suite"
//% blockId="esp01_sendhttp" block="execute HTTP method %method|host: %host|port: %port|path: %urlPath|headers: %headers|body: %body"
//% inlineInputMode=inline
export function sendHttp(method: HttpMethod, host: string, port: number, urlPath: string, headers?: string, body?: string): void {
let myMethod: string = myMethods[method]
// Establish TCP connection:
let data: string = "AT+CIPSTART=\"TCP\",\"" + host + "\"," + port
sendATCommand(data, sendSerialTimeout * 6)
data = myMethod + " " + urlPath + " HTTP/1.1" + NEWLINE
+ "Host: " + host + NEWLINE
if (headers && headers.length > 0) {
data += headers + NEWLINE
}
if (data && data.length > 0) {
data += NEWLINE + body + NEWLINE
}
data += NEWLINE
// Send data:
sendATCommand("AT+CIPSEND=" + (data.length + 2), sendSerialTimeout * 3)
sendATCommand(data, sendSerialTimeout * 6)
// Close TCP connection:
sendATCommand("AT+CIPCLOSE", sendSerialTimeout * 3)
}
/**
* Send HTTP method and response.
* @param method HTTP method, eg: HttpMethod.GET
* @param host Host, eg: "www.baidu.com"
* @param port Port, eg: 80
* @param urlPath Path, eg: "/s?wd=something"
* @param headers Headers
* @param body Body
*/
//% weight=93
//% group="Wifi HTTP Suite"
//% blockId="esp_sendhttp_response" block="Send HTTP method %method|host: %host|port: %port|path: %urlPath|headers: %headers|body: %body"
//% inlineInputMode=inline
export function sendHttpAndResponse(method: HttpMethod, host: string, port: number, urlPath: string, headers?: string, body?: string): string {
let response: string
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
response += serial.readString()
})
sendHttp(method, host, port, urlPath, headers, body)
let value: string = response.substr(response.indexOf("[") + 2, response.indexOf("]") - response.indexOf("[") - 3)
response = null
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), () => { })
return value
}
}