implement fix

This commit is contained in:
robert 2021-02-15 23:17:09 +01:00
parent 6d06b429d5
commit 3c4d8d392c
1 changed files with 18 additions and 9 deletions

View File

@ -10,12 +10,6 @@ import Adafruit_DHT
import paho.mqtt.publish as publish
import paho.mqtt.client as paho
# DATETIME
now = datetime.now()
#print("now =", now)
#Output format: DD/MM/YYYY H:M:S
dt_string = now.strftime("%d-%m-%Y %H:%M:%S")
# MQTT credentials
#mqtt_username = 'homeassistant' # MQTT client username
#mqtt_password = '3355' # MQTT client password
@ -45,14 +39,29 @@ client.connect(mqtt_broker) # MQTT server address
client.loop_start()
while True:
# DATETIME
now = datetime.now()
#print("now =", now)
#Output format: DD/MM/YYYY H:M:S
dt_string = now.strftime("%d-%m-%Y %H:%M:%S")
# Get Temperature from DHT
_, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
temperature = round(float(temperature), 2)
# cut temperature to xx.xx if more then tow digits after "."
t_split = temperature.split(".")
if len(t_split[1]) > 2:
t_cut = len(t_split[1]) - 2
t_split[1] = t_split[1][:-t_cut]
temperature = t_split[0] + "." + t_split[1]
# Get Humidity from DHT
humidity, _ = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
humidity = round(float(humidity), 2)
# cut humidity to xx.xx if more then tow digits after "."
h_split = humidity.split(".")
if len(h_split[1]) > 2:
h_cut = len(h_split[1]) - 2
h_split[1] = h_split[1][:-h_cut]
humidity = h_split[0] + "." + h_split[1]
# Publish our data
client.publish(temperature_topic, temperature)
client.publish(humidity_topic, humidity)