-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
2093 lines (1955 loc) · 74.6 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
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
/**
*Obloq implementation method.
................
*/
//////////////////////////////////// !add //////////////////////////////////////
//debug
const OBLOQ_DEBUG = false
const OBLOQ_MQTT_DEFAULT_SERVER = true
//DFRobot easy iot
const OBLOQ_MQTT_EASY_IOT_SERVER_CHINA = "iot.dfrobot.com.cn"
const OBLOQ_MQTT_EASY_IOT_SERVER_GLOBAL = "iot.dfrobot.com"
const OBLOQ_MQTT_EASY_IOT_PORT = 1883
//other iot
const OBLOQ_MQTT_USER_IOT_SERVER = "---.-----.---"
const OBLOQ_MQTT_USER_IOT_PORT = 0
//topic max number
const OBLOQ_MQTT_TOPIC_NUM_MAX = 5
//wrong type
const OBLOQ_ERROR_TYPE_IS_SUCCE = 0
const OBLOQ_ERROR_TYPE_IS_ERR = 1
const OBLOQ_ERROR_TYPE_IS_WIFI_CONNECT_TIMEOUT = -1
const OBLOQ_ERROR_TYPE_IS_WIFI_CONNECT_FAILURE = -2
const OBLOQ_ERROR_TYPE_IS_MQTT_SUBTOPIC_TIMEOUT = -3
const OBLOQ_ERROR_TYPE_IS_MQTT_CONNECT_TIMEOUT = -4
const OBLOQ_ERROR_TYPE_IS_MQTT_CONNECT_FAILURE = -5
const OBLOQ_ERROR_TYPE_IS_MQTT_SUBTOPIC_FAILURE = -6
//topics name
enum TOPIC {
topic_1 = 1,
topic_2 = 2,
topic_3 = 3,
topic_4 = 4
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
//% weight=10 color=#096670 icon="\uf1eb" block="Obloq_http"
//% groups=["04_IFTTT","03_ThingSpeak", "02_Weather", "01_System"]
namespace Obloq_http {
let wInfo: string[][] = [
["weather", "main", "", "s"],
["description", "description", "", "s"],
["temperature", "\"temp\"", "", "k"],
["humidity", "dity", "", "n"],
["temp_min", "temp_min", "", "k"],
["temp_max", "temp_max", "", "k"],
["speed", "speed", "", "n"],
["sunrise", "sunrise", "", "n"],
["sunset", "sunset", "", "n"],
["timezone", "timezone", "", "n"],
["cityName", "name", "", "s"]
]
export enum wType {
//% block="city name"
cityName = 10,
//% block="weather"
weather = 0,
//% block="description"
description = 1,
//% block="temperature"
temperature = 2,
//% block="humidity"
humidity = 3,
//% block="low temperature"
temp_min = 4,
//% block="maximum temperature"
temp_max = 5,
//% block="wind speed"
speed = 6,
//% block="time of sunrise"
sunrise = 7,
//% block="time of sunset"
sunset = 8
}
//serial
let OBLOQ_SERIAL_INIT = false
let OBLOQ_SERIAL_TX = SerialPin.P2
let OBLOQ_SERIAL_RX = SerialPin.P1
//wifi
let OBLOQ_WIFI_CONNECTED = false
let OBLOQ_WIFI_SSID = ""
let OBLOQ_WIFI_PASSWORD = ""
let OBLOQ_IP = ""
//////////////////////////////////// !add //////////////////////////////////////
//mqtt
let OBLOQ_MQTT_PORT = 0
let OBLOQ_MQTT_SERVER = ""
let OBLOQ_MQTT_PWD = ""
let OBLOQ_MQTT_ID = ""
let OBLOQ_MQTT_TOPIC = [["x","false"],["x","false"],["x","false"],["x","false"],["x","false"]]
//http
let OBLOQ_HTTP_IP = ""
let OBLOQ_HTTP_PORT = 8080
let OBLOQ_HTTP_BUSY = false
//state
let OBLOQ_WIFI_CONNECTED = false
let OBLOQ_WIFI_CONNECT_FIRST = true
let OBLOQ_MQTT_INIT = false
let OBLOQ_HTTP_INIT = false
//callback
let OBLOQ_MQTT_CB: Action[] = [null, null, null, null, null]
//commands
let OBLOQ_ANSWER_CMD = ""
let OBLOQ_ANSWER_CONTENT = ""
let OBLOQ_WRONG_TYPE = ""
//animation
let OBLOQ_WIFI_ICON = 1
let OBLOQ_MQTT_ICON = 1
//event
let OBLOQ_MQTT_EVENT = false
//mode
let OBLOQ_WORKING_MODE_IS_MQTT = false
let OBLOQ_WORKING_MODE_IS_HTTP = false
let OBLOQ_WORKING_MODE_IS_STOP = true
///////////////////////////////////////////////////////////////////////////////////////////////////
//keys
let cityID = ""
let weatherKey = ""
let aa = 0
export enum cityIDs {
//% block="Taipei"
Taipei = 1668341,
//% block="Hong Kong"
HongKong = 1819729,
//% block="Tokyo"
Tokyo = 1850147,
//% block="Seoul"
Seoul = 1835848,
//% block="Beijing"
Beijing=1816670,
//% block="Shanghai"
Shanghai=1796236,
//% block="Singapore"
Singapore=1880252,
//% block="London"
London=2643743,
//% block="Berlin"
Berlin=2950159,
//% block="Paris"
Paris= 2988507,
//% block="New York"
NewYork=5128638,
//% block="Sydney"
Sydney=2147714
}
export enum city2IDs {
//% block="Keelung"
Keelung = 6724654,
//% block="Taipei"
Taipei = 1668341,
//% block="Xinbei"
Xinbei = 1670029,
//% block="Taoyuan"
Taoyuan = 1667905,
//% block="Hsinchu"
Hsinchu = 1675107,
//% block="Miaoli"
Miaoli = 1671971,
//% block="Taichung"
Taichung = 1668399,
//% block="Changhua"
Changhua = 1679136,
//% block="Nantou"
Nantou = 1671564,
//% block="Yunlin"
Yunlin = 1665194,
//% block="Jiayi"
Jiayi = 1678836,
//% block="Tainan"
Tainan = 1668352,
//% block="Kaohsiung"
Kaohsiung = 7280289,
//% block="Pingtung"
Pingtung = 1670479,
//% block="Yilan"
Yilan = 1674197,
//% block="Hualien"
Hualien = 1674502,
//% block="Taitung"
Taitung = 1668295,
//% block="Penghu"
Penghu = 1670651,
//% block="Jincheng"
Jincheng = 1678008,
//% block="Nangan"
Nangan = 7552914
}
//////////////////////////////////// !add //////////////////////////////////////
export enum SERVERS {
//% blockId=SERVERS_China block="China"
China,
//% blockId=SERVERS_Global block="Global"
Global
}
export class PacketaMqtt {
/**
* Obloq receives commands.
*/
public topic: string;
/**
* Obloq receives the message content.
*/
public message: string;
}
export class PacketaHttp {
/**
* Obloq receives commands.
*/
public err: string;
/**
* Obloq receives the message content.
*/
public message: string;
}
//% advanced=true shim=Obloq::obloqreadString
function obloqreadString(size: number): string {
return ""
}
//% advanced=true shim=Obloq::obloqgetRxBufferSize
function obloqgetRxBufferSize(): number {
return OBLOQ_ERROR_TYPE_IS_SUCCE
}
//% 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_ERROR_TYPE_IS_SUCCE
}
//% 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
}
///////////////////////////////////////////////////////////////////////////////////////////////////
function obloqWriteString(text: string): void {
serial.writeString(text)
}
///////////////////////////////////// !add //////////////////////////////////////
//% advanced=true shim=Obloq::obloqDisDisplay
function obloqDisDisplay(): void {
return
}
//% advanced=true shim=Obloq::obloqEnDisplay
function obloqEnDisplay(): void {
return
}
function Obloq_wifi_icon_display(): void {
switch (OBLOQ_WIFI_ICON) {
case 1: {
basic.clearScreen()
led.plot(0, 4)
OBLOQ_WIFI_ICON += 1
} break;
case 2: {
led.plot(0, 2)
led.plot(1, 2)
led.plot(2, 3)
led.plot(2, 4)
OBLOQ_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)
OBLOQ_WIFI_ICON = 1
} break;
}
}
function Obloq_mqtt_icon_display(): void {
switch (OBLOQ_MQTT_ICON) {
case 1: {
basic.clearScreen()
led.plot(4, 0)
OBLOQ_MQTT_ICON += 1
} break;
case 2: {
led.plot(2, 0)
led.plot(2, 1)
led.plot(3, 2)
led.plot(4, 2)
OBLOQ_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)
OBLOQ_MQTT_ICON = 1
} break;
}
}
function Obloq_mark_reset(type: string): void {
if (type == "wifi") {
OBLOQ_WIFI_IP = "0.0.0.0"
OBLOQ_WIFI_CONNECT_FIRST = true
OBLOQ_WIFI_CONNECTED = false
}
OBLOQ_MQTT_INIT = false
OBLOQ_HTTP_INIT = false
OBLOQ_WIFI_ICON = 1
OBLOQ_WIFI_ICON = 1
for (let i = 0; i < OBLOQ_MQTT_TOPIC_NUM_MAX; i++) {
OBLOQ_MQTT_TOPIC[i][1] = "false";
}
led.stopAnimation()
basic.clearScreen()
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/*
function startWork():void{
basic.clearScreen()
led.plot(1, 2)
led.plot(2, 2)
led.plot(3, 2)
}
*/
function Obloq_serial_init(): void {
obloqWriteString("123")
let item = serial.readString()
item = serial.readString()
item = serial.readString()
item = serial.readString()
serial.redirect(
OBLOQ_SERIAL_TX,
OBLOQ_SERIAL_RX,
BaudRate.BaudRate9600
)
serial.setRxBufferSize(200)
serial.setTxBufferSize(100)
obloqWriteString("\r")
item = serial.readString()
obloqWriteString("|1|1|\r")
item = serial.readUntil("\r")
item = serial.readString()
item = serial.readString()
item = serial.readString()
item = serial.readString()
OBLOQ_SERIAL_INIT = true
OBLOQ_SERIAL_INIT = true
obloqClearRxBuffer()
obloqClearTxBuffer()
onEvent()
}
function getTimeStr(myTimes: number): string {
let myTimeStr = ""
let secs = myTimes % 60
let mins = Math.trunc(myTimes / 60)
let hours = Math.trunc(mins / 60)
mins = mins % 60
hours = hours % 24
if (hours < 10)
myTimeStr = "0" + hours
else
myTimeStr = "" + hours
myTimeStr += ":"
if (mins < 10)
myTimeStr = myTimeStr + "0" + mins
else
myTimeStr = myTimeStr + mins
myTimeStr += ":"
if (secs < 10)
myTimeStr = myTimeStr + "0" + secs
else
myTimeStr = myTimeStr + secs
return myTimeStr
}
function Obloq_connect_wifi(): void {
if (OBLOQ_SERIAL_INIT) {
if (!OBLOQ_SERIAL_INIT) {
Obloq_serial_init()
}
let item = "test"
obloqWriteString("|2|1|" + OBLOQ_WIFI_SSID + "," + OBLOQ_WIFI_PASSWORD + "|\r") //Send wifi account and password instructions
item = serial.readUntil("\r")
while (item.indexOf("|2|3|") < 0) {
item = serial.readUntil("\r")
}
OBLOQ_IP = item.substr(5, item.length - 6)
OBLOQ_WIFI_CONNECTED = true
basic.showIcon(IconNames.Yes)
}
}
///////////////////////////////////// !add //////////////////////////////////////
function Obloq_start_connect_http(): void {
OBLOQ_WORKING_MODE_IS_HTTP = true
let ret = Obloq_connect_wifi()
if (OBLOQ_DEBUG) { basic.showNumber(ret) }
switch (ret) {
case OBLOQ_ERROR_TYPE_IS_SUCCE: {
basic.showIcon(IconNames.Yes)
basic.pause(500)
basic.clearScreen()
OBLOQ_WIFI_CONNECTED = true
} break
case OBLOQ_ERROR_TYPE_IS_WIFI_CONNECT_TIMEOUT: {
basic.showIcon(IconNames.No)
basic.pause(500)
OBLOQ_WRONG_TYPE = "wifi connect timeout"
return
} break
case OBLOQ_ERROR_TYPE_IS_WIFI_CONNECT_FAILURE: {
basic.showIcon(IconNames.No)
basic.pause(500)
OBLOQ_WRONG_TYPE = "wifi connect failure"
return
} break
case OBLOQ_ERROR_TYPE_IS_ERR: {
basic.showNumber(ret)
basic.showIcon(IconNames.No)
while (true) { basic.pause(10000) }
} break
}
OBLOQ_HTTP_INIT = true
OBLOQ_WORKING_MODE_IS_STOP = false
}
function Obloq_start_connect_mqtt(SERVER: SERVERS, connectStart: string): void {
OBLOQ_WORKING_MODE_IS_MQTT = true
if (OBLOQ_MQTT_DEFAULT_SERVER) {
if (SERVER == SERVERS.China) {
OBLOQ_MQTT_SERVER = OBLOQ_MQTT_EASY_IOT_SERVER_CHINA
} else if (SERVER == SERVERS.Global) {
OBLOQ_MQTT_SERVER = OBLOQ_MQTT_EASY_IOT_SERVER_GLOBAL
}
OBLOQ_MQTT_PORT = OBLOQ_MQTT_EASY_IOT_PORT
} else {
OBLOQ_MQTT_SERVER = OBLOQ_MQTT_USER_IOT_SERVER
OBLOQ_MQTT_PORT = OBLOQ_MQTT_USER_IOT_PORT
}
let ret = 0
if (connectStart == "connect wifi") {
ret = Obloq_connect_wifi()
if (OBLOQ_DEBUG) { basic.showNumber(ret) }
switch (ret) {
case OBLOQ_ERROR_TYPE_IS_SUCCE: {
basic.showIcon(IconNames.Yes)
basic.pause(500)
basic.clearScreen()
OBLOQ_WIFI_CONNECTED = true
} break
case OBLOQ_ERROR_TYPE_IS_WIFI_CONNECT_TIMEOUT: {
basic.showIcon(IconNames.No)
basic.pause(500)
OBLOQ_WRONG_TYPE = "wifi connect timeout"
return
} break
case OBLOQ_ERROR_TYPE_IS_WIFI_CONNECT_FAILURE: {
basic.showIcon(IconNames.No)
basic.pause(500)
OBLOQ_WRONG_TYPE = "wifi connect failure"
return
} break
case OBLOQ_ERROR_TYPE_IS_ERR: {
basic.showNumber(ret)
basic.showIcon(IconNames.No)
while (true) { basic.pause(10000) }
} break
}
}
if (connectStart == "connect wifi" || connectStart == "connect mqtt") {
ret = Obloq_connect_iot();
switch (ret) {
case OBLOQ_ERROR_TYPE_IS_SUCCE: {
basic.showIcon(IconNames.Yes)
basic.pause(500)
basic.clearScreen()
} break
case OBLOQ_ERROR_TYPE_IS_MQTT_SUBTOPIC_TIMEOUT: {
basic.showIcon(IconNames.No)
basic.pause(500)
OBLOQ_WRONG_TYPE = "mqtt subtopic timeout"
return
} break
case OBLOQ_ERROR_TYPE_IS_MQTT_SUBTOPIC_FAILURE: {
basic.showIcon(IconNames.No)
basic.pause(500)
OBLOQ_WRONG_TYPE = "mqtt subtopic failure"
return
} break
case OBLOQ_ERROR_TYPE_IS_MQTT_CONNECT_TIMEOUT: {
basic.showIcon(IconNames.No)
basic.pause(500)
OBLOQ_WRONG_TYPE = "mqtt connect timeout"
return
} break
case OBLOQ_ERROR_TYPE_IS_MQTT_CONNECT_FAILURE: {
basic.showIcon(IconNames.No)
basic.pause(500)
OBLOQ_WRONG_TYPE = "mqtt connect failure"
return
} break
case OBLOQ_ERROR_TYPE_IS_ERR: {
basic.showNumber(ret)
basic.showIcon(IconNames.No)
while (true) { basic.pause(10000) }
} break
}
}
OBLOQ_MQTT_INIT = true
OBLOQ_WORKING_MODE_IS_STOP = false
}
basic.forever(() => {
if (OBLOQ_DEBUG) { led.plot(0, 0) }
basic.pause(150)
if ((OBLOQ_WRONG_TYPE == "wifi disconnect" ) ||
(OBLOQ_WRONG_TYPE == "wifi connect timeout" ) ||
(OBLOQ_WRONG_TYPE == "wifi connect failure" ) ||
(OBLOQ_WRONG_TYPE == "mqtt pulish failure" ) ||
(OBLOQ_WRONG_TYPE == "mqtt subtopic timeout") ||
(OBLOQ_WRONG_TYPE == "mqtt subtopic failure") ||
(OBLOQ_WRONG_TYPE == "mqtt connect timeout" ) ||
(OBLOQ_WRONG_TYPE == "mqtt connect failure" ))
{
OBLOQ_WORKING_MODE_IS_STOP = true
let type = "wifi"//OBLOQ_WRONG_TYPE.substr(0,4)
Obloq_mark_reset(type)
if (OBLOQ_DEBUG) { basic.showString(OBLOQ_WRONG_TYPE) }
if (OBLOQ_WORKING_MODE_IS_MQTT) {
if (OBLOQ_MQTT_SERVER = OBLOQ_MQTT_EASY_IOT_SERVER_CHINA) {
Obloq_start_connect_mqtt(SERVERS.China, "connect " + type)
} else if (OBLOQ_MQTT_SERVER = OBLOQ_MQTT_EASY_IOT_SERVER_GLOBAL) {
Obloq_start_connect_mqtt(SERVERS.Global, "connect " + type)
} else {
//do nothing
}
if (OBLOQ_MQTT_INIT) {
OBLOQ_WRONG_TYPE = ""
OBLOQ_WORKING_MODE_IS_STOP = false
}
}
if (OBLOQ_WORKING_MODE_IS_HTTP) {
Obloq_start_connect_http()
if (OBLOQ_HTTP_INIT) {
OBLOQ_WRONG_TYPE = ""
OBLOQ_WORKING_MODE_IS_STOP = false
}
}
}
if (OBLOQ_DEBUG) { led.unplot(0, 0) }
basic.pause(150)
})
/**
* Two parallel stepper motors are executed simultaneously(DegreeDual).
* @param SSID to SSID ,eg: "yourSSID"
* @param PASSWORD to PASSWORD ,eg: "yourPASSWORD"
* @param IP to IP ,eg: "0.0.0.0"
* @param PORT to PORT ,eg: 80
* @param receive to receive ,eg: SerialPin.P1
* @param send to send ,eg: SerialPin.P2
*/
//% weight=99
//% receive.fieldEditor="gridpicker" receive.fieldOptions.columns=3
//% send.fieldEditor="gridpicker" send.fieldOptions.columns=3
//% blockId=Obloq_http_setup
//% block="Obloq setup http | Pin set: | receiving data (green wire): %receive| sending data (blue wire): %send | Wi-Fi: | name: %SSID| password: %PASSWORD| http config: | ip: %IP| port: %PORT | start connection"
export function Obloq_http_setup(/*serial*/receive: SerialPin, send: SerialPin,
/*wifi*/SSID: string, PASSWORD: string,
/*mqtt*/IP: string, PORT: number):
void {
OBLOQ_WIFI_SSID = SSID
OBLOQ_WIFI_PASSWORD = PASSWORD
OBLOQ_HTTP_IP = IP
OBLOQ_HTTP_PORT = PORT
OBLOQ_SERIAL_TX = send
OBLOQ_SERIAL_RX = receive
Obloq_serial_init()
Obloq_start_connect_http()
}
/**
* 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
//% receive.fieldEditor="gridpicker" receive.fieldOptions.columns=3
//% send.fieldEditor="gridpicker" send.fieldOptions.columns=3
//% SERVER.fieldEditor="gridpicker" SERVER.fieldOptions.columns=2
//% blockId=Obloq_mqtt_setup
//% block="Obloq setup mqtt | Pin set: | receiving data (green wire): %receive| sending data (blue wire): %send | Wi-Fi: | name: %SSID| password: %PASSWORD| IoT service: | Iot_id: %IOT_ID| Iot_pwd: %IOT_PWD| (default topic_0) Topic: %IOT_TOPIC | start connection: | Servers: %SERVER"
export function Obloq_mqtt_setup(/*serial*/receive: SerialPin, send: SerialPin,
/*wifi*/SSID: string, PASSWORD: string,
/*mqtt*/IOT_ID: string, IOT_PWD: string, IOT_TOPIC: string,
/*connect*/SERVER: SERVERS):
void {
OBLOQ_WIFI_SSID = SSID
OBLOQ_WIFI_PASSWORD = PASSWORD
OBLOQ_MQTT_PWD = IOT_PWD
OBLOQ_MQTT_ID = IOT_ID
OBLOQ_MQTT_TOPIC[0][0] = IOT_TOPIC
OBLOQ_SERIAL_TX = send
OBLOQ_SERIAL_RX = receive
Obloq_serial_init()
Obloq_start_connect_mqtt(SERVER,"connect wifi")
}
/**
* Disconnect the serial port.
*/
//% weight=200
//% blockId=Obloq_mqtt_add_topic
//% block="subscribe additional %top |: %IOT_TOPIC"
//% top.fieldEditor="gridpicker" top.fieldOptions.columns=2
//% advanced=true
export function Obloq_mqtt_add_topic(top: TOPIC, IOT_TOPIC: string): void {
OBLOQ_MQTT_TOPIC[top][0] = IOT_TOPIC
if(!OBLOQ_MQTT_INIT || OBLOQ_WORKING_MODE_IS_STOP) return
let _timeout = 0
if (OBLOQ_MQTT_TOPIC[top][0] != "x" && OBLOQ_MQTT_TOPIC[top][1] == "false") {
Obloq_subTopic(<string>OBLOQ_MQTT_TOPIC[top][0])
} else {
return
}
while (_timeout < 1000) {
if (OBLOQ_ANSWER_CMD == "SubOk") {
OBLOQ_ANSWER_CMD = ""
OBLOQ_MQTT_TOPIC[top][1] = "true"
break
} else if (OBLOQ_ANSWER_CMD == "SubFailure") {
OBLOQ_WRONG_TYPE = "mqtt subtopic failure"
return
}
basic.pause(1)
_timeout += 1
}
if (_timeout >= 1000 && OBLOQ_ANSWER_CMD != "SubOk") {
OBLOQ_WRONG_TYPE = "mqtt subtopic timeout"
} else {
OBLOQ_MQTT_TOPIC[top][1] = "true"
}
}
/**
* Disconnect the serial port.
*/
/*
export function Obloq_serial_quit(): void {
obloqWriteString("quit!\r")
}*/
/**
* Send the ping.time(ms): private long maxWait
* @param time to timeout, eg: 10000
*/
//% weight=49
//% blockId=Obloq_send_ping
//% block="sendPing"
//% advanced=true
export function Obloq_send_ping(): boolean {
while (OBLOQ_WORKING_MODE_IS_STOP) { basic.pause(20) }
let time = 5000
if (time < 100) {
time = 100
}
let timeout = time / 100
let _timeout = 0
if (!OBLOQ_SERIAL_INIT) {
Obloq_serial_init()
}
obloqWriteString("|1|1|\r")
while (true) {
if (OBLOQ_ANSWER_CMD == "PingOk") {
return true
} else if (OBLOQ_ANSWER_CMD == "timeout") {
return false
}
basic.pause(100)
_timeout += 1
if (_timeout > timeout) {
if (OBLOQ_ANSWER_CMD != "PingOk") {
return false
}
else {
return true
}
}
}
return false
}
/**
* Get the software version.time(ms): private long maxWait
* @param time to timeout, eg: 10000
*/
//% weight=50
//% blockId=Obloq_get_version
//% block="get version"
//% advanced=true
export function Obloq_get_version(): string {
while (OBLOQ_WORKING_MODE_IS_STOP) { basic.pause(20) }
let time = 5000
if (time < 100) {
time = 100
}
let timeout = time / 100
let _timeout = 0
if (!OBLOQ_SERIAL_INIT) {
Obloq_serial_init()
}
obloqWriteString("|1|2|\r")
while (true) {
if (OBLOQ_ANSWER_CMD == "GetVersion") {
return OBLOQ_ANSWER_CONTENT
} else if (OBLOQ_ANSWER_CMD == "timeout") {
return "timeout"
}
basic.pause(100)
_timeout += 1
if (_timeout > timeout) {
if (OBLOQ_ANSWER_CMD != "GetVersion") {
return "timeout"
}
else {
return OBLOQ_ANSWER_CONTENT
}
}
}
return ""
}
/**
* Heartbeat request.time(ms): private long maxWait
* @param time to timeout, eg: 10000
*/
//% weight=48
//% blockId=Obloq_get_heartbeat
//% block="get heartbeat"
//% advanced=true
export function Obloq_get_heartbeat(): boolean {
while (OBLOQ_WORKING_MODE_IS_STOP) { basic.pause(20) }
let time = 5000
if (time < 100) {
time = 100
}
let timeout = time / 100
let _timeout = 0
if (!OBLOQ_SERIAL_INIT) {
Obloq_serial_init()
}
obloqWriteString("|1|3|" + time + "|\r")
while (true) {
if (OBLOQ_ANSWER_CMD == "Heartbeat") {
return true
} else if (OBLOQ_ANSWER_CMD == "timeout") {
return false
}
basic.pause(100)
_timeout += 1
if (_timeout > timeout) {
if (OBLOQ_ANSWER_CMD != "Heartbeat") {
return false
}
else {
return true
}
}
}
return false
}
/**
* Stop the heartbeat request.
*/
//% weight=47
//% blockId=Obloq_stop_heartbeat
//% block="stop heartbeat"
//% advanced=true
export function Obloq_stop_heartbeat(): boolean {
while (OBLOQ_WORKING_MODE_IS_STOP) { basic.pause(20) }
let time = 5000
if (time < 100) {
time = 100
}
let timeout = time / 100
let _timeout = 0
if (!OBLOQ_SERIAL_INIT) {
Obloq_serial_init()
}
obloqWriteString("|1|3|-2|\r")
while (true) {
if (OBLOQ_ANSWER_CMD == "Heartbeat") {
return true
} else if (OBLOQ_ANSWER_CMD == "timeout") {
return false
}
basic.pause(100)
_timeout += 1
if (_timeout > timeout) {
if (OBLOQ_ANSWER_CMD != "Heartbeat") {
return false
}
else {
return true
}
}
}
return false
}
function Obloq_disconnect_wifi(): boolean {
while (OBLOQ_WORKING_MODE_IS_STOP) { basic.pause(20) }
let time = 5000
if (time < 100) {
time = 100
}
let timeout = time / 100
let _timeout = 0
if (!OBLOQ_SERIAL_INIT) {
Obloq_serial_init()
}
obloqWriteString("|2|2|\r")
while (true) {
if (OBLOQ_ANSWER_CMD == "WifiDisconnect") {
Obloq_mark_reset("wifi")
return true
} else if (OBLOQ_ANSWER_CMD == "timeout") {
return false
}
basic.pause(100)
_timeout += 1
if (_timeout > timeout) {
if (OBLOQ_ANSWER_CMD != "WifiDisconnect") {
return false
}
else {
return true
}
}
}
return false
}
/**
* Reconnect WiFi.time(ms): private long maxWait
* @param time to timeout, eg: 10000
*/
/*
export function Obloq_wifi_reconnect(): boolean {
while (OBLOQ_WORKING_MODE_IS_STOP) { basic.pause(20) }
let time = 10000
if (time < 100) {
time = 100
}
let timeout = time / 100
let _timeout = 0
if (!OBLOQ_SERIAL_INIT) {
Obloq_serial_init()
}
obloqWriteString("|2|3|\r")
while (true) {
if (OBLOQ_ANSWER_CMD == "WifiConnected") {
OBLOQ_WIFI_IP = OBLOQ_ANSWER_CONTENT
OBLOQ_WIFI_CONNECT_FIRST = false
OBLOQ_WIFI_CONNECTED = true
return true
}
basic.pause(100)
_timeout += 1
if (_timeout > timeout) {
if (OBLOQ_ANSWER_CMD != "WifiConnected") {
return false
}
else {
OBLOQ_WIFI_IP = OBLOQ_ANSWER_CONTENT
OBLOQ_WIFI_CONNECT_FIRST = false
OBLOQ_WIFI_CONNECTED = true
return true
}
}
}
return false
}*/
/**
* pin set
* @param receive to receive ,eg: SerialPin.P1
* @param send to send ,eg: SerialPin.P2
*/
/*
export function Obloq_serial_pin_set(receive: SerialPin, send: SerialPin): void {
while (OBLOQ_WORKING_MODE_IS_STOP) { basic.pause(20) }
OBLOQ_SERIAL_TX = send
OBLOQ_SERIAL_RX = receive
Obloq_serial_init()
}*/
/**
* connect Wifi.SSID(string):account; PWD(string):password;
* @param SSID to SSID ,eg: "yourSSID"
* @param PASSWORD to PASSWORD ,eg: "yourPASSWORD"
*/
/*
//% weight=100
//% blockId=Obloq_wifi_connect_export
//% block="wifi connect to| SSID %SSID| PASSWORD %PASSWORD"
//% advanced=true
export function Obloq_wifi_connect_export(SSID: string, PASSWORD: string): void {
while (OBLOQ_WORKING_MODE_IS_STOP) { basic.pause(20) }
OBLOQ_WIFI_SSID = SSID
OBLOQ_WIFI_PASSWORD = PASSWORD
Obloq_connect_wifi()
}*/
function Obloq_connect_wifi(): number {
if (OBLOQ_WIFI_CONNECTED == true) {
return OBLOQ_ERROR_TYPE_IS_SUCCE
}
OBLOQ_WIFI_ICON = 1
let timeout = 10000 //Set the default timeout period 10s.
timeout = timeout < 100 ? 100 : timeout //Timeout minimum resolution 100ms
let timeout_count_max = timeout / 100
let timeout_count_now = 0