-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathesp8266.ts
165 lines (139 loc) · 6 KB
/
esp8266.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
//% weight=2 color=#1174EE icon="\uf1eb" block="ESP8266"
namespace esp8266 {
let isWifiConnected = false;
/**
* Setup Uart WiFi to connect to Wi-Fi
*/
//% block="Setup Wifi|TX %txPin|RX %rxPin|Baud rate %baudrate|SSID = %ssid|Password = %passwd"
//% txPin.defl=SerialPin.C17
//% rxPin.defl=SerialPin.C16
//% baudRate.defl=BaudRate.BaudRate115200
export function setupWifi(txPin: SerialPin, rxPin: SerialPin, baudRate: BaudRate, ssid: string, passwd: string) {
let result = 0
isWifiConnected = false
serial.redirect(
txPin,
rxPin,
baudRate
)
sendAtCmd("AT")
result = waitAtResponse("OK", "ERROR", "None", 1000)
sendAtCmd("AT+CWMODE=1")
result = waitAtResponse("OK", "ERROR", "None", 1000)
sendAtCmd(`AT+CWJAP="${ssid}","${passwd}"`)
result = waitAtResponse("WIFI GOT IP", "ERROR", "None", 20000)
if (result == 1) {
isWifiConnected = true
}
}
/**
* Check if Grove - Uart WiFi V2 is connected to Wifi
*/
//% block="Wifi OK?"
export function wifiOK() {
return isWifiConnected
}
/**
* Send data to ThinkSpeak
*/
//% block="Send Data to your ThinkSpeak Channel|Write API Key %apiKey|Field1 %field1|Field2 %field2|Field3 %field3|Field4 %field4|Field5 %field5|Field6 %field6|Field7 %field7|Field8 %field8"
//% block.loc.de="Sende Daten an deinen ThinkSpeak Kanal|Write API Key %apiKey|Feld 1 %field1||Feld 2 %field2|Feld 3 %field3|Feld 4 %field4|Feld 5 %field5|Feld 6 %field6|Feld 7 %field7|Feld 8 %field8"
//% expandableArgumentMode="enabled"
//% apiKey.defl="your Write API Key"
export function sendToThinkSpeak(apiKey: string, field1: number=0, field2: number=0, field3: number=0, field4: number=0, field5: number=0, field6: number=0, field7: number=0, field8: number=0) {
let result = 0
let retry = 2
// close the previous TCP connection
if (isWifiConnected) {
sendAtCmd("AT+CIPCLOSE")
waitAtResponse("OK", "ERROR", "None", 2000)
}
while (isWifiConnected && retry > 0) {
retry = retry - 1;
// establish TCP connection
sendAtCmd("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",80")
result = waitAtResponse("OK", "ALREADY CONNECTED", "ERROR", 2000)
if (result == 3) continue
let data = "GET /update?api_key=" + apiKey
if (!isNaN(field1)) data = data + "&field1=" + field1
if (!isNaN(field2)) data = data + "&field2=" + field2
if (!isNaN(field3)) data = data + "&field3=" + field3
if (!isNaN(field4)) data = data + "&field4=" + field4
if (!isNaN(field5)) data = data + "&field5=" + field5
if (!isNaN(field6)) data = data + "&field6=" + field6
if (!isNaN(field7)) data = data + "&field7=" + field7
if (!isNaN(field8)) data = data + "&field8=" + field8
sendAtCmd("AT+CIPSEND=" + (data.length + 2))
result = waitAtResponse(">", "OK", "ERROR", 2000)
if (result == 3) continue
sendAtCmd(data)
result = waitAtResponse("SEND OK", "SEND FAIL", "ERROR", 5000)
// // close the TCP connection
// sendAtCmd("AT+CIPCLOSE")
// waitAtResponse("OK", "ERROR", "None", 2000)
if (result == 1) break
}
}
/**
* Send data to IFTTT
*/
//% block="Send Data to your IFTTT Event|Event %event|Key %key|value1 %value1||value2 %value2|value3 %value3"
//% block.loc.de="Sende Daten an dein IFTTT Event|Event %event|Key %key|Wert 1 %value1|Wert 2 %value2|Wert 3 %value3"
//% event.defl="your Event"
//% key.defl="your Key"
//% value1.defl="Hello"
//% value2.defl="Calliope"
//% value3.defl="mini"
export function sendToIFTTT(event: string, key: string, value1: string, value2: string, value3: string) {
let result = 0
let retry = 2
// close the previous TCP connection
if (isWifiConnected) {
sendAtCmd("AT+CIPCLOSE")
waitAtResponse("OK", "ERROR", "None", 2000)
}
while (isWifiConnected && retry > 0) {
retry = retry - 1;
// establish TCP connection
sendAtCmd("AT+CIPSTART=\"TCP\",\"maker.ifttt.com\",80")
result = waitAtResponse("OK", "ALREADY CONNECTED", "ERROR", 2000)
if (result == 3) continue
let data = "GET /trigger/" + event + "/with/key/" + key
data = data + "?value1=" + value1
data = data + "&value2=" + value2
data = data + "&value3=" + value3
data = data + " HTTP/1.1"
data = data + "\u000D\u000A"
data = data + "User-Agent: curl/7.58.0"
data = data + "\u000D\u000A"
data = data + "Host: maker.ifttt.com"
data = data + "\u000D\u000A"
data = data + "Accept: */*"
data = data + "\u000D\u000A"
sendAtCmd("AT+CIPSEND=" + (data.length + 2))
result = waitAtResponse(">", "OK", "ERROR", 2000)
if (result == 3) continue
sendAtCmd(data)
result = waitAtResponse("SEND OK", "SEND FAIL", "ERROR", 5000)
// close the TCP connection
// sendAtCmd("AT+CIPCLOSE")
// waitAtResponse("OK", "ERROR", "None", 2000)
if (result == 1) break
}
}
function waitAtResponse(target1: string, target2: string, target3: string, timeout: number) {
let buffer = ""
let start = input.runningTime()
while ((input.runningTime() - start) < timeout) {
buffer += serial.readString()
if (buffer.includes(target1)) return 1
if (buffer.includes(target2)) return 2
if (buffer.includes(target3)) return 3
basic.pause(100)
}
return 0
}
function sendAtCmd(cmd: string) {
serial.writeString(cmd + "\u000D\u000A")
}
}