-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathubidot.py
50 lines (42 loc) · 1.31 KB
/
ubidot.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
#Chanbroset, Andreas, Bryan
from umqtt.robust import MQTTClient
import machine as m
import ubinascii
import network
import time
import dht
from machine import Pin
from time import sleep
sta_if = network.WLAN(network.STA_IF); sta_if.active(True)
sta_if.scan() # Scan for available access points
sta_if.active(True)
sta_if.connect("AIU-WIFI", "") # Connect to an AP
sta_if.isconnected()
time.sleep(3)
ubidotsToken = "BBFF-SPONj6AlHG36Okio1VdmISf9jX55G2"
clientID = ubinascii.hexlify(m.unique_id())
client = MQTTClient("clientID", "industrial.api.ubidots.com", 1883, user = ubidotsToken, password = ubidotsToken)
def checkwifi():
while not sta_if.isconnected():
time.sleep_ms(500)
print("Connecting WiFI failed")
sta_if.connect()
pin13 = m.Pin(13, m.Pin.IN, m.Pin.PULL_UP)
d = dht.DHT11(Pin(32, Pin.IN))
def publish():
hum = 0
temp = 0
while True:
checkwifi()
client.connect()
d.measure()
temp = d.temperature()
# print("Temperature: ", temp, "°C")
hum = d.humidity()
# print("Humidity: ", hum, "g.kg^-1")
sleep(1)
msg = b'{"temp":%s, "hum":%s, "dht": {"context":{"hum": %s, "temp": %s}}}' % (temp, hum, hum, temp)
print(msg)
client.publish(b"/v1.6/devices/ESP32", msg)
time.sleep(10)
publish()