-
Notifications
You must be signed in to change notification settings - Fork 0
/
mIoT.ts
182 lines (158 loc) · 5.28 KB
/
mIoT.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
//% color=#45B39D icon="\uf1eb" block="mIoT"
//% groups="['WiFi', 'Thingspeak', 'Dashboard']"
namespace mIoT {
//debug
const debug = false
//network type
const netTypeIsSucc = 0
const netTypeIsErr = 1
const netTypeIsWifiConnectTimeout = -1
const netTypeIsWifiConnectFailure = -2
//data type
const strTypeIsNone = ""
const boolTypeIsTrue = true
const boolTypeIsFalse = false
//serial
let mIoTSerialInited = boolTypeIsFalse
let mIoTSerialTX = SerialPin.P15
let mIoTSerialRX = SerialPin.P16
//wifi
let mIoTWifiSSID = strTypeIsNone
let mIoTWifiPSWD = strTypeIsNone
let mIoTWifiIP = "0.0.0.0"
//web
let mIoTWebSSID = strTypeIsNone
let mIoTWebPSWD = strTypeIsNone
//state
let mIoTWifiConnected = boolTypeIsFalse
//msg
let msg = strTypeIsNone
function mIoTSerialInit(): void{
showSerialNO()
serial.redirect(
mIoTSerialTX,
mIoTSerialRX,
BaudRate.BaudRate115200
)
mIoTWriteString("Connected", 100)
while(msg.substr(0,6) != "ConnOK"){
msg = strTypeIsNone
mIoTReadString()
}
showSerialOK()
mIoTSerialInited = boolTypeIsTrue
}
function mIoTWriteString(text: string, waitTime: number=10): void {
serial.writeString(text)
basic.pause(waitTime)
}
function mIoTReadString(): void {
while(msg == strTypeIsNone) {
msg = serial.readLine()
}
}
function mIoTSetSSIDPW(ssid: string, password: string): void {
mIoTWifiSSID = ssid
mIoTWifiPSWD = password
}
//% blockId=mIoTWifiConnect block="Wifi connect to|SSID|%ssid|PASSWORD|%password" group="WiFi"
export function mIoTWifiConnect(ssid: string="", password: string=""): void {
if (ssid != strTypeIsNone || password != strTypeIsNone) {
mIoTSetSSIDPW(ssid, password)
}
if (mIoTWifiConnected == boolTypeIsTrue) {
return
}
if (!mIoTSerialInited) {
mIoTSerialInit()
}
if (mIoTSerialInited) {
mIoTWriteString("SSID:" + mIoTWifiSSID, 1000)
mIoTWriteString("PSWD:" + mIoTWifiPSWD, 1000)
msg = strTypeIsNone
mIoTReadString()
if (msg.substr(0,6) == "WIFIOK") {
basic.showLeds(`
# # # . .
. . . # .
# # . . #
. . # . #
# . # . #
`)
mIoTWifiConnected = boolTypeIsTrue
}
if (msg.substr(0,6) == "WIFITO") {
basic.showLeds(`
. . . . .
. . . . .
# . # . .
. # . . .
# . # . .
`)
}
}
}
//% blockId=mIoTWifiReconnect block="Wifi Reconnect" group="WiFi"
export function mIoTWifiReconnect(): void {
if (mIoTWifiConnected == boolTypeIsFalse) {
mIoTWifiConnect(strTypeIsNone, strTypeIsNone)
}
}
//% blockId=mIoTWifiStatus block="Wifi Status" group="WiFi"
export function mIoTWifiStatus(): boolean {
return mIoTWifiConnected
}
function showSerialNO() {
basic.showIcon(IconNames.No)
}
function showSerialOK() {
basic.showIcon(IconNames.Yes)
}
//% weight=30 block="Write Thingspeak| apiKey(write) %apiKey| text %text" group="Thingspeak"
export function writeThingspeak(apiKey: string, text: string) {
mIoTWifiReconnect()
mIoTWriteString("TKEY:" + apiKey, 100)
mIoTWriteString("TTXT:" + text, 100)
mIoTWriteString("TWRGO", 100)
}
//% weight=20 block="Write Thingspeak| apiKey(write) %apiKey| field1 %value1" group="Thingspeak"
export function writeThingspeakv1(apiKey: string, value1: number) {
mIoTWifiReconnect()
mIoTWriteString("TKEY:" + apiKey, 100)
mIoTWriteString("VAL1:" + value1, 100)
mIoTWriteString("TWRGO", 100)
}
//% weight=10 block="Write Thingspeak| apiKey(write) %apiKey| field1 %value1| field2 %value2" group="Thingspeak"
export function writeThingspeakv2(apiKey: string, value1: number, value2: number) {
mIoTWifiReconnect()
mIoTWriteString("TKEY:" + apiKey, 100)
mIoTWriteString("VAL1:" + value1, 100)
mIoTWriteString("VAL2:" + value2, 100)
mIoTWriteString("TWRGO", 100)
}
function mIoTSetWebSSIDPW(ssid: string, password: string): void {
mIoTWebSSID = ssid
mIoTWebPSWD = password
}
//% block="Web Dashboard Init| SSID %wSSID| Password %wPassword" group="Dashboard" advanced=true
export function mIoTWebDashboard(ssid: string, password: string) {
if (!mIoTSerialInited) {
mIoTSerialInit()
}
mIoTSetWebSSIDPW(ssid, password)
mIoTWriteString("WSID:" + ssid, 100)
mIoTWriteString("WPWD:" + password, 100)
mIoTWriteString("WDBGO", 100)
while(msg.substr(0,5) != "WebOK"){
msg = strTypeIsNone
mIoTReadString()
}
basic.showLeds(`
. # # # .
# . # . #
# # # # #
# . # . #
. # # # .
`)
}
}