-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmicroIoT.ts
More file actions
1237 lines (1164 loc) · 39.5 KB
/
microIoT.ts
File metadata and controls
1237 lines (1164 loc) · 39.5 KB
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 pxt-microIoT/microIoT.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 MIT Lesser General Public License
*
* @author [email](jie.tang@dfrobot.com)
* @version V0.0.3
* @date 2019-12-31
*/
const OBLOQ_MQTT_EASY_IOT_SERVER_CHINA = "iot.dfrobot.com.cn"
const OBLOQ_MQTT_EASY_IOT_SERVER_GLOBAL = "api.beebotte.com"
const OBLOQ_MQTT_EASY_IOT_SERVER_EN = "iot.dfrobot.com"
const microIoT_WEBHOOKS_URL = "maker.ifttt.com"
const OBLOQ_MQTT_EASY_IOT_SERVER_TK = "api.thingspeak.com"
const microIoT_Weather_URL = "api.dfrobot.top"
enum NeoPixelColors {
//% block=red
Red = 0xFF0000,
//% block=orange
Orange = 0xFFA500,
//% block=yellow
Yellow = 0xFFFF00,
//% block=green
Green = 0x00FF00,
//% block=blue
Blue = 0x0000FF,
//% block=indigo
Indigo = 0x4b0082,
//% block=violet
Violet = 0x8a2be2,
//% block=purple
Purple = 0xFF00FF,
//% block=white
White = 0xFFFFFF,
//% block=black
Black = 0x000000
}
enum PIN {
P15,
P0,
P1,
P2,
P8,
P12,
P16
};
/**
*Obloq implementation method.
*/
//% weight=10 color=#008B00 icon="\uf1eb" block="microIoT"
namespace microIoT {
let BrightnessP15=255;
let BrightnessP1 = 255;
let BrightnessP0 = 255;
let BrightnessP2 = 255;
let BrightnessP8 = 255;
let BrightnessP12 = 255;
let BrightnessP16 = 255;
let LenP15 = 4;
let LenP1 = 4;
let LenP0 = 4;
let LenP2 = 4;
let LenP8 = 4;
let LenP12 = 4;
let LenP16 = 4;
let IIC_ADDRESS = 0x16
let Topic0CallBack: Action = null;
let Topic1CallBack: Action = null;
let Topic2CallBack: Action = null;
let Topic3CallBack: Action = null;
let Topic4CallBack: Action = null;
let Wifi_Status = 0x00
let microIoT_WEBHOOKS_KEY = ""
let microIoT_WEBHOOKS_EVENT = ""
let microIoT_BEEBOTTE_Token = ""
let microIoT_THINGSPEAK_KEY = ""
let READ_STATUS = 0x00
let SET_PARA = 0x01
let RUN_COMMAND = 0x02
/*set para*/
let SETWIFI_NAME = 0x01
let SETWIFI_PASSWORLD = 0x02
let SETMQTT_SERVER = 0x03
let SETMQTT_PORT = 0x04
let SETMQTT_ID = 0x05
let SETMQTT_PASSWORLD = 0x06
let SETHTTP_IP = 0x07
let SETHTTP_PORT = 0x08
/*run command*/
let SEND_PING = 0x01
let CONNECT_WIFI = 0x02
let RECONNECT_WIFI = 0x03
let DISCONECT_WIFI = 0x04
let CONNECT_MQTT = 0x05
let SUB_TOPIC0 = 0x06
let SUB_TOPIC1 = 0x07
let SUB_TOPIC2 = 0x08
let SUB_TOPIC3 = 0x09
let SUB_TOPIC4 = 0x0A
let PUB_TOPIC0 = 0x0B
let PUB_TOPIC1 = 0x0C
let PUB_TOPIC2 = 0x0D
let PUB_TOPIC3 = 0x0E
let PUB_TOPIC4 = 0x0F
let GET_URL = 0x10
let POST_URL = 0x11
let PUT_URL = 0x12
let GET_VERSION = 0x13
let mqttState = 0
let wifiConnected = 0
let READmode = 0x00
let versionState = 0
/*read para value*/
let READ_PING = 0x01
let READ_WIFISTATUS = 0x02
let READ_IP = 0x03
let READ_MQTTSTATUS = 0x04
let READ_SUBSTATUS = 0x05
let READ_TOPICDATA = 0x06
let HTTP_REQUEST = 0x10
let READ_VERSION = 0x12
/*para status */
let PING_OK = 0x01
let WIFI_DISCONNECT = 0x00
let WIFI_CONNECTING = 0x02
let WIFI_CONNECTED = 0x03
let MQTT_CONNECTED = 0x01
let MQTT_CONNECTERR = 0x02
let DISCONNECT_MQTT = 0x15
let SUB_TOPIC_OK = 0x01
let SUB_TOPIC_Ceiling = 0x02
let microIoTStatus = ""
let WIFI_NAME = ""
let WIFI_PASSWORLD = ""
let MQTT_SERVER = ""
let MQTT_PORT = ""
let MQTT_ID = ""
let MQTT_PASSWORLD = ""
let Topic_0 = ""
let Topic_1 = ""
let Topic_2 = ""
let Topic_3 = ""
let Topic_4 = ""
let RECDATA = ""
let HTTP_IP = ""
let HTTP_PORT = ""
let microIoT_IP = "0.0.0.0"
let G_city = 0;
export enum aMotors {
//% blockId="M1" block="M1"
M1 = 0,
//% blockId="M2" block="M2"
M2 = 1,
//% blockId="ALL" block="ALL"
ALL = 2
}
export enum aServos {
//% blockId="S1" block="S1"
S1 = 0,
//% blockId="S2" block="S2"
S2 = 1
}
export enum Dir {
//% blockId="CW" block="CW"
CW = 0x0,
//% blockId="CCW" block="CCW"
CCW = 0x1
}
export enum SERVERS {
//% blockId=SERVERS_China block="EasyIOT_CN"
China,
//% blockId=SERVERS_English block="EasyIOT_EN"
English,
}
export enum TOPIC {
topic_0 = 0,
topic_1 = 1,
topic_2 = 2,
topic_3 = 3,
topic_4 = 4
}
export class PacketMqtt {
public message: string;
}
/**
* Configure the micro:IoT servo.
*/
//% weight=50
//% blockId=microIoT_ServoRun block="Servo|%index|angle|%angle"
//% angle.min=0 angle.max=180
//% index.fieldEditor="gridpicker" index.fieldOptions.columns=2
export function microIoT_ServoRun(index: aServos, angle: number): void {
let buf = pins.createBuffer(2);
if (index == 0) {
buf[0] = 0x14;
}
if (index == 1) {
buf[0] = 0x15;
}
buf[1] = angle;
pins.i2cWriteBuffer(0x16, buf);
}
/**
* Configure the direction and speed of the micro:IoT motor
*/
//% weight=49
//% blockId=microIoT_MotorRun block="Motor|%index|move|%Dir|at speed|%speed"
//% speed.min=0 speed.max=255
//% index.fieldEditor="gridpicker" index.fieldOptions.columns=2
//% direction.fieldEditor="gridpicker" direction.fieldOptions.columns=2
export function microIoT_MotorRun(index: aMotors, direction: Dir, speed: number): void {
let buf = pins.createBuffer(3);
if (index == 0) {
buf[0] = 0x00;
if (direction == 0x00) {
buf[1] = 0x01;
} else {
buf[1] = 0x00;
}
} else if (index == 1) {
buf[0] = 0x02;
buf[1] = direction;
} else if (index == 2) {
buf[0] = 0x00;
if (direction == 0x00) {
buf[1] = 0x01;
} else {
buf[1] = 0x00;
}
buf[2] = speed;
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
buf[0] = 0x02;
buf[1] = direction;
} else {
}
buf[2] = speed;
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
}
/**
* Stop the motor
*/
//% weight=48
//% blockId=microIoT_motorStop block="Motor |%motors stop"
//% motors.fieldEditor="gridpicker" motors.fieldOptions.columns=2
export function microIoT_motorStop(motors: aMotors): void {
let buf = pins.createBuffer(3);
if (motors == 0) {
buf[0] = 0x00;
} else if (motors == 1) {
buf[0] = 0x02;
} else if (motors == 2) {
buf[0] = 0x00;
buf[1] = 0;
buf[2] = 0;
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
buf[0] = 0x02;
}
buf[1] = 0;
buf[2] = 0;
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
}
function microIoT_setPara(cmd: number, para: string): void {
let buf = pins.createBuffer(para.length + 4);
buf[0] = 0x1E
buf[1] = SET_PARA
buf[2] = cmd
buf[3] = para.length
for (let i = 0; i < para.length; i++)
buf[i + 4] = para[i].charCodeAt(0)
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
}
function microIoT_runCommand(cmd: number): void {
let buf = pins.createBuffer(3);
buf[0] = 0x1E
buf[1] = RUN_COMMAND
buf[2] = cmd
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
}
function microIoT_readStatus(para: number): number {
let buf = pins.createBuffer(3);
buf[0] = 0x1E
buf[1] = READ_STATUS
buf[2] = para
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
let recbuf = pins.createBuffer(2)
recbuf = pins.i2cReadBuffer(IIC_ADDRESS, 2, false)
return recbuf[1]
}
function microIoT_readValue(para: number): string {
let buf = pins.createBuffer(3);
let paraValue = 0x00
let tempLen = 0x00
let dataValue = ""
buf[0] = 0x1E
buf[1] = READ_STATUS
buf[2] = para
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
microIoT_CheckStatus("READ_IP");
return RECDATA
}
function microIoT_ParaRunCommand(cmd: number, data: string): void {
let buf = pins.createBuffer(data.length + 4)
buf[0] = 0x1E
buf[1] = RUN_COMMAND
buf[2] = cmd
buf[3] = data.length
for (let i = 0; i < data.length; i++)
buf[i + 4] = data[i].charCodeAt(0)
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
}
function microIoT_CheckStatus(cmd: string): void {
let startTime = input.runningTime();
let currentTime = 0;
while (true) {
currentTime = input.runningTime();
if (microIoTStatus == cmd) {
serial.writeString("OKOK\r\n");
return;
}
basic.pause(50);
if (versionState == 1) {
if ((currentTime - startTime) > 20000)
return;
}
}
}
/**
* WiFi configuration
* @param SSID to SSID ,eg: "yourSSID"
* @param PASSWORD to PASSWORD ,eg: "yourPASSWORD"
*/
//% weight=100
//% blockId=microIoT_wifi block="Micro:IoT setup |Wi-Fi: |name: %SSID| password:%PASSWORD"
export function microIoT_WIFI(SSID: string, PASSWORD: string): void {
microIoT_setPara(SETWIFI_NAME, SSID)
microIoT_setPara(SETWIFI_PASSWORLD, PASSWORD)
microIoT_runCommand(CONNECT_WIFI)
microIoT_CheckStatus("WiFiConnected");
Wifi_Status = WIFI_CONNECTED
}
/**
* MQTT configuration
* @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"
*/
//% weight=100
//% blockExternalInputs=1
//% blockId=microIoT_MQTT block="Micro:IoT setup mqtt|IOT_ID(user): %IOT_ID| IOT_PWD(password) :%IOT_PWD|(default topic_0) Topic: %IOT_TOPIC| server: %SERVERS"
export function microIoT_MQTT(/*SSID: string, PASSWORD: string,*/
IOT_ID: string, IOT_PWD: string,
IOT_TOPIC: string, servers: SERVERS):
void {
if (servers == SERVERS.China) {
microIoT_setPara(SETMQTT_SERVER, OBLOQ_MQTT_EASY_IOT_SERVER_CHINA)
} else if (servers == SERVERS.English) {
microIoT_setPara(SETMQTT_SERVER, OBLOQ_MQTT_EASY_IOT_SERVER_EN)
} //else { microIoT_setPara(SETMQTT_SERVER, OBLOQ_MQTT_EASY_IOT_SERVER_GLOBAL) }
microIoT_setPara(SETMQTT_PORT, "1883")
microIoT_setPara(SETMQTT_ID, IOT_ID)
microIoT_setPara(SETMQTT_PASSWORLD, IOT_PWD)
serial.writeString("wifi conneced ok\r\n");
microIoT_runCommand(CONNECT_MQTT);
microIoT_CheckStatus("MQTTConnected");
serial.writeString("mqtt connected\r\n");
basic.pause(100)
Topic_0 = IOT_TOPIC
microIoT_ParaRunCommand(SUB_TOPIC0, IOT_TOPIC);
microIoT_CheckStatus("SubTopicOK");
serial.writeString("sub topic ok\r\n");
}
/**
* Add an MQTT subscription
*/
//% weight=200
//% blockId=microIoT_add_topic
//% block="subscribe additional %top |: %IOT_TOPIC"
//% top.fieldEditor="gridpicker" top.fieldOptions.columns=2
//% advanced=true
export function microIoT_add_topic(top: TOPIC, IOT_TOPIC: string): void {
microIoT_ParaRunCommand((top + 0x06), IOT_TOPIC);
microIoT_CheckStatus("SubTopicOK");
}
/**
* MQTT sends information to the corresponding subscription
* @param Mess to Mess ,eg: "mess"
*/
//% weight=99
//% blockId=microIoT_SendMessage block="MQTT Send Message %string| to |%TOPIC"
export function microIoT_SendMessage(Mess: string, Topic: TOPIC): void {
let topic = 0
switch (Topic) {
case TOPIC.topic_0:
topic = PUB_TOPIC0
break;
case TOPIC.topic_1:
topic = PUB_TOPIC1
break;
case TOPIC.topic_2:
topic = PUB_TOPIC2
break;
case TOPIC.topic_3:
topic = PUB_TOPIC3
break;
case TOPIC.topic_4:
topic = PUB_TOPIC4
break;
default:
break;
}
microIoT_ParaRunCommand(topic, Mess)
}
function microIoT_callback(top: TOPIC, a: Action): void {
switch (top) {
case TOPIC.topic_0:
Topic0CallBack = a;
break;
case TOPIC.topic_1:
Topic1CallBack = a;
break;
case TOPIC.topic_2:
Topic2CallBack = a;
break;
case TOPIC.topic_3:
Topic3CallBack = a;
break;
case TOPIC.topic_4:
Topic4CallBack = a;
break;
default:
break;
}
}
/**
* MQTT processes the subscription when receiving message
*/
//% weight=98
//% blockGap=60
//% blockId=obloq_mqtt_callback_user_more block="MQTT on %top |received"
//% top.fieldEditor="gridpicker" top.fieldOptions.columns=2
export function microIoT_MQTT_Event(top: TOPIC, cb: (message: string) => void) {
microIoT_callback(top, () => {
const packet = new PacketMqtt()
packet.message = RECDATA
cb(packet.message)
});
}
/**
* IFTTT configuration
* @param EVENT to EVENT ,eg: "yourEvent"
* @param KEY to KEY ,eg: "yourKey"
*/
//% weight=80
//% receive.fieldEditor="gridpicker" receive.fieldOptions.columns=3
//% send.fieldEditor="gridpicker" send.fieldOptions.columns=3
//% blockId=microIoT_http_IFTTT
//% block="Webhooks config:|event: %EVENT|key: %KEY|"
export function microIoT_http_IFTTT(EVENT: string, KEY: string): void {
microIoT_WEBHOOKS_EVENT = EVENT
microIoT_WEBHOOKS_KEY = KEY
}
function microIoT_http_wait_request(time: number): string {
if (time < 100) {
time = 100
}
let timwout = time / 100
let _timeout = 0
while (true) {
basic.pause(100)
if (microIoTStatus == "HTTP_REQUEST") {
microIoTStatus = "";
return RECDATA
} else if (microIoTStatus == "HTTP_REQUESTFailed") {
microIoTStatus = "";
return "requestFailed"
}
_timeout += 1
if (_timeout > timwout) {
return "timeOut"
}
}
}
/**
* ThingSpeak configuration
* @param KEY to KEY ,eg: "your Key"
*/
//% weight=28
//% receive.fieldEditor="gridpicker" receive.fieldOptions.columns=3
//% send.fieldEditor="gridpicker" send.fieldOptions.columns=3
//% blockId=WiFi_IoT_I2C_ThingSpeak_configura
//% block="ThingSpeak configure key: %KEY"
export function ThingSpeakConfigure(KEY: string): void {
microIoT_THINGSPEAK_KEY = KEY
}
/**
* ThingSpeak configured and sent data
* @param field1 ,eg: 2020
*/
//% weight=27
//% blockId=WiFi_IoT_I2C_ThingSpeak_Configure
//% expandableArgumentMode="enabled"
//% inlineInputMode=inline
//% block="ThingSpeak send Field1: %field1||Field2: %field2|Field3: %field3|Field4: %field4|Field5: %field5|Field6: %field6|Field7: %field7 Field8: %field8"
export function ThingSpeakSend(field1: string, field2?: string, field3?: string, field4?: string, field5?: string, field6?: string, field7?: string, field8?: string): void {
microIoT_setPara(SETHTTP_IP, OBLOQ_MQTT_EASY_IOT_SERVER_TK)
let tempStr = ""
tempStr = "update?api_key=" + microIoT_THINGSPEAK_KEY + "&field1=" + field1
// if(field2 != undefined){
// tempStr += "&field2=" + field2
// }else if(field3 != undefined){
// tempStr += "&field3=" + field3
// }else if(field4 != undefined){
// tempStr += "&field4=" + field4
// }else if(field5 != undefined){
// tempStr += "&field5=" + field5
// }else if(field6 != undefined){
// tempStr += "&field6=" + field6
// }else if(field7 != undefined){
// tempStr += "&field7=" + field7
// }else if(field8 != undefined){
// tempStr += "&field8=" + field8
// }else{
// tempStr += "\r"
// }
if (field2 != undefined)
tempStr += "&field2=" + field2
if (field3 != undefined)
tempStr += "&field3=" + field3
if (field4 != undefined)
tempStr += "&field4=" + field4
if (field5 != undefined)
tempStr += "&field5=" + field5
if (field6 != undefined)
tempStr += "&field6=" + field6
if (field7 != undefined)
tempStr += "&field7=" + field7
if (field8 != undefined)
tempStr += "&field8=" + field8
microIoT_ParaRunCommand(GET_URL, tempStr);
}
/**
* IFTTT send data
* time(ms): private long maxWait
* @param time set timeout, eg: 10000
*/
//% weight=78
//% blockId=microIoT_http_post
//% block="IFTTT(post) | value1 %value1| value2 %value2| value3 %value3| timeout(ms) %time"
export function microIoT_http_post(value1: string, value2: string, value3: string, time: number): void {
microIoT_setPara(SETHTTP_IP, microIoT_WEBHOOKS_URL)
let tempStr = ""
tempStr = "trigger/" + microIoT_WEBHOOKS_EVENT + "/with/key/" + microIoT_WEBHOOKS_KEY + ",{\"value1\":\"" + value1 + "\",\"value2\":\"" + value2 + "\",\"value3\":\"" + value3 + "\" }" + "\r"
microIoT_ParaRunCommand(POST_URL, tempStr)
}
// /**Beebotte Configure
// * @param token ,eg: "Your Channel Token"
// */
// //%weight=30
// //%blockID=WiFi_IoT_I2C_BeeBotte_Configura block="BeeBotte configura key: %token "
// export function token(token:string):void{
// microIoT_BEEBOTTE_Token = token;
// }
// /**BeeBotte send data
// * @param channel ,eg: "Your Channel Name"
// * @param resource ,eg: "Your Resource Name"
// * @param data ,eg: "Send Message"
// */
// //%weight=29
// //%blockID=WiFi_IoT_I2C_BeeBotte_sendmessage block="BeeBotte Channel: %channel Resource: %resource send value %data "
// export function sendmessage(channel:string, resource:string, data:string){
// microIoT_setPara(SETHTTP_IP, OBLOQ_MQTT_EASY_IOT_SERVER_GLOBAL)
// let tempStr = ""
// tempStr = "v1/data/write/" + channel + "/" + resource + "?token=" + microIoT_BEEBOTTE_Token +",{\"data\":" + data + "}\r\n";
// microIoT_ParaRunCommand(POST_URL, tempStr);
// }
/**
* Get IP address.
*/
//% weight=51
//% blockId=microIoT_wifi_ipconfig
//% block="ipconfig"
//% advanced=true
export function microIoT_wifi_ipconfig(): string {
return microIoT_IP;
//microIoT_readValue(READ_IP)
}
/**
* 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 microIoT_send_ping(): boolean {
let buf = pins.createBuffer(3);
buf[0] = 0x1E;
buf[1] = RUN_COMMAND;
buf[2] = SEND_PING;
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
microIoT_CheckStatus("PingOK");
return true;
}
/**
* Get the software version.time(ms): private long maxWait
* @param time to timeout, eg: 10000
*/
//% weight=50
//% blockId=microIoT_get_version
//% block="get version"
//% advanced=true
export function microIoT_get_version(): string {
let buf = pins.createBuffer(3);
buf[0] = 0x1E;
buf[1] = RUN_COMMAND;
buf[2] = GET_VERSION;
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
//microIoT_CheckStatus("READ_VERSION");
return RECDATA
}
/**
* Heartbeat request.time(ms): private long maxWait
* @param time to timeout, eg: 10000
*/
//% weight=48
//% blockId=microIoT_get_heartbeat
//% block="get heartbeat"
//% advanced=true
export function microIoT_get_heartbeat(): boolean {
return true
}
/**
* Stop the heartbeat request.
*/
//% weight=47
//% blockId=microIoT_stop_heartbeat
//% block="stop heartbeat"
//% advanced=true
export function microIoT_stop_heartbeat(): boolean {
return true
}
function microIoT_GetData(len: number): void {
RECDATA = ""
let tempbuf = pins.createBuffer(1)
tempbuf[0] = 0x22
pins.i2cWriteBuffer(IIC_ADDRESS, tempbuf);
let tempRecbuf = pins.createBuffer(len)
tempRecbuf = pins.i2cReadBuffer(IIC_ADDRESS, len, false)
for (let i = 0; i < len; i++) {
RECDATA += String.fromCharCode(tempRecbuf[i])
}
}
function microIoT_InquireStatus(): void {
let buf = pins.createBuffer(3)
let tempId = 0
let tempStatus = 0
buf[0] = 0x1E
buf[1] = READmode
buf[2] = 0x06
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
let recbuf = pins.createBuffer(2)
recbuf = pins.i2cReadBuffer(IIC_ADDRESS, 2, false)
tempId = recbuf[0]
tempStatus = recbuf[1]
switch (tempId) {
case READ_PING:
if (tempStatus == PING_OK) {
microIoTStatus = "PingOK"
} else {
microIoTStatus = "PingERR"
}
break;
case READ_WIFISTATUS:
if (tempStatus == WIFI_CONNECTING) {
microIoTStatus = "WiFiConnecting"
} else if (tempStatus == WIFI_CONNECTED) {
microIoTStatus = "WiFiConnected"
} else if (tempStatus == WIFI_DISCONNECT) {
microIoTStatus = "WiFiDisconnect"
wifiConnected++;
if (wifiConnected == 2) {
wifiConnected = 0;
microIoT_runCommand(WIFI_CONNECTED);
}
} else {
} break;
case READ_MQTTSTATUS:
if (tempStatus == MQTT_CONNECTED) {
microIoTStatus = "MQTTConnected"
mqttState = 1;
} else if (tempStatus == MQTT_CONNECTERR) {
microIoTStatus = "MQTTConnectERR"
} else if (tempStatus == 0) {//新版本修复重连
microIoT_runCommand(DISCONNECT_MQTT);
microIoT_runCommand(WIFI_CONNECTED);
}
break;
case READ_SUBSTATUS:
if (tempStatus == SUB_TOPIC_OK) {
microIoTStatus = "SubTopicOK"
} else if (tempStatus == SUB_TOPIC_Ceiling) {
microIoTStatus = "SubTopicCeiling"
} else {
microIoTStatus = "SubTopicERR"
}
break;
case READ_IP:
microIoTStatus = "READ_IP"
microIoT_GetData(tempStatus)
microIoT_IP = RECDATA
if (mqttState == 1) {
mqttState = 0;
microIoT_runCommand(DISCONNECT_MQTT);
basic.pause(200)
microIoT_runCommand(CONNECT_MQTT);
//microIoT_CheckStatus("MQTTConnected");
}
break;
case SUB_TOPIC0:
microIoTStatus = "READ_TOPICDATA"
microIoT_GetData(tempStatus)
if (Topic0CallBack != null) {
Topic0CallBack();
}
break;
case SUB_TOPIC1:
microIoTStatus = "READ_TOPICDATA"
microIoT_GetData(tempStatus)
if (Topic1CallBack != null) {
Topic1CallBack();
}
break;
case SUB_TOPIC2:
microIoTStatus = "READ_TOPICDATA"
microIoT_GetData(tempStatus)
if (Topic2CallBack != null) {
Topic2CallBack();
}
break;
case SUB_TOPIC3:
microIoTStatus = "READ_TOPICDATA"
microIoT_GetData(tempStatus)
if (Topic3CallBack != null) {
Topic3CallBack();
}
break;
case SUB_TOPIC4:
microIoTStatus = "READ_TOPICDATA"
microIoT_GetData(tempStatus)
if (Topic4CallBack != null) {
Topic4CallBack();
}
break;
case HTTP_REQUEST:
microIoTStatus = "HTTP_REQUEST"
microIoT_GetData(tempStatus)
break;
case READ_VERSION:
microIoTStatus = "READ_VERSION"
microIoT_GetData(tempStatus)
break;
default:
break;
}
basic.pause(50);
}
basic.forever(function () {
microIoT_InquireStatus();
})
/**
* Initialize OLED, just put the module in the module at the beginning of the code, no need to reuse
*/
//% weight=200
//% block="init device"
export function microIoT_initDisplay(): void {
microIoT_cmd(0xAE); // Set display OFF
microIoT_cmd(0xD5); // Set Display Clock Divide Ratio / OSC Frequency 0xD4
microIoT_cmd(0x80); // Display Clock Divide Ratio / OSC Frequency
microIoT_cmd(0xA8); // Set Multiplex Ratio
microIoT_cmd(0x3F); // Multiplex Ratio for 128x64 (64-1)
microIoT_cmd(0xD3); // Set Display Offset
microIoT_cmd(0x00); // Display Offset
microIoT_cmd(0x40); // Set Display Start Line
microIoT_cmd(0x8D); // Set Charge Pump
microIoT_cmd(0x14); // Charge Pump (0x10 External, 0x14 Internal DC/DC)
microIoT_cmd(0xA1); // Set Segment Re-Map
microIoT_cmd(0xC8); // Set Com Output Scan Direction
microIoT_cmd(0xDA); // Set COM Hardware Configuration
microIoT_cmd(0x12); // COM Hardware Configuration
microIoT_cmd(0x81); // Set Contrast
microIoT_cmd(0xCF); // Contrast
microIoT_cmd(0xD9); // Set Pre-Charge Period
microIoT_cmd(0xF1); // Set Pre-Charge Period (0x22 External, 0xF1 Internal)
microIoT_cmd(0xDB); // Set VCOMH Deselect Level
microIoT_cmd(0x40); // VCOMH Deselect Level
microIoT_cmd(0xA4); // Set all pixels OFF
microIoT_cmd(0xA6); // Set display not inverted
microIoT_cmd(0xAF); // Set display On
microIoT_clear();
let Version = microIoT.microIoT_get_version();
if (Version == "V4.1") {
versionState = 1
let buf = pins.createBuffer(3);
buf[0] = 0x1E;
buf[1] = 0x02;
buf[2] = 0x17;
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
basic.pause(2000)
}
}
/**
* OLED clear
*/
//% weight=60
//% block="clear OLED"
export function microIoT_clear() {
for (let j = 0; j < 8; j++) {
microIoT_setText(j, 0);
{
for (let i = 0; i < 16; i++) //clear all columns
{
microIoT_putChar(' ');
}
}
}
microIoT_setText(0, 0);
}
function microIoT_setText(row: number, column: number) {
let r = row;
let c = column;
if (row < 0) { r = 0 }
if (column < 0) { c = 0 }
if (row > 7) { r = 7 }
if (column > 15) { c = 15 }
microIoT_cmd(0xB0 + r); //set page address
microIoT_cmd(0x00 + (8 * c & 0x0F)); //set column lower address
microIoT_cmd(0x10 + ((8 * c >> 4) & 0x0F)); //set column higher address
}
function microIoT_putChar(c: string) {
let c1 = c.charCodeAt(0);
microIoT_writeCustomChar(basicFont[c1 - 32]);
}
/**
* @param line line num (8 pixels per line), eg: 0
* @param text value , eg: DFRobot
* OLED display string
*/
//% weight=60
//% text.defl="DFRobot"
//% line.min=0 line.max=7
//% block="OLED show text %text|on line %line"
export function microIoT_showUserText(line: number, text: string): void {
microIoT_setText(line, 0);
for (let c of text) {
microIoT_putChar(c);
}
for (let i = text.length; i < 16; i++) {
microIoT_setText(line, i);
microIoT_putChar(" ");
}
}
/**
* @param line line num (8 pixels per line), eg: 0
* @param n value , eg: 2019
* OLED shows the number
*/
//% weight=60
//% line.min=0 line.max=7
//% block="OLED show number %n|on line %line"
export function microIoT_showUserNumber(line: number, n: number): void {
microIoT.microIoT_showUserText(line, "" + n)
}
function microIoT_writeCustomChar(c: string) {
for (let i = 0; i < 8; i++) {
microIoT_writeData(c.charCodeAt(i));
}
}
function microIoT_cmd(c: number) {
pins.i2cWriteNumber(0x3C, c, NumberFormat.UInt16BE);
}
function microIoT_writeData(n: number) {
let b = n;
if (n < 0) { n = 0 }
if (n > 255) { n = 255 }
pins.i2cWriteNumber(0x3C, 0x4000 + b, NumberFormat.UInt16BE);
}
const DISPLAY_OFF = 0xAE;
const DISPLAY_ON = 0xAF;
const basicFont: string[] = [
"\x00\x00\x00\x00\x00\x00\x00\x00", // " "
"\x00\x00\x5F\x00\x00\x00\x00\x00", // "!"
"\x00\x00\x07\x00\x07\x00\x00\x00", // """
"\x00\x14\x7F\x14\x7F\x14\x00\x00", // "#"
"\x00\x24\x2A\x7F\x2A\x12\x00\x00", // "$"
"\x00\x23\x13\x08\x64\x62\x00\x00", // "%"
"\x00\x36\x49\x55\x22\x50\x00\x00", // "&"
"\x00\x00\x05\x03\x00\x00\x00\x00", // "'"
"\x00\x1C\x22\x41\x00\x00\x00\x00", // "("
"\x00\x41\x22\x1C\x00\x00\x00\x00", // ")"
"\x00\x08\x2A\x1C\x2A\x08\x00\x00", // "*"
"\x00\x08\x08\x3E\x08\x08\x00\x00", // "+"
"\x00\xA0\x60\x00\x00\x00\x00\x00", // ","
"\x00\x08\x08\x08\x08\x08\x00\x00", // "-"
"\x00\x60\x60\x00\x00\x00\x00\x00", // "."
"\x00\x20\x10\x08\x04\x02\x00\x00", // "/"
"\x00\x3E\x51\x49\x45\x3E\x00\x00", // "0"
"\x00\x00\x42\x7F\x40\x00\x00\x00", // "1"
"\x00\x62\x51\x49\x49\x46\x00\x00", // "2"
"\x00\x22\x41\x49\x49\x36\x00\x00", // "3"
"\x00\x18\x14\x12\x7F\x10\x00\x00", // "4"
"\x00\x27\x45\x45\x45\x39\x00\x00", // "5"
"\x00\x3C\x4A\x49\x49\x30\x00\x00", // "6"
"\x00\x01\x71\x09\x05\x03\x00\x00", // "7"
"\x00\x36\x49\x49\x49\x36\x00\x00", // "8"
"\x00\x06\x49\x49\x29\x1E\x00\x00", // "9"
"\x00\x00\x36\x36\x00\x00\x00\x00", // ":"
"\x00\x00\xAC\x6C\x00\x00\x00\x00", // ";"
"\x00\x08\x14\x22\x41\x00\x00\x00", // "<"
"\x00\x14\x14\x14\x14\x14\x00\x00", // "="
"\x00\x41\x22\x14\x08\x00\x00\x00", // ">"
"\x00\x02\x01\x51\x09\x06\x00\x00", // "?"
"\x00\x32\x49\x79\x41\x3E\x00\x00", // "@"
"\x00\x7E\x09\x09\x09\x7E\x00\x00", // "A"
"\x00\x7F\x49\x49\x49\x36\x00\x00", // "B"
"\x00\x3E\x41\x41\x41\x22\x00\x00", // "C"