-
Notifications
You must be signed in to change notification settings - Fork 12
/
mqtt-dht.py
54 lines (40 loc) · 1.57 KB
/
mqtt-dht.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
#!/usr/bin/env python2
import paho.mqtt.client as mqtt
import time
import Adafruit_DHT
from configparser import ConfigParser
import json
config = ConfigParser(delimiters=('=', ))
config.read('config.ini')
sensor_type = config['sensor'].get('type', 'dht22').lower()
if sensor_type == 'dht22':
sensor = Adafruit_DHT.DHT22
elif sensor_type == 'dht11':
sensor = Adafruit_DHT.dht11
elif sensor_type == 'am2302':
sensor = Adafruit_DHT.AM2302
else:
raise Exception('Supported sensor types: DHT22, DHT11, AM2302')
pin = config['sensor'].get('pin', 10)
topic = config['mqtt'].get('topic', 'temperature/dht22')
decim_digits = config['sensor'].getint('decimal_digits', 2)
sleep_time = config['sensor'].getint('interval', 60)
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code {}".format(rc))
client = mqtt.Client()
client.on_connect = on_connect
client.connect(config['mqtt'].get('hostname', 'homeassistant'),
config['mqtt'].getint('port', 1883),
config['mqtt'].getint('timeout', 60))
client.loop_start()
while True:
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
data = {'temperature': round(temperature, decim_digits),
'humidity': round(humidity, decim_digits)}
client.publish(topic, json.dumps(data))
print('Published. Sleeping ...')
else:
print('Failed to get reading. Skipping ...')
time.sleep(sleep_time)