 # Part 2 of Many DHT11 and some Math

Hi All,

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

import time

import sys

import math

time.sleep(30)

clientid = “”

mqttc = mqtt.Client(client_id=clientid)

mqttc.connect(“mqtt.mydevices.com”, port=1883, keepalive=60)

mqttc.loop_start()

topic_dht11_temp = “v1/” + username + “/things/” + clientid + “/data/1”

topic_dht11_humidity = “v1/” + username + “/things/” + clientid + “/data/2”

topic_dht11_vpd = “v1/” + username + “/things/” + clientid + “/data/3”

while True:

``````try:

humidity11, temp11 = Adafruit_DHT.read_retry(11, 17)   # 11 is the sensor type, 17 is the GPIO pin number

VPD = 0

if temp11 is not None:

temp11 = "temp,c=" + str(temp11)

T = temp11

if humidity11 is not None:

humidity11 = "rel_hum,p=" + str(humidity11)

H = humidity11

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)

time.sleep(5)

except (EOFError, SystemExit, KeyboardInterrupt):

mqttc.disconnect()

sys.exit()
``````

The channel is showing up in the dashboard, but the value stays 0.

Regards

Jacques

why are you again using paho library?

I couldnt get the other one working but in the last post you said you got it working. what is the issue you are having with the cayenne python library?

THis is what I tried:

import cayenne.client as cayenne
import time

time.sleep(60) #Sleep to allow wireless to connect before starting MQTT

MQTT_CLIENT_ID = “”

client = cayenne.CayenneMQTTClient()

timestamp = 0

while True:
client.loop()
if (time.time() > timestamp + 5):
humidity11, temp11 = Adafruit_DHT.read_retry(11, 17) #11 is the sensor type, 17 is the GPIO pin number that DATA wire is connected to

``````    if temp11 is not None:
client.virtualWrite(1, temp11, cayenne.TYPE_TEMPERATURE, cayenne.UNIT_CELSIUS)
if humidity11 is not None:
client.virtualWrite(2, humidity11, cayenne.TYPE_RELATIVE_HUMIDITY, cayenne.UNIT_PERCENT)
``````
``    timestamp = time.time()``

I was referring to the import paho.mqtt.client as mqtt code from the top. But I will revisit the the code, and try and get it work before I attempt the math portion of the code.

change the following:

``````if temp11 is not None:
client.virtualWrite(1, temp11, "temp", "c" )
if humidity11 is not None:
client.virtualWrite(2, humidity11, "rel_hum", "p")
``````

what was the issue you had with cayenne code?

Previously, my PI board didnt want to connect to the dashboard.

However, I feel like a total idiot for not changing the template text as you sugested in you previous post. thats what copy and paste gets you.

Anyway, its working now So next up is the math and channel 3

I would like to calculate vapour pressure deficiency, which is basically a function of temperature and humidity, by using the MAGNUS equation.

#VPD=((6.1078EXP(17.08085T in degC/(234.175+T in degC)))-(6.1078EXP(17.08085T in degC/(234.175+T in degC))*(Humidity in %/100)))/10.

VPD =((6.1078math.e**(17.08085T/(234.175+T)))-(6.1078math.e**(17.08085T/(234.175+T))*(H/100)))/10

VPD is measured in kpa.

where did you get the python VPD formula from? This way is much better https://physics.stackexchange.com/questions/4343/how-can-i-calculate-vapor-pressure-deficit-from-temperature-and-relative-humidit/4553#4553

1 Like

It was not specifically for python, but rather a general formula from a research paper. A vary simplified vertion will look someting like this:

VPD: 610.78 x e^(T / (T +238.3) x 17.2694)) x (1 – RH/100)

As far as I undertand powers in python is denoted as math.e** but I might be mistaken.

exactly The other formula is to complex and I don’t need german precision.

someting like this ?

while True:
client.loop()
if (time.time() > timestamp + 5):
humidity11, temp11 = Adafruit_DHT.read_retry(11, 17) #11 is the sensor type, 17 is the GPIO pin number that DATA wire is connected to
VPD, SVP, AVP = (1, 1, 1) #default variables to 1

``````  if temp11 is not None:
client.virtualWrite(1, temp11, "temp", "c" )
if humidity11 is not None:
client.virtualWrite(2, humidity11, "rel_hum", "p")

#Additional if statement to calculate VPD
if VPD is not None:

# Saturation Vapor Pressure (es) =
SVP = 0.6108 * exp(17.27 * temp11 / (temp11 + 237.3))
#Actual Vapor Pressure (ea) =
AVP = humidity11 / 100 * SVP
#Vapor Pressure Deficit =
VPD = AVP - SVP
client.virtualWrite(3, VPD, "pressure", "kpa" )

timestamp = time.time()``````

not required this lines.

1 Like

I will definatly give this a try when I get back home and give feedback.

I tried it with the existing code last night, it works but intermidently. Might be the initialiseing the initial variables and also the additional if statment.

There’s nothing wrong with the paho library, you just have to make sure you are doing certain things correctly like calling the mqtt loop and not delaying long. The cayenne library handles all that for you so it’s actually easier. Your code looks fine. I have a project here that uses the paho if you want to compare.

2 Likes

Actually, I was just scanning for the basic errors. I just looked at your code again and there’s a logic error. Get rid of this line and it should work `if VPD is not None:`

2 Likes

Hi Adam, yes I implemented the suggestions that was made by Shramik and all seems to work fine. Ill run overnight just to make sure.

I also added my path to crontab for reboot.

in term of the paho lib I got it to work thanks was using it. At least I can fall back on that code.

Now I need to figure out why my DHT11 is giving me scewed results.

But this part of the code is done !

1 Like

Final code for Part 2 of Many DHT and some Math:

import cayenne.client as cayenne
import time
#add math function for VPD calculation
import math

time.sleep(60) #Sleep to allow wireless to connect before starting MQTT

MQTT_CLIENT_ID = “”

client = cayenne.CayenneMQTTClient()

timestamp = 0

while True:
client.loop()
if (time.time() > timestamp + 5):
humidity11, temp11 = Adafruit_DHT.read_retry(11, 17) #11 is the sensor type, 17 is the GPIO pin number that DATA wire is connected to

``````  if temp11 is not None:
client.virtualWrite(1, temp11, "temp", "c" )
if humidity11 is not None:
client.virtualWrite(2, humidity11, "rel_hum", "p")

#Where the magic of mathematics happen :

# Saturation Vapor Pressure (es) =
#SVP = 0.6108 * math.exp(17.27 * temp11 / (temp11 + 237.3))
#Actual Vapor Pressure (ea) =
#AVP = humidity11 / 100 * SVP
#Vapor Pressure Deficit =
#VPD = AVP - SVP
#client.virtualWrite(3, VPD, "pressure", "kpa" )

VPDME=((6.1078*math.exp(17.08085*temp11/(234.175+temp11)))-(6.1078*math.exp(17.08085*temp11/(234.175+temp11))*(humidity11/100)))/10.
client.virtualWrite(3, VPDME, "press", "kpa" )

timestamp = time.time()``````