-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP8266.ts
103 lines (92 loc) · 2.96 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
/**
* Use this file to define custom functions and blocks.
* Read more at https://makecode.microbit.org
*/
/**
* Custom blocks
*/
//% color=#0fbc11 icon="\uf1eb" weight=90
namespace ESP8266_IoT {
let tobesendstring = ""
/**
* TODO: Set pin RX and TX for ESP8266 Serial Wifi Module,Baud rate: 9600.
* @param wifiRX describe parameter here, eg: SerialPin.P2
* @param wifiTX describe parameter here, eg: SerialPin.P1
*/
//% weight=100
//% blockId="wifi_init" block="set ESP8266 RX %wifiRX| TX %wifiTX|at baud rate 9600"
export function initwifi(wifiRX: SerialPin, wifiTX: SerialPin): void {
serial.redirect(
wifiRX,
wifiTX,
BaudRate.BaudRate115200
)
basic.pause(10)
//serial.writeLine("AT")
serial.writeString("AT+CWMODE=1" + "\u000D" + "\u000A")
basic.pause(5000)
serial.writeString("AT+RST" + "\u000D" + "\u000A")
basic.pause(5000)
// Add code here
}
/**
* TODO: connectwifi,Fill in your ssid and your key.
* @param ssid describe parameter here, eg: "your ssid"
* @param key describe parameter here, eg: "your key"
*/
//% weight=99
//% blockId="wifi_connect" block="connect wifi SSDI: %ssid| KEY: %key"
export function connectwifi(ssid: string, key: string): void {
// Add code here
let text = "AT+CWJAP=\""
+ ssid
+ "\",\""
+ key
+ "\""
serial.writeString(text + "\u000D" + "\u000A")
basic.pause(7000)
}
/**
* TODO: connect thingspeak IoT TCP server
*/
//% weight=98
//% blockId="TCP_connect" block="connect thingspeak"
export function connectthingspeak(): void {
// Add code here
let text = "AT+CIPSTART=\"TCP\",\"184.106.153.149\",80"
serial.writeString(text + "\u000D" + "\u000A")
basic.pause(7000)
}
/**
* TODO: Set data to be sent.
* @param write_api_key describe parameter here, eg: "your write api key"
* @param n1 describe parameter here, eg: 0
*/
//% weight=97
//% blockId="send_text" block="set data to be send : Write API Key= %write_api_key|field1= %n1"
export function tosendtext(write_api_key: string,
n1: number ): void {
let text=""
text = "GET /update?key="
+ write_api_key
+ "&field1="
+ n1
tobesendstring = text
// Add code here
}
/**
* TODO: send data
*/
//% weight=96
//% blockId=senddata block="send data to thingspeak"
export function senddata(): void {
let text = ""
text = "AT+CIPSEND="
+ (tobesendstring.length + 2)
serial.writeString(text + "\u000D" + "\u000A")
basic.pause(6000)
serial.writeString(tobesendstring + "\u000D" + "\u000A")
basic.pause(6000)
// Add code here
}
}