change mechanism to run

removed the loop, needs to be executed by cron
This commit is contained in:
robert 2021-02-18 09:46:13 +01:00
parent 3a4d974a89
commit 3a2d1ec06f
1 changed files with 18 additions and 35 deletions

View File

@ -36,43 +36,26 @@ time_topic = 'weathersensors/' + client_id + '/time'
#client.username_pw_set(mqtt_username, mqtt_password) # MQTT server credentials #client.username_pw_set(mqtt_username, mqtt_password) # MQTT server credentials
client.connect(mqtt_broker) # MQTT server address 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: print(dt_string)
# DATETIME # Get Temperature from DHT
now = datetime.now() _, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
#print("now =", now)
#Output format: DD/MM/YYYY H:M:S
dt_string = now.strftime("%d-%m-%Y %H:%M:%S")
print(dt_string) temperature = round(float(temperature),2)
# Get Temperature from DHT
_, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
print("temperature before cut: " + str(temperature)) print("temperature after round: " + temperature)
# cut temperature to xx.xx if more then tow digits after "." # Get Humidity from DHT
t_split = str(temperature).split(".") humidity, _ = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
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 cut: " + temperature) humidity = round(float(humidity),2)
# Get Humidity from DHT
humidity, _ = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
print("humidity before cut: " + str(humidity)) print("humidity after round: " + humidity)
# cut humidity to xx.xx if more then tow digits after "." # Publish our data
h_split = str(humidity).split(".") client.publish(temperature_topic, temperature)
if len(h_split[1]) > 2: client.publish(humidity_topic, humidity)
h_cut = len(h_split[1]) - 2 client.publish(time_topic, dt_string)
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)