From 3a2d1ec06ff5d27dc992e8d02d7d7f5945a21924 Mon Sep 17 00:00:00 2001 From: robert Date: Thu, 18 Feb 2021 09:46:13 +0100 Subject: [PATCH] change mechanism to run removed the loop, needs to be executed by cron --- mqtt_publish_temp.py | 53 +++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 35 deletions(-) diff --git a/mqtt_publish_temp.py b/mqtt_publish_temp.py index a003889..d4e193b 100644 --- a/mqtt_publish_temp.py +++ b/mqtt_publish_temp.py @@ -36,43 +36,26 @@ time_topic = 'weathersensors/' + client_id + '/time' #client.username_pw_set(mqtt_username, mqtt_password) # MQTT server credentials client.connect(mqtt_broker) # MQTT server address -client.loop_start() +# 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") -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") +print(dt_string) +# Get Temperature from DHT +_, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN) - print(dt_string) - # Get Temperature from DHT - _, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN) +temperature = round(float(temperature),2) - print("temperature before cut: " + str(temperature)) - # cut temperature to xx.xx if more then tow digits after "." - t_split = str(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] +print("temperature after round: " + temperature) +# Get Humidity from DHT +humidity, _ = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN) - print("temperature after cut: " + temperature) - # Get Humidity from DHT - humidity, _ = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN) +humidity = round(float(humidity),2) - print("humidity before cut: " + str(humidity)) - # cut humidity to xx.xx if more then tow digits after "." - h_split = str(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] - - print("humidity after cut: " + humidity) - # Publish our data - client.publish(temperature_topic, temperature) - client.publish(humidity_topic, humidity) - client.publish(time_topic, dt_string) - - time.sleep(30) \ No newline at end of file +print("humidity after round: " + humidity) +# Publish our data +client.publish(temperature_topic, temperature) +client.publish(humidity_topic, humidity) +client.publish(time_topic, dt_string) \ No newline at end of file