-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
63 lines (51 loc) · 1.88 KB
/
main.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
import time
import board
import adafruit_dht
import paho.mqtt.client as mqtt
import yaml
with open('config.yml', 'r') as file:
config = yaml.safe_load(file)
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, reason_code, properties):
if config['debug'] == True :
print(f"Connected with result code {reason_code}")
def publish(temperature, humidity):
mqttc.publish(config['mqtt_temp_topic'], temperature)
mqttc.publish(config['mqtt_hum_topic'], humidity)
read_period = config['read_period'] #seconds
max_reporting_period = config['max_reporting_period'] #seconds
count = 0
old_temperature = 0
old_humidity = 0
#sensor = adafruit_dht.DHT22(board.D4)
sensor = adafruit_dht.DHT11(board.D4)
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqttc.on_connect = on_connect
if config['mqtt_tls'] == True :
mqttc.tls_set()
mqttc.username_pw_set(username=config['mqtt_user'],password=config['mqtt_password'])
mqttc.connect(config['mqtt_host'], config['mqtt_port'], 60)
mqttc.loop_start()
while True:
try:
temperature = sensor.temperature
humidity = sensor.humidity
count = count + read_period
if count > max_reporting_period or old_temperature != temperature or old_humidity != humidity:
count = 0
publish(temperature, humidity)
old_temperature = temperature
old_humidity = humidity
if config['debug'] == True :
print("Temp={0:0.1f}ºC, Humidity={1:0.1f}%".format(temperature, humidity))
except RuntimeError as error:
# Errors happen fairly often, DHT's are hard to read, just keep going
if config['debug'] == True :
print(error.args[0])
time.sleep(2.0)
continue
except Exception as error:
sensor.exit()
raise error
time.sleep(read_period)
mqttc.loop_stop()