-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.ts
1368 lines (1071 loc) · 46.1 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
enum USER {
//% block="INDUSTRIAL"
INDUSTRIAL = 1,
//% block="EDUCATIONAL"
EDUCATIONAL = 2
}
//% groups='["Common",ATT", "Ubidots", "Azure", "MQTT", "others"]'
//% weight=6 color=#2699BF icon="\uf110" block="CW01"
namespace cw01 {
class cw01_int_var123 {
res: string
TOKEN: string
DEVICE_ID: string
asset_name: string
NEWLINE: string
start: boolean
latitude: number
longitude: number
select: boolean
azureAccess: string
mqtt_payload: string
prev_mqtt_payload: string
block: boolean
mqtt_topic: string
fail_count: number
topics: string[]
topic_count: number
topic_rcv: string
timer: number
att_string: boolean
att_string_value: string
att_number: boolean
att_number_value: number
att_state: boolean
att_state_value: boolean
att_asset: string
mqtt_message: string
constructor() {
this.res = ""
this.TOKEN = ""
this.DEVICE_ID = ""
this.asset_name = ""
this.NEWLINE = "\u000D\u000A"
this.start = false
this.latitude = 0
this.longitude = 0
this.select = false
this.azureAccess = ""
this.mqtt_payload = ""
this.prev_mqtt_payload = ""
this.block = false
this.mqtt_topic = ""
this.fail_count = 0
this.topics = []
this.topic_count = 0
this.topic_rcv = ""
this.timer = 0
this.att_string = false
this.att_string_value = ""
this.att_number = false
this.att_number_value = 0
this.att_state = false
this.att_state_value = false
this.att_asset = ""
this.mqtt_message = ""
}
}
class cw01_mqtt {
new_payload: string
prev_payload: string
new_topic: string
prev_topic: string
enable_event_1: boolean
enable_event_2: boolean
id: string
id_enable: boolean
timer_enable: boolean
sending_payload: boolean
sending_pingreq: boolean
receiving_msg: boolean
mqtt_busy: boolean
mac_addr: string
constructor() {
this.new_payload = ""
this.prev_payload = ""
this.new_topic = ""
this.prev_topic = ""
this.enable_event_1 = false
this.enable_event_2 = false
this.id = ""
this.id_enable = false
this.timer_enable = true
this.sending_payload = false
this.sending_pingreq = false
this.receiving_msg = false
this.mac_addr = ""
this.mqtt_busy = false
}
}
class button_class {
sending_data: boolean
constructor() {
this.sending_data = false
}
}
let sdFlag=false
let cw01_vars = new cw01_int_var123()
let cw01_mqtt_vars = new cw01_mqtt()
let cw01_button_object = new button_class()
let en_Feedback: boolean = true
cw01_vars.start = true
serial.redirect(SerialPin.P1, SerialPin.P0, 115200)
serial.setRxBufferSize(200)
basic.showIcon(IconNames.Chessboard)
basic.pause(2000)
serial.writeString("ATE0" + cw01_vars.NEWLINE)
basic.pause(300)
serial.readString()
cw01_mqtt_vars.mac_addr = extract_mac()
serial.writeString("AT+CWMODE_DEF=3" + cw01_vars.NEWLINE)
basic.pause(300)
serial.writeString("AT+CIPRECVMODE=1" + cw01_vars.NEWLINE)
basic.pause(300)
serial.writeString("AT+TEST" + cw01_vars.NEWLINE)
basic.pause(300)
serial.readString();
serial.writeString("AT+CWHOSTNAME?" + cw01_vars.NEWLINE);
basic.pause(1000)
read_and_set_name();
function read_and_set_name(): void {
let name: string = "";
name = serial.readString()
if (!(name.includes("CW01"))) {
serial.writeString("AT+CWHOSTNAME=\"CW01\"" + cw01_vars.NEWLINE)
basic.pause(1000)
control.reset()
}
}
function extract_mac(): string {
let raw_str: string = ""
let mac_addr: string = ""
let index: number = 0
serial.writeString("AT+CIPSTAMAC_CUR?" + cw01_vars.NEWLINE)
basic.pause(500)
raw_str = serial.readString()
index = raw_str.indexOf("\"") + 1
mac_addr = raw_str.substr(index, 17)
return mac_addr
}
/**
* Connect to W-Fi
*/
//% weight=91 color=#ad0303
//% group="Common"
//% blockId="connectToWifi" block="CW01 connect to WiFi SSID %SSID password %PSK"
export function connectToWifi(SSID: string, PSK: string): void {
serial.writeString("AT+CWMODE=1" + cw01_vars.NEWLINE)
basic.pause(100)
serial.readString()
serial.writeString("AT+CWJAP=\"" + SSID + "\",\"" + PSK + "\"" + cw01_vars.NEWLINE)
basic.pause(200)
serial.readString()
let loop_count = 0
do {
cw01_vars.res = serial.readString()
basic.pause(1000)
loop_count++
if(loop_count == 20)
break
} while (!cw01_vars.res.includes("WIFI CONNECTED"));
if (cw01_vars.res.includes("WIFI CONNECTED")) {
basic.showString("C")
basic.pause(5000)
basic.showString("")
cw01_vars.res = ""
}
if(loop_count == 20)
{
basic.showString("D")
basic.pause(5000)
basic.showString("")
}
}
/**
* Enable feedback through microbit Matrix LEDs
*/
//% weight=91 color=#ad0303
//% group="Common"
//% blockId="enableFeedback" block="CW01 enable feedback LEDs %u"
export function enableFeedback(u: boolean): void
{
en_Feedback = u
}
/**
* Connect to AllThingsTalk IoT platform
*/
//% weight=91
//% group="ATT"
//% blockId="connectToATT" block="CW01 connect to ATT with token %TKN and device-id %ID"
export function connectToATT(TKN: string, ID: string): void {
cw01_vars.DEVICE_ID = ID
cw01_vars.TOKEN = TKN
serial.writeString("AT+CIPSTART=\"TCP\",\"api.allthingstalk.io\",80" + cw01_vars.NEWLINE)
basic.pause(500)
}
/**
* Send string data to AllThingsTalk IoT platform
*/
//% weight=91
//% group="ATT"
//% blockId="IoTSendStringToATT" block="CW01 send string %value to ATT asset %asset"
export function IoTSendStringToATT(value: string, asset: string): void {
let att_connected: string = ""
while (cw01_button_object.sending_data) {
basic.pause(100)
}
cw01_button_object.sending_data = true
do {
cw01_vars.asset_name = asset
serial.writeString("AT+CIPMODE=0" + cw01_vars.NEWLINE)
basic.pause(100)
let payload: string = "{\"value\": \"" + value + "\"}"
let request: string = "PUT /device/" + cw01_vars.DEVICE_ID + "/asset/" + cw01_vars.asset_name + "/state" + " HTTP/1.1" + cw01_vars.NEWLINE +
"Host: api.allthingstalk.io" + cw01_vars.NEWLINE +
"User-Agent: CW01/1.0" + cw01_vars.NEWLINE +
"Accept: */*" + cw01_vars.NEWLINE +
"Authorization: Bearer " + cw01_vars.TOKEN + cw01_vars.NEWLINE +
"Content-Type:application/json" + cw01_vars.NEWLINE +
"Content-Length: " + (payload.length).toString() + cw01_vars.NEWLINE + cw01_vars.NEWLINE + payload + cw01_vars.NEWLINE
serial.writeString("AT+CIPSEND=" + (request.length + 2).toString() + cw01_vars.NEWLINE)
basic.pause(50)
serial.writeString(request + cw01_vars.NEWLINE)
basic.pause(1000)
att_connected = serial.readString()
if (att_connected.includes("link is not valid")) {
connectToATT(cw01_vars.TOKEN, cw01_vars.DEVICE_ID)
} else {
att_connected = ""
}
get_status()
} while (att_connected.includes("link is not valid"))
cw01_button_object.sending_data = false
}
/**
* Send numerical data to AllThingsTalk IoT platform
*/
//% weight=91
//% group="ATT"
//% blockId="IoTSendValueToATT" block="CW01 send value %value to ATT asset %asset"
export function IoTSendValueToATT(value: number, asset: string): void {
let att_connected: string = ""
while (cw01_button_object.sending_data) {
basic.pause(100)
}
cw01_button_object.sending_data = true
do {
cw01_vars.asset_name = asset
serial.writeString("AT+CIPMODE=0" + cw01_vars.NEWLINE)
basic.pause(100)
let payload: string = "{\"value\": " + value.toString() + "}"
let request: string = "PUT /device/" + cw01_vars.DEVICE_ID + "/asset/" + cw01_vars.asset_name + "/state" + " HTTP/1.1" + cw01_vars.NEWLINE +
"Host: api.allthingstalk.io" + cw01_vars.NEWLINE +
"User-Agent: CW01/1.0" + cw01_vars.NEWLINE +
"Accept: */*" + cw01_vars.NEWLINE +
"Authorization: Bearer " + cw01_vars.TOKEN + cw01_vars.NEWLINE +
"Content-Type:application/json" + cw01_vars.NEWLINE +
"Content-Length: " + (payload.length).toString() + cw01_vars.NEWLINE + cw01_vars.NEWLINE + payload + cw01_vars.NEWLINE
serial.writeString("AT+CIPSEND=" + (request.length + 2).toString() + cw01_vars.NEWLINE)
basic.pause(50)
serial.writeString(request + cw01_vars.NEWLINE)
basic.pause(1000)
att_connected = serial.readString()
if (att_connected.includes("link is not valid")) {
connectToATT(cw01_vars.TOKEN, cw01_vars.DEVICE_ID)
} else {
att_connected = ""
}
get_status()
} while (att_connected.includes("link is not valid"))
cw01_button_object.sending_data = false
}
/**
* Send boolean data to AllThingsTalk IoT platform
*/
//% weight=91
//% group="ATT"
//% blockId="IoTSendStateToATT" block="CW01 send state %state to ATT asset %asset_name"
export function IoTSendStateToATT(state: boolean, asset: string): void {
let att_connected: string = ""
while (cw01_button_object.sending_data) {
basic.pause(100)
}
cw01_button_object.sending_data = true
do {
let stateStr: string
if (state == true) {
stateStr = "true"
} else {
stateStr = "false"
}
cw01_vars.asset_name = asset
serial.writeString("AT+CIPMODE=0" + cw01_vars.NEWLINE)
basic.pause(100)
let payload: string = "{\"value\": " + stateStr + "}"
let request: string = "PUT /device/" + cw01_vars.DEVICE_ID + "/asset/" + cw01_vars.asset_name + "/state" + " HTTP/1.1" + cw01_vars.NEWLINE +
"Host: api.allthingstalk.io" + cw01_vars.NEWLINE +
"User-Agent: CW01/1.0" + cw01_vars.NEWLINE +
"Accept: */*" + cw01_vars.NEWLINE +
"Authorization: Bearer " + cw01_vars.TOKEN + cw01_vars.NEWLINE +
"Content-Type:application/json" + cw01_vars.NEWLINE +
"Content-Length: " + (payload.length).toString() + cw01_vars.NEWLINE + cw01_vars.NEWLINE + payload + cw01_vars.NEWLINE
serial.writeString("AT+CIPSEND=" + (request.length + 2).toString() + cw01_vars.NEWLINE)
basic.pause(50)
serial.writeString(request + cw01_vars.NEWLINE)
basic.pause(1000)
att_connected = serial.readString()
if (att_connected.includes("link is not valid")) {
connectToATT(cw01_vars.TOKEN, cw01_vars.DEVICE_ID)
} else {
att_connected = ""
}
get_status()
} while (att_connected.includes("link is not valid"))
cw01_button_object.sending_data = false
}
/**
* Get latest value of asset from AllThingsTalk IoT platform. Asset can be string, numerical and boolean
*/
//% weight=91
//% group="ATT"
//% blockId="IoTgetATTAssetValue" block="CW01 get ATT asset %asset state"
export function IoTgetATTAssetValue(asset: string): string {
let att_connected: string = ""
while (cw01_button_object.sending_data) {
basic.pause(100)
}
cw01_button_object.sending_data = true
cw01_vars.res = ""
let index1: number
let index2: number
let value: string
do {
cw01_vars.asset_name = asset
basic.pause(100)
let request: string = "GET /device/" + cw01_vars.DEVICE_ID + "/asset/" + cw01_vars.asset_name + "/state" + " HTTP/1.1" + cw01_vars.NEWLINE +
"Host: api.allthingstalk.io" + cw01_vars.NEWLINE +
"User-Agent: CW01/1.0" + cw01_vars.NEWLINE +
"Accept: */*" + cw01_vars.NEWLINE +
"Authorization: Bearer " + cw01_vars.TOKEN + cw01_vars.NEWLINE + cw01_vars.NEWLINE
serial.writeString("AT+CIPSEND=" + (request.length + 2).toString() + cw01_vars.NEWLINE)
basic.pause(50)
serial.writeString(request + cw01_vars.NEWLINE)
basic.pause(1200)
att_connected = serial.readString()
if (att_connected.includes("link is not valid")) {
connectToATT(cw01_vars.TOKEN, cw01_vars.DEVICE_ID)
} else {
att_connected = ""
}
if (!att_connected.includes("link is not valid")) {
serial.writeString("AT+CIPRECVDATA=200" + cw01_vars.NEWLINE)
basic.pause(100)
serial.readString()
basic.pause(400)
serial.writeString("AT+CIPRECVDATA=200" + cw01_vars.NEWLINE)
basic.pause(400)
cw01_vars.res += serial.readString()
index1 = cw01_vars.res.indexOf("\"value\":") + "\"value\":".length
index2 = cw01_vars.res.indexOf("}", index1)
value = cw01_vars.res.substr(index1, index2 - index1)
}
} while (att_connected.includes("link is not valid"))
cw01_button_object.sending_data = false
return value
}
/**
* Connect to Ubidots IoT platform
*/
//% weight=91 color=#f2ca00
//% group="Ubidots"
//% blockId="connectToUbidots" block="CW01 connect to Ubidots %user| with token %TKN"
export function connectToUbidots(User: USER, TKN: string): void {
switch (User) {
case USER.INDUSTRIAL: cw01_vars.select = true;
case USER.EDUCATIONAL: cw01_vars.select = false;
}
cw01_vars.TOKEN = TKN
serial.writeString("AT+CIPSTART=\"TCP\",\"things.ubidots.com\",80" + cw01_vars.NEWLINE)
basic.pause(500)
}
/**
* Get latest value of variable from Ubidots IoT platform
*/
//% weight=91 color=#f2ca00
//% group="Ubidots"
//% blockId="IoTgetValuefromUbidots" block="CW01 get value from Ubidots device %device variable %variable"
export function IoTgetValuefromUbidots(device: string, variable: string): string {
let ubi_connected: string = ""
while (cw01_button_object.sending_data) {
basic.pause(100)
}
cw01_button_object.sending_data = true
cw01_vars.res = ""
let value: string
let index1: number
let index2: number
do {
let industrial: string = "industrial.api.ubidots.com"
let educational: string = "things.ubidots.com"
let server: string
if (cw01_vars.select) {
server = industrial
} else {
server = educational
}
let request: string = "GET /api/v1.6/devices/" + device + "/" + variable + "/values/?page_size=1 HTTP/1.1" + cw01_vars.NEWLINE +
"Host: " + server + cw01_vars.NEWLINE +
"User-Agent: CW01/1.0" + cw01_vars.NEWLINE +
"Accept: */*" + cw01_vars.NEWLINE +
"X-Auth-Token: " + cw01_vars.TOKEN + cw01_vars.NEWLINE +
"Content-Type: application/json" + cw01_vars.NEWLINE + cw01_vars.NEWLINE
serial.writeString("AT+CIPSEND=" + (request.length).toString() + cw01_vars.NEWLINE)
basic.pause(400)
serial.writeString(request)
basic.pause(1000)
ubi_connected = serial.readString()
if (ubi_connected.includes("link is not valid")) {
if (cw01_vars.select) {
connectToUbidots(USER.INDUSTRIAL, cw01_vars.TOKEN)
} else {
connectToUbidots(USER.EDUCATIONAL, cw01_vars.TOKEN)
}
} else {
ubi_connected = ""
}
} while (ubi_connected.includes("link is not valid"));
serial.writeString("AT+CIPRECVDATA=200" + cw01_vars.NEWLINE)
basic.pause(400)
serial.readString()
serial.writeString("AT+CIPRECVDATA=100" + cw01_vars.NEWLINE)
basic.pause(400)
serial.readString()
basic.pause(100)
serial.writeString("AT+CIPRECVDATA=200" + cw01_vars.NEWLINE)
basic.pause(400)
cw01_vars.res += serial.readString()
basic.pause(400)
index1 = cw01_vars.res.indexOf("\"value\": ") + "\"value\": ".length
index2 = cw01_vars.res.indexOf("]", index1)
value = cw01_vars.res.substr(index1, index2 - index1 - 1)
cw01_button_object.sending_data = false
return value
}
/**
* Send numerical value to Ubidots IoT platform. Select loc to true if you want to send GPS
* location entered with IoTaddLocation block
*/
//% weight=91 color=#f2ca00
//% group="Ubidots"
//% blockId="IoTSendValueToUbidots" block="CW01 send value %value to Ubidots device %device variable %variable , include location %loc"
export function IoTSendValueToUbidots(value: number, device: string, variable: string, loc: boolean): void {
let ubi_connected: string = ""
let value_store: number = value
while (cw01_button_object.sending_data) {
basic.pause(100)
}
cw01_button_object.sending_data = true
do {
let payload: string = "{\"value\": " + value.toString() + "}"
if (loc) {
payload = "{\"value\": " + value.toString() + ", \"context\": {\"lat\": " + cw01_vars.latitude.toString() + ", \"lng\": " + cw01_vars.longitude.toString() + "}}"
}
let industrial: string = "industrial.api.ubidots.com"
let educational: string = "things.ubidots.com"
let server: string
if (cw01_vars.select) {
server = industrial
} else {
server = educational
}
let request: string = "POST /api/v1.6/devices/" + device + "/" + variable + "/values HTTP/1.1" + cw01_vars.NEWLINE +
"Host: " + server + cw01_vars.NEWLINE +
"User-Agent: CW01/1.0" + cw01_vars.NEWLINE +
"X-Auth-Token: " + cw01_vars.TOKEN + cw01_vars.NEWLINE +
"Content-Type: application/json" + cw01_vars.NEWLINE +
"Connection: keep-alive" + cw01_vars.NEWLINE +
"Accept: */*" + cw01_vars.NEWLINE +
"Content-Length: " + (payload.length).toString() + cw01_vars.NEWLINE + cw01_vars.NEWLINE + payload + cw01_vars.NEWLINE
serial.writeString("AT+CIPSEND=" + (request.length).toString() + cw01_vars.NEWLINE)
basic.pause(300)
serial.writeString(request)
basic.pause(1000)
ubi_connected = serial.readString()
if (ubi_connected.includes("link is not valid")) {
if (cw01_vars.select) {
connectToUbidots(USER.INDUSTRIAL, cw01_vars.TOKEN)
} else {
connectToUbidots(USER.EDUCATIONAL, cw01_vars.TOKEN)
}
} else {
ubi_connected = ""
}
get_status()
basic.pause(100)
serial.writeString("AT+CIPRECVDATA=400" + cw01_vars.NEWLINE)
basic.pause(100)
serial.readString()
} while (ubi_connected.includes("link is not valid"));
cw01_button_object.sending_data = false;
}
/**
* Connect to Microsoft Azure cloud computing platform
*/
//% weight=91 color=#4B0082
//% group="Azure"
//% blockId="connectToAzure" block="CW01 connect to Azure with access enpoint %access"
export function connectToAzure(access: string): void {
serial.writeString("AT+CIPSTART=\"TCP\",\"proxy.xinabox.cc\",80" + cw01_vars.NEWLINE)
basic.pause(500)
cw01_vars.azureAccess = access
}
/**
* Send string data to Microsoft Azure cloud computing platform
*/
//% weight=91 color=#4B0082
//% group="Azure"
//% blockId="IoTSendStringToAzure" block="CW01 update Azure variable %asset with string %value"
export function IoTSendStringToAzure(asset: string, value: string): void {
let payload: string = "{\"" + asset + "\": " + value + "}"
let request: string = "POST /135/" + cw01_vars.azureAccess + " HTTP/1.1" + cw01_vars.NEWLINE +
"Host: proxy.xinabox.cc" + cw01_vars.NEWLINE +
"User-Agent: CW01/1.0" + cw01_vars.NEWLINE +
"Content-Type: application/json" + cw01_vars.NEWLINE +
"Accept: */*" + cw01_vars.NEWLINE +
"Content-Length: " + (payload.length).toString() + cw01_vars.NEWLINE + cw01_vars.NEWLINE + payload + cw01_vars.NEWLINE
serial.writeString("AT+CIPSEND=" + (request.length).toString() + cw01_vars.NEWLINE)
basic.pause(100)
serial.writeString(request)
basic.pause(100)
serial.readString()
basic.pause(1000)
if (!get_status()) {
connectToAzure(cw01_vars.azureAccess)
}
}
/**
* Send numerical value to Microsoft Azure cloud computing platform
*/
//% weight=91 color=#4B0082
//% group="Azure"
//% blockId="IoTSendValueToAzure" block="CW01 update Azure variable %asset with value %value"
export function IoTSendValueToAzure(asset: string, value: number): void {
let payload: string = "{\"" + asset + "\": " + value.toString() + "}"
let request: string = "POST /135/" + cw01_vars.azureAccess + " HTTP/1.1" + cw01_vars.NEWLINE +
"Host: proxy.xinabox.cc" + cw01_vars.NEWLINE +
"User-Agent: CW01/1.0" + cw01_vars.NEWLINE +
"Content-Type: application/json" + cw01_vars.NEWLINE +
"Accept: */*" + cw01_vars.NEWLINE +
"Content-Length: " + (payload.length).toString() + cw01_vars.NEWLINE + cw01_vars.NEWLINE + payload + cw01_vars.NEWLINE
serial.writeString("AT+CIPSEND=" + (request.length).toString() + cw01_vars.NEWLINE)
basic.pause(100)
serial.writeString(request)
basic.pause(100)
serial.readString()
basic.pause(1000)
if (!get_status()) {
connectToAzure(cw01_vars.azureAccess)
}
}
/**
* Send two numerical values to Microsoft Azure cloud computing platform
*/
//% weight=91 color=#4B0082
//% group="Azure"
//% advanced=true
//% blockId="IoTSendTwoValuesToAzure" block="CW01 update Azure variables %asset1 with value %value1 and %asset2 with value %value2"
export function IoTSendTwoValuesToAzure(asset1: string, value1: number, asset2: string, value2: number): void {
let payload: string = "{\"" + asset1 + "\": " + value1.toString() + "," + "\"" + asset2 + "\": " + value2.toString() + "}"
let request: string = "POST /135/" + cw01_vars.azureAccess + " HTTP/1.1" + cw01_vars.NEWLINE +
"Host: proxy.xinabox.cc" + cw01_vars.NEWLINE +
"User-Agent: CW01/1.0" + cw01_vars.NEWLINE +
"Content-Type: application/json" + cw01_vars.NEWLINE +
"Accept: */*" + cw01_vars.NEWLINE +
"Content-Length: " + (payload.length).toString() + cw01_vars.NEWLINE + cw01_vars.NEWLINE + payload + cw01_vars.NEWLINE
serial.writeString("AT+CIPSEND=" + (request.length).toString() + cw01_vars.NEWLINE)
basic.pause(100)
serial.writeString(request)
basic.pause(100)
serial.readString()
basic.pause(1000);
if (!get_status()) {
connectToAzure(cw01_vars.azureAccess)
}
}
/**
* Send two numerical values to Microsoft Azure cloud computing platform
*/
//% weight=91 color=#4B0082
//% group="Azure"
//% advanced=true
//% blockId="IoTSendThreeValuesToAzure" block="CW01 update Azure variables %asset1 with value %value1, %asset2 with value %value2 and %asset3 with value %value3 "
export function IoTSendThreeValuesToAzure(asset1: string, value1: number, asset2: string, value2: number, asset3: string, value3: number): void {
let payload: string = "{\"" + asset1 + "\": " + value1.toString() + "," + "\"" + asset2 + "\": " + value2.toString() + "," + "\"" + asset3 + "\": " + value3.toString() + "}"
let request: string = "POST /135/" + cw01_vars.azureAccess + " HTTP/1.1" + cw01_vars.NEWLINE +
"Host: proxy.xinabox.cc" + cw01_vars.NEWLINE +
"User-Agent: CW01/1.0" + cw01_vars.NEWLINE +
"Content-Type: application/json" + cw01_vars.NEWLINE +
"Accept: */*" + cw01_vars.NEWLINE +
"Content-Length: " + (payload.length).toString() + cw01_vars.NEWLINE + cw01_vars.NEWLINE + payload + cw01_vars.NEWLINE
serial.writeString("AT+CIPSEND=" + (request.length).toString() + cw01_vars.NEWLINE)
basic.pause(100)
serial.writeString(request)
basic.pause(100)
serial.readString()
basic.pause(1000)
if (!get_status()) {
connectToAzure(cw01_vars.azureAccess)
}
}
/**
* Send two numerical values to Microsoft Azure cloud computing platform
*/
//% weight=91 color=#4B0082
//% group="Azure"
//% advanced=true
//% blockId="IoTSendFourValuesToAzure" block="CW01 update Azure variables %asset1 with value %value1, %asset2 with value %value2, %asset3 with value %value3 and %asset4 with value %value4"
export function IoTSendFourValuesToAzure(asset1: string, value1: number, asset2: string, value2: number, asset3: string, value3: number, asset4: string, value4: number): void {
let payload: string = "{\"" + asset1 + "\": " + value1.toString() + "," + "\"" + asset2 + "\": " + value2.toString() + "," + "\"" + asset3 + "\": " + value3.toString() + "," + "\"" + asset4 + "\": " + value4.toString() + "}"
let request: string = "POST /135/" + cw01_vars.azureAccess + " HTTP/1.1" + cw01_vars.NEWLINE +
"Host: proxy.xinabox.cc" + cw01_vars.NEWLINE +
"User-Agent: CW01/1.0" + cw01_vars.NEWLINE +
"Content-Type: application/json" + cw01_vars.NEWLINE +
"Accept: */*" + cw01_vars.NEWLINE +
"Content-Length: " + (payload.length).toString() + cw01_vars.NEWLINE + cw01_vars.NEWLINE + payload + cw01_vars.NEWLINE
serial.writeString("AT+CIPSEND=" + (request.length).toString() + cw01_vars.NEWLINE)
basic.pause(100)
serial.writeString(request)
basic.pause(100)
serial.readString()
basic.pause(1000)
if (!get_status()) {
connectToAzure(cw01_vars.azureAccess)
}
}
/**
* Send two Values to Microsoft Azure and get response.
*/
//% weight=91 color=#4B0082
//% group="Azure"
//% blockId="IoTSendTwoValuesSaveResponseAzure" block="CW01 update two variables %asset1 and %asset2 with values %value1 and %value2 and get response"
export function IoTSendTwoValuesSaveResponseAzure(asset1: string, asset2: string, value1: number, value2: number): string {
let value: string
let index1: number
let index2: number
let len: number = 0
let i: number = 0
let connection: boolean = true
let payload: string = "{\"" + asset1 + "\": " + value1.toString() + "," + "\"" + asset2 + "\": " + value2.toString() + "}"
let request: string = "POST /135/" + cw01_vars.azureAccess + " HTTP/1.1" + cw01_vars.NEWLINE +
"Host: proxy.xinabox.cc" + cw01_vars.NEWLINE +
"User-Agent: CW01/1.0" + cw01_vars.NEWLINE +
"Content-Type: application/json" + cw01_vars.NEWLINE +
"Connection: Keep-Alive" + cw01_vars.NEWLINE +
"Accept: */*" + cw01_vars.NEWLINE +
"Content-Length: " + (payload.length).toString() + cw01_vars.NEWLINE + cw01_vars.NEWLINE + payload + cw01_vars.NEWLINE
serial.writeString("AT+CIPSEND=" + (request.length).toString() + cw01_vars.NEWLINE)
basic.pause(100)
serial.writeString(request)
basic.pause(10)
basic.pause(3000)
basic.pause(100)
if (!get_status()) {
connection = false
connectToAzure(cw01_vars.azureAccess)
}
basic.pause(500)
if (connection) {
cw01_vars.res = ""
serial.readString()
serial.writeString("AT+CIPRECVDATA=900" + cw01_vars.NEWLINE)
basic.pause(200)
serial.readString()
serial.writeString("AT+CIPRECVDATA=800" + cw01_vars.NEWLINE)
basic.pause(200)
cw01_vars.res = ""
while (true) {
cw01_vars.res = serial.readUntil("\n")
basic.pause(1)
if (cw01_vars.res.compare("\r") == 0)
break
}
while (true) {
cw01_vars.res = serial.readUntil("\n")
basic.pause(1)
if (cw01_vars.res.compare("\r") == 0)
break
}
cw01_vars.res = serial.readString()
index1 = 0
index2 = cw01_vars.res.indexOf("OK")
len = index2 - index1 - 1
value = cw01_vars.res.substr(index1, len)
} else {
value = ""
}
/*if (cw01_vars.res.includes(asset)) {
index1 = cw01_vars.res.indexOf(searchString) + searchString.length
index2 = cw01_vars.res.indexOf("}", index1)
value = cw01_vars.res.substr(index1, index2 - index1)
} else {
value = ""
}*/
return value
}
/**
* Connect to MQTT broker through port number 1883
*/
//% weight=91
//% group="MQTT"
//% blockId="IoTMQTTConnect" block="CW01 connect to MQTT broker URL %broker with username %Username and password %Password"
export function IoTMQTTConnect(broker: string, Username: string, Password: string): void {
serial.writeString("AT+CIPSTART=\"TCP\",\"" + broker + "\",1883" + cw01_vars.NEWLINE)
basic.pause(7000)
let protocol_name_prior: Buffer = pins.packBuffer("!H", [4])
let protocol_name: string = "MQTT"
let protocol_lvl: Buffer = pins.packBuffer("!B", [4])
//let msg_part_one: string = protocol_name + protocol_lvl
let connect_flags: Buffer = (pins.packBuffer("!B", [(1 << 7) | (1 << 6) | (1 << 1)]))
let keep_alive: Buffer = pins.packBuffer("!H", [3600])
let client_id: string
if (cw01_mqtt_vars.id_enable) {
client_id = cw01_mqtt_vars.id
} else {
client_id = cw01_mqtt_vars.mac_addr
}
let client_id_len: Buffer = pins.packBuffer("!H", [client_id.length])
let username: string = Username
let username_len: Buffer = pins.packBuffer("!H", [username.length])
let password: string = Password
let password_len: Buffer = pins.packBuffer("!H", [password.length])
//let msg_part_two = client_id_len + client_id + username_len + username + password_len + password
serial.writeString("AT+CIPSEND=" + (1 + 1 + protocol_name_prior.length + protocol_name.length + protocol_lvl.length + connect_flags.length + keep_alive.length + client_id_len.length + client_id.length + username_len.length + username.length + password_len.length + password.length) + cw01_vars.NEWLINE)
basic.pause(1000)
//Msg part one
serial.writeBuffer(pins.packBuffer("!B", [1 << 4]))
serial.writeBuffer(pins.packBuffer("!B", [protocol_name_prior.length + protocol_name.length + protocol_lvl.length + connect_flags.length + keep_alive.length + client_id_len.length + client_id.length + username_len.length + username.length + password_len.length + password.length]))
//Msg part two
serial.writeBuffer(protocol_name_prior)
serial.writeString(protocol_name)
serial.writeBuffer(protocol_lvl)
serial.writeBuffer(connect_flags)
serial.writeBuffer(keep_alive)
serial.writeBuffer(client_id_len)
serial.writeString(client_id)
serial.writeBuffer(username_len)
serial.writeString(username)
serial.writeBuffer(password_len)
serial.writeString(password)
basic.pause(3000)
cw01_vars.timer = input.runningTime()
serial.writeString("AT+CIPRECVDATA=200" + cw01_vars.NEWLINE)
basic.pause(100)
serial.readString()
control.inBackground(function () {
while (true) {
basic.pause(30000)
if (((input.runningTime() - cw01_vars.timer) > 180000) && !cw01_mqtt_vars.sending_payload && !cw01_mqtt_vars.receiving_msg) {
cw01_mqtt_vars.sending_pingreq = true
cw01_vars.timer = input.runningTime()
let header_one: Buffer = pins.packBuffer("!B", [0xC0])
let header_two: Buffer = pins.packBuffer("!B", [0x00])
serial.writeString("AT+CIPSEND=" + (header_one.length + header_two.length) + cw01_vars.NEWLINE)
basic.pause(100)
serial.writeBuffer(header_one)
serial.writeBuffer(header_two)
cw01_mqtt_vars.sending_pingreq = false
}
}
})
control.raiseEvent(EventBusSource.MICROBIT_ID_BUTTON_AB, EventBusValue.MICROBIT_BUTTON_EVT_CLICK)
}
/**
* Set client ID of microbit
*/
//% weight=91
//% group="MQTT"
//% blockId="IoTMQTTSetClientID" block="CW01 set MQTT client ID %ID"
//% advanced=true
export function IoTMQTTSetClientID(ID: string) {
cw01_mqtt_vars.id = ID
cw01_mqtt_vars.id_enable = true;
}
/**
* Send payload to MQTT topic