diff --git a/mqtt_publish_temp.py b/mqtt_publish_temp.py index 787fb3a..b28faa1 100644 --- a/mqtt_publish_temp.py +++ b/mqtt_publish_temp.py @@ -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)