implement fix
This commit is contained in:
parent
6d06b429d5
commit
3c4d8d392c
@ -10,12 +10,6 @@ import Adafruit_DHT
|
|||||||
import paho.mqtt.publish as publish
|
import paho.mqtt.publish as publish
|
||||||
import paho.mqtt.client as paho
|
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 credentials
|
||||||
#mqtt_username = 'homeassistant' # MQTT client username
|
#mqtt_username = 'homeassistant' # MQTT client username
|
||||||
#mqtt_password = '3355' # MQTT client password
|
#mqtt_password = '3355' # MQTT client password
|
||||||
@ -45,14 +39,29 @@ client.connect(mqtt_broker) # MQTT server address
|
|||||||
client.loop_start()
|
client.loop_start()
|
||||||
|
|
||||||
while True:
|
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
|
# Get Temperature from DHT
|
||||||
_, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
|
_, 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
|
# Get Humidity from DHT
|
||||||
humidity, _ = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
|
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
|
# Publish our data
|
||||||
client.publish(temperature_topic, temperature)
|
client.publish(temperature_topic, temperature)
|
||||||
client.publish(humidity_topic, humidity)
|
client.publish(humidity_topic, humidity)
|
||||||
|
Loading…
Reference in New Issue
Block a user