This post will build on what I have learned from my previous post First Post - How to get a DHT11 running for longer than 6 hours
So basically with help of Ninja Shramik, I got the DHT11 working pretty sweet. So next up is to do some math and send that data to my dashboard as channel 3 (1 and 2 is for temperature and the humidity).
Hacking my previous code, this is what I have so far:
import paho.mqtt.client as mqtt
#Adding additional line for calculation - import math library
username = “”
password = “”
clientid = “”
mqttc = mqtt.Client(client_id=clientid)
mqttc.connect(“mqtt.mydevices.com”, port=1883, keepalive=60)
topic_dht11_temp = “v1/” + username + “/things/” + clientid + “/data/1”
topic_dht11_humidity = “v1/” + username + “/things/” + clientid + “/data/2”
#Adding additional line for calculating VPD
topic_dht11_vpd = “v1/” + username + “/things/” + clientid + “/data/3”
try: humidity11, temp11 = Adafruit_DHT.read_retry(11, 17) # 11 is the sensor type, 17 is the GPIO pin number #Adding additional line to zero all my variables: VPD = 0 if temp11 is not None: temp11 = "temp,c=" + str(temp11) mqttc.publish(topic_dht11_temp, payload=temp11, retain=True) #add temp variable for VPD T = temp11 if humidity11 is not None: humidity11 = "rel_hum,p=" + str(humidity11) mqttc.publish(topic_dht11_humidity, payload=humidity11, retain=True) #add humidity variable for VPD H = humidity11 #Adding additional if statements to the python script if VPD is not None: #Using the Magnus Equation to calculate vapour pressure defficiency (VPD) #VPD=((6.1078*EXP(17.08085*T in degC/(234.175+T in degC)))-(6.1078*EXP(17.08085*T in degC/(234.175+T in degC))*(Humidity in %/100)))/10. VPD =((6.1078*math.e**(17.08085*T/(234.175+T)))-(6.1078*math.e**(17.08085*T/(234.175+T))*(H/100)))/10 VPD = "vpd,kpa=" + str(VPD) mqttc.publish(topic_dht11_vpd, payload=VPD, retain=True) time.sleep(5) except (EOFError, SystemExit, KeyboardInterrupt): mqttc.disconnect() sys.exit()
The channel is showing up in the dashboard, but the value stays 0.