-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom.ts
81 lines (74 loc) · 3.36 KB
/
custom.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
//% color=#009b5b icon="\uf1eb" block="ESP8266 ThingSpeak"
namespace esp8266_naim{
let wifi_connected: boolean = false
let thingspeak_connected: boolean = false
let last_upload_successful: boolean = false
// write AT command with CR+LF ending
function sendAT(command: string, wait: number = 100) {
serial.writeString(command + "\u000D\u000A")
basic.pause(wait)
}
// wait for certain response from ESP8266
function waitResponse(): boolean {
let serial_str: string = ""
let result: boolean = false
let time: number = input.runningTime()
while (true) {
serial_str += serial.readString()
if (serial_str.length > 200) serial_str = serial_str.substr(serial_str.length - 200)
if (serial_str.includes("OK") || serial_str.includes("ALREADY CONNECTED")) {
result = true
break
} else if (serial_str.includes("ERROR") || serial_str.includes("SEND FAIL")) {
break
}
if (input.runningTime() - time > 30000) break
}
return result
}
/**
* Initialize ESP8266 module and connect it to Wifi router
*/
//% block="Initialize ESP8266|RX (Tx of micro:bit) %tx|TX (Rx of micro:bit) %rx|Baud rate %baudrate|Wifi SSID = %ssid|Wifi PW = %pw"
//% tx.defl=SerialPin.P0
//% rx.defl=SerialPin.P1
//% ssid.defl=your_ssid
//% pw.defl=your_pw
export function connectWifi(tx: SerialPin, rx: SerialPin, baudrate: BaudRate, ssid: string, pw: string) {
wifi_connected = false
thingspeak_connected = false
serial.redirect(
tx,
rx,
baudrate
)
sendAT("AT+RESTORE", 1000) // restore to factory settings
sendAT("AT+CWMODE=1") // set to STA mode
sendAT("AT+RST", 1000) // reset
sendAT("AT+CWJAP=\"" + ssid + "\",\"" + pw + "\"", 0) // connect to Wifi router
wifi_connected = waitResponse()
basic.pause(100)
}
/**
* Connect to ThingSpeak and download data. It would not download anything if it failed to connect to Wifi or ThingSpeak.
*/
//% block="download data from ThingSpeak|URL/IP = %ip|read API key = %read_api_key|Field 1 = %n1|Field 2 = %n2|Field 3 = %n3|Field 4 = %n4|Field 5 = %n5|Field 6 = %n6|Field 7 = %n7|Field 8 = %n8"
//% ip.defl=api.thingspeak.com
//% read_api_key.defl=your_read_api_key
export function connectThingSpeak(ip: string, read_api_key: string, n1: number, n2: number, n3: number, n4: number, n5: number, n6: number, n7: number, n8: number) {
if (wifi_connected && read_api_key != "") {
thingspeak_connected = false
sendAT("AT+CIPSTART=\"TCP\",\"" + ip + "\",80", 0) // connect to website server
thingspeak_connected = waitResponse()
basic.pause(100)
if (thingspeak_connected) {
// last_download_successful = false
let str: string = "GET /update?api_key=" + read_api_key + "&field1=" + n1 + "&field2=" + n2 + "&field3=" + n3 + "&field4=" + n4 + "&field5=" + n5 + "&field6=" + n6 + "&field7=" + n7 + "&field8=" + n8
sendAT("AT+CIPSEND=" + (str.length + 2))
sendAT(str, 0) // upload data
last_upload_successful = waitResponse()
basic.pause(100)
}
}
}
}