-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
140 lines (128 loc) · 5.28 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
namespace ESP8266_SUPER_CONNECT {
let wifi_connected: boolean = false
let thingspeak_connected: boolean = false
let ifttt_connected:boolean = false
let last_upload_successful: boolean = false
let serial_str: string = ""
// 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(str: string): boolean {
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(str)) {
result = true
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, pwd: string) {
wifi_connected = false
thingspeak_connected = false
serial.redirect(
tx,
rx,
baudrate
)
sendAT("AT+RESTORE", 1000) // restore to factory settings
sendAT("AT+RST", 1000) // reset
sendAT("AT+CWMODE=1") // set to STA mode
// join wifi router
sendAT("AT+CWJAP=\"" + ssid + "\",\"" + pwd + "\"")
wifi_connected = waitResponse("OK")
//if (!result) control.reset()
}
/**
* Connect to ThingSpeak and upload data. It would not upload anything if it failed to connect to Wifi or ThingSpeak.
*/
//% block="Upload data to ThingSpeak|URL/IP = %ip|Write API key = %write_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
//% write_api_key.defl=your_write_api_key
export function connectThingSpeak(ip: string, write_api_key: string, n1: number, n2: number, n3: number, n4: number, n5: number, n6: number, n7: number, n8: number) {
if (wifi_connected && write_api_key != "") {
thingspeak_connected = false
sendAT("AT+CIPSTART=\"TCP\",\"" + ip + "\",80", 0) // connect to website server
thingspeak_connected = waitResponse("OK")
basic.pause(100)
if (thingspeak_connected) {
last_upload_successful = false
let str: string = "GET /update?api_key=" + write_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("OK")
basic.pause(100)
}
}
}
/**
* Connect to IFTTT and call a Webhook. It would not call anything if it failed to connect to Wifi or IFTTT.
*/
//% block="IFTTT Webhook|URL/IP = %ip|Event name = %event_name|Key = %key|Value = %value"
//% ip.defl=maker.ifttt.com
//% event_name.defl=your_event_name
//% key.defl=your_key
//% value.defl=value
export function IFTTTWebhook (ip: string, event_name: string, key: string, value: string) {
if (wifi_connected && event_name != "" && key != "") {
ifttt_connected = false
sendAT("AT+CIPSTART=\"TCP\",\"maker.ifttt.com\",80", 0) // connect to website server
ifttt_connected = waitResponse("OK")
basic.pause(100)
if (ifttt_connected) {
last_upload_successful = false
let str: string = `POST /trigger/${event_name}/with/key/${key} HTTP/1.1\u000D\u000AHost: maker.ifttt.com\u000D\u000AContent-Type: application/x-www-form-urlencoded\u000D\u000AContent-Length: 38\u000D\u000A\u000D\u000Avalue1=${value}\u000D\u000A`
sendAT("AT+CIPSEND=" + (str.length + 2))
sendAT(str, 0) // upload data
last_upload_successful = waitResponse("OK")
basic.pause(100)
}
}
}
/**
* Wait between uploads
*/
//% block="Wait %delay ms"
//% delay.min=0 delay.defl=5000
export function wait(delay: number) {
if (delay > 0) basic.pause(delay)
}
/**
* Check if ESP8266 successfully connected to Wifi
*/
//% block="Wifi connected ?"
export function isWifiConnected() {
return wifi_connected
}
/**
* Check if ESP8266 successfully connected to ThingSpeak
*/
//% block="ThingSpeak connected ?"
export function isThingSpeakConnected() {
return thingspeak_connected
}
/**
* Check if ESP8266 successfully uploaded data to ThingSpeak
*/
//% block="Last data upload successful ?"
export function isLastUploadSuccessful() {
return last_upload_successful
}
}