-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathObloq.ts
1218 lines (1149 loc) · 38.1 KB
/
Obloq.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* @file Obloq/Obloq.ts
* @brief DFRobot's obloq makecode library.
* @n [Get the module here](http://www.dfrobot.com.cn/goods-1577.html)
* @n Obloq is a serial port of WIFI connection module, Obloq can connect
* to Microsoft Azure IoT and other standard MQTT protocol IoT.
*
* @copyright [DFRobot](http://www.dfrobot.com), 2016
* @copyright GNU Lesser General Public License
*
* @author [email]([email protected])
* @version V1.0
* @date 2018-03-20
*/
let DEBUG = true
let MQTT_DEFAULT = true
//DFRobot easy iot
const EASY_IOT_SERVER = "api.thingspeak.com"
const EASY_IOT_PORT = 80
//const EASY_IOT_PORT = 1883
//other iot
const USER_IOT_SERVER = "-----------"
const USER_IOT_PORT = 0
//wifi
let OBLOQ_SSID = ""
let OBLOQ_PASSWORD = ""
//mqtt
let OBLOQ_MQTT_PORT = 0
let OBLOQ_MQTT_SERVER = ""
let OBLOQ_IOT_PWD = ""
let OBLOQ_IOT_ID = ""
let OBLOQ_IOT_TOPIC = ""
//http
let OBLOQ_HTTP_IP = ""
let OBLOQ_HTTP_PORT = 80
//let OBLOQ_HTTP_PORT = 8080
//Connect to the WiFi IP address.
let IP = "192.168.2.81"
//Record state
let FIRST = true
let initmqtt = false
let defobloq = false
//http
let myip = ""
let myport = 80
//mqtt
let myhost = ""
let mymqport = 80
//callback function
let cb: Action
let mycb: Action
//Record commands and messages.
let e = ""
let param = ""
let diwifi = ""
//serial
let serialinit = false
//animation
let wifi_icon = 1
let mqtt_icon = 1
//serial pin
let Tx = SerialPin.P2
let Rx = SerialPin.P1
//event flag
let event = false
//mode
let mode = 0
/**
*Obloq implementation method.
*/
//% weight=10 color=#008B00 icon="\uf1eb" block="Obloq"
namespace Obloq {
const OBLOQ_STR_NONE = ""
const OBLOQ_SUCCE_OK = 0
const OBLOQ_SUCCE_ERR = 1
const OBLOQ_WIFI_CONNECT_FAILURE = -2
const OBLOQ_WIFI_CONNECT_TIMEOUT = -1
const OBLOQ_MQTT_SUBTOPIC_TIMEOUT = -3
const OBLOQ_MQTT_CONNECT_TIMEOUT = -4
const OBLOQ_MQTT_CONNECT_FAILURE = -5
const OBLOQ_MODE_MQTT = 2
const OBLOQ_MODE_HTTP = 3
const OBLOQ_TRUE = true
const OBLOQ_FALSE = false
export class Packeta {
/**
* Obloq receives commands.
*/
public mye: string;
/**
* Obloq receives the message content.
*/
public myparam: string;
}
//% advanced=true shim=Obloq::obloqreadString
function obloqreadString(size: number): string {
return OBLOQ_STR_NONE
}
//% advanced=true shim=Obloq::obloqgetRxBufferSize
function obloqgetRxBufferSize(): number {
return OBLOQ_SUCCE_OK
}
//% advanced=true shim=Obloq::obloqSetTxBufferSize
function obloqSetTxBufferSize(size: number): void {
return
}
//% advanced=true shim=Obloq::obloqSetRxBufferSize
function obloqSetRxBufferSize(size: number): void {
return
}
//% advanced=true shim=Obloq::obloqRxBufferedSize
function obloqRxBufferedSize(): number {
return OBLOQ_SUCCE_OK
}
//% advanced=true shim=Obloq::obloqEventAfter
function obloqEventAfter(len: number): void {
return
}
//% advanced=true shim=Obloq::obloqEventOn
function obloqEventOn(msg: string): void {
return
}
//% advanced=true shim=Obloq::obloqClearRxBuffer
function obloqClearRxBuffer(): void {
return
}
//% advanced=true shim=Obloq::obloqClearTxBuffer
function obloqClearTxBuffer(): void {
return
}
//% advanced=true shim=Obloq::obloqforevers
function obloqforevers(a: Action): void {
return
}
//% advanced=true shim=Obloq::obloqWriteString
function obloqWriteString(text: string): void {
return
}
//% advanced=true shim=Obloq::obloqDisDisplay
function obloqDisDisplay(): void {
return
}
//% advanced=true shim=Obloq::obloqEnDisplay
function obloqEnDisplay(): void {
return
}
function Obloq_wifiIconShow(): void {
switch (wifi_icon) {
case 1: {
basic.clearScreen()
led.plot(0, 4)
wifi_icon += 1
} break;
case 2: {
led.plot(0, 2)
led.plot(1, 2)
led.plot(2, 3)
led.plot(2, 4)
wifi_icon += 1
} break;
case 3: {
led.plot(0, 0)
led.plot(1, 0)
led.plot(2, 0)
led.plot(3, 1)
led.plot(4, 2)
led.plot(4, 3)
led.plot(4, 4)
wifi_icon = 1
} break;
}
}
function Obloq_mqttIconShow(): void {
switch (mqtt_icon) {
case 1: {
basic.clearScreen()
led.plot(4, 0)
mqtt_icon += 1
} break;
case 2: {
led.plot(2, 0)
led.plot(2, 1)
led.plot(3, 2)
led.plot(4, 2)
mqtt_icon += 1
} break;
case 3: {
led.plot(0, 0)
led.plot(0, 1)
led.plot(0, 2)
led.plot(1, 3)
led.plot(2, 4)
led.plot(3, 4)
led.plot(4, 4)
mqtt_icon = 1
} break;
}
}
function Obloq_serialInit(): void{
let item = ""
//First send data through usb, avoid the first data scrambled.
obloqWriteString("123")
item = serial.readString()
item = serial.readString()
item = serial.readString()
serial.redirect(
Tx,
Rx,
BaudRate.BaudRate9600
)
obloqSetTxBufferSize(300)
obloqSetRxBufferSize(300)
obloqWriteString("\r")
item = serial.readString()
serialinit = OBLOQ_TRUE
obloqClearRxBuffer()
obloqClearTxBuffer()
onEvent()
}
/**
* Two parallel stepper motors are executed simultaneously(DegreeDual).
* @param SSID to SSID ,eg: "yourSSID"
* @param PASSWORD to PASSWORD ,eg: "yourPASSWORD"
* @param IOT_ID to IOT_ID ,eg: "yourIotId"
* @param IOT_PWD to IOT_PWD ,eg: "yourIotPwd"
* @param IOT_TOPIC to IOT_TOPIC ,eg: "yourIotTopic"
* @param receive to receive ,eg: SerialPin.P1
* @param send to send ,eg: SerialPin.P2
*/
//% weight=102
//% blockId=Obloq_setup
//% block="Obloq setup | Wi-Fi: | Wi-Fi name : %SSID| Wi-Fi password: %PASSWORD| Iot service: | IOT_ID: %IOT_ID| IOT_PWD: %IOT_PWD| IOT_TOPIC: %IOT_TOPIC| Pin set: | Receiving data (green wire): %receive| Sending data (blue wire): %send"
export function Obloq_setup(/*wifi*/SSID: string, PASSWORD: string,
/*mqtt*/IOT_ID: string, IOT_PWD: string, IOT_TOPIC: string,
/*serial*/receive: SerialPin, send: SerialPin):
void {
OBLOQ_SSID = SSID
OBLOQ_PASSWORD = PASSWORD
if (MQTT_DEFAULT) {
OBLOQ_MQTT_SERVER = EASY_IOT_SERVER
OBLOQ_MQTT_PORT = EASY_IOT_PORT
} else {
OBLOQ_MQTT_SERVER = USER_IOT_SERVER
OBLOQ_MQTT_PORT = USER_IOT_PORT
}
OBLOQ_IOT_PWD = IOT_PWD
OBLOQ_IOT_ID = IOT_ID
OBLOQ_IOT_TOPIC = IOT_TOPIC
Tx = send
Rx = receive
Obloq_serialInit()
}
/**
* Disconnect the serial port.
*/
//% weight=97
//% blockGap=50
//% blockId=Obloq_quit
//% block="quit"
//% advanced=true
export function Obloq_quit(): void {
obloqWriteString("quit!\r")
}
/**
* Send the ping.time(ms): private long maxWait
* @param time to timeout, eg: 10000
*/
//% weight=49
//% blockId=Obloq_sendPing
//% block="sendPing timeout|%time"
//% advanced=true
export function Obloq_sendPing(time: number): string {
if (time < 100) {
time = 100
}
let timeout = time / 100
let _timeout = 0
if (!serialinit) {
Obloq_serialInit()
}
obloqWriteString("|1|1|\r")
if (!initmqtt) {
let num = 0
let item = ""
while (OBLOQ_TRUE) {
if (e == "Pingok") {
return "true"
} else if (e == "timeout") {
return "timeout"
}
basic.pause(100)
_timeout += 1
if (_timeout > timeout) {
return "timeout"
}
}
} else {
return "true"
}
return "true"
}
/**
* Get the software version.time(ms): private long maxWait
* @param time to timeout, eg: 10000
*/
//% weight=50
//% blockId=Obloq_getVersion
//% block="get version timeout %time"
//% advanced=true
export function Obloq_getVersion(time: number): string {
if (time < 100) {
time = 100
}
let timeout = time / 100
let _timeout = 0
if (!serialinit) {
Obloq_serialInit()
}
obloqWriteString("|1|2|\r")
if (!initmqtt) {
let num = 0
let item = ""
while (OBLOQ_TRUE) {
if (e == "getVersion") {
return param
} else if (e == "timeout") {
return "timeout"
}
basic.pause(100)
_timeout += 1
if (_timeout > timeout) {
return "timeout"
}
}
} else {
return "true"
}
return "true"
}
/**
* Heartbeat request.time(ms): private long maxWait
* @param time to timeout, eg: 10000
*/
//% weight=48
//% blockId=Obloq_getHeartbeat
//% block="get heartbeat timeout %time"
//% advanced=true
export function Obloq_getHeartbeat(time: number): string {
if (time < 100) {
time = 100
}
let timeout = time / 100
let _timeout = 0
if (!serialinit) {
Obloq_serialInit()
}
obloqWriteString("|1|3|" + time + "|\r")
if (!initmqtt) {
let num = 0
let item = ""
while (OBLOQ_TRUE) {
if (e == "Heartbeat") {
return param
} else if (e == "timeout") {
return "timeout"
}
basic.pause(100)
_timeout += 1
if (_timeout > timeout) {
return "timeout"
}
}
} else {
return "true"
}
return "true"
}
/**
* Stop the heartbeat request.
*/
//% weight=47
//% blockId=Obloq_stopHeartbeat
//% block="stop heartbeat"
//% advanced=true
export function Obloq_stopHeartbeat(): boolean {
if (!serialinit) {
Obloq_serialInit()
}
obloqWriteString("|1|3|-1000|\r")
return OBLOQ_TRUE
}
function Obloq_disconnectWifi(): void {
if (!serialinit) {
Obloq_serialInit()
}
}
/**
* Reconnect WiFi.time(ms): private long maxWait
* @param time to timeout, eg: 10000
*/
//% weight=99
//% blockId=Obloq_reconnectWifi
//% block="reconnectWifi timeout %time"
//% advanced=true
export function Obloq_reconnectWifi(time: number): boolean {
if (time < 100) {
time = 100
}
let timeout = time / 100
let _timeout = 0
if (!serialinit) {
Obloq_serialInit()
}
obloqWriteString("|2|3|\r")
if (!initmqtt) {
while (OBLOQ_TRUE) {
if (e == "WifiConnected") {
IP = param
return OBLOQ_TRUE
}
basic.pause(100)
_timeout += 1
if (_timeout > timeout) {
return OBLOQ_FALSE
}
}
} else {
return OBLOQ_TRUE
}
return OBLOQ_TRUE
}
/**
* connect Wifi.SSID(string):account; PWD(string):password;
* time(ms): private long maxWait
*/
//% weight=100
//% blockId=Obloq_startConnect
//% block="start connect"
export function Obloq_startConnect(): void {
mode = OBLOQ_MODE_MQTT
let ret = Obloq_connectWifi()
if (DEBUG) { basic.showNumber(ret) }
switch (ret) {
case OBLOQ_SUCCE_OK: {
basic.showIcon(IconNames.Yes)
basic.pause(500)
basic.clearScreen()
} break;
case OBLOQ_WIFI_CONNECT_TIMEOUT: {
Obloq_disconnectWifi()
diwifi = "PulishFailure"
return
} break;
case OBLOQ_WIFI_CONNECT_FAILURE: {
basic.showIcon(IconNames.No)
while (OBLOQ_TRUE) { basic.pause(10000) }
} break;
case OBLOQ_SUCCE_ERR: {
basic.showIcon(IconNames.No)
while (OBLOQ_TRUE) { basic.pause(10000) }
} break;
}
ret = Obloq_connectIot()
switch (ret) {
case OBLOQ_SUCCE_OK: {
initmqtt = OBLOQ_TRUE
basic.showIcon(IconNames.Yes)
basic.pause(500)
basic.clearScreen()
} break;
case OBLOQ_MQTT_SUBTOPIC_TIMEOUT: {
FIRST = OBLOQ_TRUE
Obloq_disconnectMqtt()
diwifi = "PulishFailure"
return
} break;
case OBLOQ_MQTT_CONNECT_TIMEOUT: {
FIRST = OBLOQ_TRUE
Obloq_disconnectMqtt()
diwifi = "PulishFailure"
return
} break;
case OBLOQ_MQTT_CONNECT_FAILURE: {
basic.showIcon(IconNames.No)
while (OBLOQ_TRUE) { basic.pause(10000) }
} break;
case OBLOQ_SUCCE_ERR: {
basic.showIcon(IconNames.No)
while (OBLOQ_TRUE) { basic.pause(10000) }
} break;
}
}
basic.forever(() => {
if (DEBUG) { led.plot(0, 0) }
basic.pause(150)
if (diwifi == "PulishFailure") {
if (mode == OBLOQ_MODE_MQTT) {
Obloq_startConnect()
}
if (initmqtt) {
diwifi = ""
}
}
if (DEBUG) { led.unplot(0, 0) }
basic.pause(150)
})
/**
* pin set
* @param receive to receive ,eg: SerialPin.P1
* @param send to send ,eg: SerialPin.P2
*/
//% weight=101
//% blockId=Obloq_pinSet
//% block="pin set| receive %SerialPin| send %SerialPin"
//% advanced=true
export function Obloq_pinSet(receive: SerialPin, send: SerialPin): void {
Tx = send
Rx = receive
Obloq_serialInit()
}
/**
* connect Wifi.SSID(string):account; PWD(string):password;
* @param SSID to SSID ,eg: "yourSSID"
* @param PASSWORD to PASSWORD ,eg: "yourPASSWORD"
*/
//% weight=100
//% blockId=Obloq_connectWifiExport
//% block="wifi connect to| SSID %SSID| PASSWORD %PASSWORD"
//% advanced=true
export function Obloq_connectWifiExport(SSID: string, PASSWORD: string): void {
OBLOQ_SSID = SSID
OBLOQ_PASSWORD = PASSWORD
Obloq_connectWifi()
}
function Obloq_connectWifi(): number {
wifi_icon = 1
let time = 10000
if (time < 100) {
time = 100
}
let timeout = time / 100
let _timeout = 0
if (FIRST) {
//serial init
if (!serialinit) {
Obloq_serialInit()
}
//show icon
Obloq_wifiIconShow()
for (let i = 0; i < 3; i++) {
obloqWriteString("|1|1|\r")
basic.pause(100)
}
obloqreadString(obloqgetRxBufferSize())
obloqWriteString("|2|1|" + OBLOQ_SSID + "," + OBLOQ_PASSWORD + "|\r")
FIRST = OBLOQ_FALSE
}
while (OBLOQ_TRUE) {
if ((_timeout+1) % 3 == 0) {
Obloq_wifiIconShow()
}
if (e == "WifiConnected") {
IP = param
return OBLOQ_SUCCE_OK
} else if (e == "DisConnected") {
return OBLOQ_WIFI_CONNECT_FAILURE
}
basic.pause(100)
_timeout += 1
if (_timeout > timeout) {
//basic.showIcon(IconNames.No)
return OBLOQ_WIFI_CONNECT_TIMEOUT
}
}
return OBLOQ_SUCCE_OK
}
/**
* Get IP address.
*/
//% weight=98
//% blockId=Obloq_Obloq_ifconfig
//% block="ipconfig"
//% advanced=true
export function Obloq_ipconfig(): string {
return IP
}
/**
* Set the HTTP parameters.ip(string):ip address;port(number):The port number.
* @param ip set ip addr, eg: 0.0.0.0
* @param port set port, eg: 8080
*/
//% weight=80
//% blockId=Obloq_initHttp
//% block="http set | ip %ip| port %port"
//% advanced=true
export function Obloq_initHttp(ip: string, port: number): void {
defobloq = OBLOQ_TRUE
myip = ip
myport = port
initmqtt = OBLOQ_FALSE
}
/**
* The HTTP get request.url(string):URL:time(ms): private long maxWait
* @param time set timeout, eg: 10000
*/
//% weight=79
//% blockId=Obloq_httpGet
//% block="http get | url %url| timeout %time"
//% advanced=true
export function Obloq_httpGet(url: string, time: number): string[] {
if (time < 100) {
time = 100
}
let timeout = time / 100
let _timeout = 0
if (!serialinit) {
Obloq_serialInit()
}
obloqWriteString("|3|1|http://" + myip + ":" + myport + "/" + url + "|\r")
//while((strncmp(buf,"|1|3|\r",strlen(buf)) == 0)){
// buf = readbuf(mp_obj_get_int(time));
// }
let item = ""
let num = 0
let j = 0
while (OBLOQ_TRUE) {
if (e == "200") {
let list = ["200", param]
return list
} else if (e == "err") {
let list = ["err", param]
return list
} else if (e == "|2|1|") {
let list = ["999", "disconnet wifi"]
return list
}
basic.pause(100)
_timeout += 1
if (_timeout > timeout) {
let list = ["408", "time out"]
return list
}
}
let list = ["408", "time out"]
return list
}
/**
* The HTTP post request.url(string): URL; content(string):content
* time(ms): private long maxWait
* @param time set timeout, eg: 10000
*/
//% weight=78
//% blockId=Obloq_httpPost
//% block="http post | url %url| content %content| timeout %time"
//% advanced=true
export function Obloq_httpPost(url: string, content: string, time: number): string[] {
if (time < 100) {
time = 100
}
let timeout = time / 100
let _timeout = 0
if (!serialinit) {
Obloq_serialInit()
}
obloqWriteString("|3|2|http://" + myip + ":" + myport + "/" + url + "," + content + "|\r")
//while((strncmp(buf,"|1|3|\r",strlen(buf)) == 0)){
// buf = readbuf(mp_obj_get_int(time));
// }
let item = ""
let num = 0
let j = 0
while (OBLOQ_TRUE) {
if (e == "200") {
let list = ["200", param]
return list
} else if (e == "err") {
let list = ["err", param]
return list
} else if (e == "|2|1|") {
let list = ["999", "disconnet wifi"]
return list
}
basic.pause(100)
_timeout += 1
if (_timeout > timeout) {
let list = ["408", "time out"]
return list
}
}
let list = ["408", "time out"]
return list
}
/**
* The HTTP put request,Obloq.put() can only be used for http protocol!
* url(string): URL; content(string):content; time(ms): private long maxWait
* @param time set timeout, eg: 10000
*/
//% weight=77
//% blockId=Obloq_httpPut
//% block="http put | url %url| content %content| timeout %time"
//% advanced=true
export function Obloq_httpPut(url: string, content: string, time: number): string[] {
if (time < 100) {
time = 100
}
let timeout = time / 100
let _timeout = 0
if (!serialinit) {
Obloq_serialInit()
}
obloqWriteString("|3|3|http://"+myip+":"+myport+"/"+url+","+content+"|\r")
let item = ""
let num = 0
let j = 0
while (OBLOQ_TRUE) {
if (e == "200") {
let list = ["200", param]
return list
} else if (e == "err") {
let list = ["err", param]
return list
} else if (e == "|2|1|") {
let list = ["999", "disconnet wifi"]
return list
}
basic.pause(100)
_timeout += 1
if (_timeout > timeout) {
let list = ["408", "time out"]
return list
}
}
let list = ["408", "time out"]
return list
}
/**
* Delete an HTTP connection.url(string): URL; content(string):content
* time(ms): private long maxWait
* @param time set timeout, eg: 10000
*/
//% weight=76
//% blockGap=50
//% blockId=Obloq_httpDelete
//% block="http delete | url %url| content %content| timeout %time"
//% advanced=true
export function Obloq_httpDelete(url: string, content: string, time: number): string[] {
if (time < 100) {
time = 100
}
let timeout = time / 100
let _timeout = 0
if (!serialinit) {
Obloq_serialInit()
}
obloqWriteString("|3|4|http://"+myip+":"+myport+"/"+url+","+content+"|\r")
let item = ""
let num = 0
let j = 0
while (OBLOQ_TRUE) {
if (e == "200") {
let list = ["200", param]
return list
} else if (e == "err") {
let list = ["err", param]
return list
} else if (e == "|2|1|") {
let list = ["999", "disconnet wifi"]
return list
}
basic.pause(100)
_timeout += 1
if (_timeout > timeout) {
let list = ["408", "time out"]
return list
}
}
let list = ["408", "time out"]
return list
}
function Obloq_connectMqtt(): void {
if (!serialinit) {
Obloq_serialInit()
}
obloqWriteString("|4|1|1|" + myhost + "|" + mymqport + "|" + OBLOQ_IOT_ID + "|" + OBLOQ_IOT_PWD + "|\r")
}
function Obloq_connectIot(): number {
mqtt_icon = 1
let iconnum = 0
let _timeout = 0
defobloq = OBLOQ_TRUE
myhost = OBLOQ_MQTT_SERVER
mymqport = OBLOQ_MQTT_PORT
mycb = cb
Obloq_connectMqtt()
while (_timeout < 2000) {
if (_timeout % 50 == 0) {
Obloq_mqttIconShow()
iconnum += 1;
}
if (e == "MqttConneted") {
break
} else if (e == "ConnectErr") {
OBLOQ_MQTT_CONNECT_FAILURE
}
basic.pause(1)
_timeout += 1
}
if (_timeout >= 2000) {
//basic.showString("timeout!")
return OBLOQ_MQTT_CONNECT_TIMEOUT
}
Obloq_subTopic()
let __timeout = _timeout + 2000
while (_timeout < __timeout) {
if (_timeout % 50 == 0) {
Obloq_mqttIconShow()
iconnum += 1
}
if (iconnum > 6) {//动画两次以上
if (e == "SubOk") {
break
}
}
basic.pause(1)
_timeout += 1
}
if (_timeout >= __timeout) {
//basic.showString("timeout!")
return OBLOQ_MQTT_SUBTOPIC_TIMEOUT
}
return OBLOQ_SUCCE_OK
//basic.showString("ok")
}
/**
* Reconnect the MQTT.
*/
//% weight=65
//% blockId=Obloq_reconnectMqtt
//% block="mqtt reconnect"
//% advanced=true
export function Obloq_reconnectMqtt(): void {
if (!serialinit) {
Obloq_serialInit()
}
obloqWriteString("|4|1|5|\r")
}
/**
* Disconnect the MQTT connection.
*/
//% weight=66
//% blockId=Obloq_disconnectMqtt
//% block="mqtt disconnect"
//% advanced=true
export function Obloq_disconnectMqtt(): void {
if (!serialinit) {
Obloq_serialInit()
}
}
/**
* Send a message.
* @param top set top, eg: top
* @param mess set mess, eg: mess
*/
//% weight=68
//% blockId=Obloq_sendMessage
//% block="pubLish | %mess"
export function Obloq_sendMessage(mess: string): void {
if (!initmqtt) {
return
}
if (!serialinit) {
Obloq_serialInit()
}
obloqWriteString("|4|1|3|"+OBLOQ_IOT_TOPIC+"|"+mess+"|\r")
}
/**
* Subscribe to a Topic
* @param top set top, eg: top
*/
//% weight=67
//% blockId=Obloq_subTopic
//% block="subTopic"
//% advanced=true
export function Obloq_subTopic(): void {
if (!serialinit) {
Obloq_serialInit()
}
obloqWriteString("|4|1|2|"+OBLOQ_IOT_TOPIC+"|\r")
}
function obloq_mqttCallback(a: Action): void{
cb = a
}
/**
* This is an MQTT listener callback function, which is very important.
* The specific use method can refer to "example/ObloqMqtt.ts"
*/
//% weight=62
//% blockGap=50
//% mutate=objectdestructuring
//% mutateText=Packeta
//% mutateDefaults="myparam:message"
//% blockId=obloq_mqttCallbackUser block="on obloq received"
export function obloq_mqttCallbackUser(cb: (packet: Packeta) => void) {
obloq_mqttCallback(() => {
const packet = new Packeta();
packet.mye = e
packet.myparam = param;
cb(packet)
});
}
function obloqRecevice(): void {
let size = obloqRxBufferedSize()
//serial.writeNumber(size)
if (size > 5) { // serial.writeNumber(1);
let item = obloqreadString(size)
//if (size > 10) {serial.writeString(item) }
for (let i = 0; i < size; i++) {
if (item.charAt(i) == '1') {
if (item.charAt(i + 1) == '|') {
if (item.charAt(i + 2) == '1') { //|1|1|
e = "Pingok"
param = ""
return
} else if (item.charAt(i + 2) == '2') { //|1|2|
let z = 0
let j = i + 4
for (i = i + 4; i < size; i++) {
if (item.charAt(i) == '|') {
break;
} else {
z = z + 1
}
}