-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUbidots.py
60 lines (51 loc) · 1.78 KB
/
Ubidots.py
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
from microbit import *
uart.init(baudrate=9600, bits=8, parity=None, stop=1, tx=pin20, rx=pin19)
def CW01(parm):
# Parm must follow this format:
# start with a '+'
# then a number 1,2,3 etc. See below
# then a seperator '@'
# then a parameter followed by a seperator '@'
# last parameter is ended with a end symbol '$'
# Commands:
# 1 for WiFi: "+1@ssid@secret$"
# 2 for login to MQTT: "+2@username@password$"
# 3 for MQTT server: "+3@server@port$"
# 4 for ubidot: "+4@device@variable@value$" (creates a MQTT publish)
# 5 for MQTT publish: "+5@topic@payload$"
# 6 for MQTT subscribe:"+6@topic$" (maximum 1 subscription)
# always start with "$" just to clean out the serial buffer
display.clear()
uart.write(parm)
sleep(500)
data = uart.readline()
while(data is None):
data = uart.readline()
# print("Something") # You can't print - it becomes input to the CW01
if int(data[:1]) == 1:
display.show(Image.YES)
elif int(data[:1]) == 0:
display.show(Image.NO)
else:
display.show(str(data)[3:4])
sleep(500)
display.show(Image.SQUARE)
sleep(2000)
uart.write("$") # Clean out Serial buffer
sleep(100)
uart.write("+9@?$") # Reboot CW01
sleep(5000)
uart.write("$") # Clean out Serial buffer
sleep(500)
CW01("+1@XinaBox@RapidIoT$")
CW01("+2@A1E-sfzxancRQIZOPixTNfcWofeJW9zdcL@?$") # UbiDot
# CW01("+2@?@?$")
CW01("+3@things.ubidots.com@1883$") # UbiDot
# CW01("+3@test.mosquitto.org@1883$")
# CW01("+3@broker.hivemq.com@1883$")
CW01("+6@/v1.6/devices/microbit/number/lv$")
while True:
CW01("+4@microbit@temperature@" + str(temperature()) + "$")
CW01("+4@microbit@a@"+str(button_a.is_pressed())+"$")
CW01("+4@microbit@b@"+str(button_b.is_pressed())+"$")
sleep(500)