-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.py
45 lines (32 loc) · 1.38 KB
/
publish.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
#!/usr/bin/python
import paho.mqtt.client as mqtt
import time
import os
import sys
import datetime
import Adafruit_DHT
thingid = os.getenv('thingid','default')
brokeraddr = os.getenv('brokeraddr','openhabian')
refresh = int(os.getenv('refresh', '5'))
pin = int(os.getenv('pin', '4'))
thingTopic = "jumajumo/" + thingid + "/"
client = mqtt.Client(thingid)
client.will_set(thingTopic + "sys/state", "OFFLINE", qos=1, retain=True)
client.connect(brokeraddr, keepalive=600)
client.loop_start()
client.publish(thingTopic, str(datetime.datetime.now()), qos=1, retain=True)
client.publish(thingTopic + "sys/type", "sensor", qos=1, retain=True)
client.publish(thingTopic + "sys/device", "dht22", qos=1, retain=True)
client.publish(thingTopic + "env/thingid", thingid, qos=1, retain=True)
client.publish(thingTopic + "env/brokeraddr", brokeraddr, qos=1, retain=True)
client.publish(thingTopic + "env/refresh", refresh, qos=1, retain=True)
client.publish(thingTopic + "env/pin", pin, qos=1, retain=True)
try:
while True:
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, pin)
client.publish(thingTopic + "sys/state", "ONLINE", qos=2, retain=True)
client.publish(thingTopic + "humidity",humidity, qos=1, retain=False)
client.publish(thingTopic + "temperature", temperature, qos=1, retain=False)
time.sleep(refresh)
except:
client.disconnect()