-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpi_am2301.py
executable file
·72 lines (63 loc) · 2.47 KB
/
rpi_am2301.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
66
67
68
69
70
71
72
#!/usr/bin/python3
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Installation: python3 -m pip install homeassistant-mqtt-binding adafruit-circuitpython-bmp3xx adafruit-circuitpython-sgp30 adafruit-circuitpython-dht
from paho.mqtt.client import Client
from ha_mqtt.mqtt_thermometer import MqttThermometer
from ha_mqtt.mqtt_device_base import MqttDeviceSettings
from ha_mqtt.ha_device import HaDevice
import time
import board
import adafruit_dht
import datetime
import logging
logging.basicConfig(level=logging.DEBUG)
def on_log(mqttc, obj, level, string):
print("[{}] {} - {} - {}".format(datetime.datetime.now(),level,string,obj))
# instantiate an paho mqtt client and connect to the mqtt server
client = Client("StudyRPi")
client.connect("homeassistant.local", 1883)
# client.loop_start()
client.on_log = on_log
# instantiate an MQTTThermometer object
dev = HaDevice("Study AM2301", "StudyAM2301")
th = MqttThermometer(MqttDeviceSettings("Study AM2301 Temperature", "StudyTemp",client,dev),"°C")
hum = MqttThermometer(MqttDeviceSettings("Study AM2301 Humidity", "StudyHumidity",client,dev),"%")
#th = MqttThermometer("Study", "StudyTemp",client)
# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D17)
# you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio.
# This may be necessary on a Linux single board computer like the Raspberry Pi,
# but it will not work in CircuitPython.
# dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)
success = False
for i in range(10):
try:
# Print the values to the serial port
temperature_c = dhtDevice.temperature
humidity = dhtDevice.humidity
temp = f"{temperature_c:2.2f}"
#print(f"publishing temperature: {temp} {th.unit_of_measurement}")
th.publish_state(temp)
hum.publish_state(humidity)
success = True
print(
"[{}] Published Temp: {} Humidity: {}".format(datetime.datetime.now(),temp, humidity)
)
time.sleep(2.0)
break
except RuntimeError as error:
# Errors happen fairly often, DHT's are hard to read, just keep going
print("RuntimeError: {}".format(error.args[0]))
time.sleep(2.0)
continue
except Exception as error:
dhtDevice.exit()
raise error
print("stopping loop")
client.loop_stop()
dhtDevice.exit()
#th.close()
#hum.close()
client.disconnect()
print("closed connection")