-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht22.py
65 lines (48 loc) · 1.67 KB
/
dht22.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
61
62
63
64
65
#!/usr/bin/python3
from time import sleep
import Adafruit_DHT
from paho.mqtt import client as mqtt_client
port = 1883
broker = "192.168.10.18"
client_id = "lothlorien-python-mqtt"
#broker = "mosquitto"
#client_id = "ithilien-python-mqtt"
def connect_mqtt():
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code {}\n".format(rc))
def on_disconnect(client, userdata, rc):
client.connect(broker, port, keepalive=60)
client = mqtt_client.Client(client_id)
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.connect(broker, port, keepalive=60)
return client
def publish(client, topic, msg):
result = client.publish(topic, msg)
status = result[0]
if status == 0:
print("Sent `{}` to topic `{}`".format(msg, topic))
else:
raise RuntimeError("Failed to send message to topic {}".format(topic))
def measure():
while True:
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 4)
print("Temp: {0:0.1f} C Humidity: {1:0.1f} %".format(temperature, humidity))
publish(client, "/sensors/living_room/dht22/temperature", temperature)
publish(client, "/sensors/living_room/dht22/humidity", humidity)
sleep(60)
if __name__ == "__main__":
while True:
client = connect_mqtt()
client.loop_start()
while True:
try:
measure()
except Exception as error:
print("Exception: ", error)
break
print("Retrying in 5 min")
sleep(300)