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()
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.
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:
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()
Hey @shramik_salgaonkar the pi seems to stop after a while with the above-mentioned code (cayenne.client). App says server crashed, web app says offline, raspberry is online but DHT11 is offline.
For some reason I think its the calculation part that causes the hangup.
When I add the equation to my initial code “top of page” it says can multiply int with float.
if temp11 is not None:
client.virtualWrite(1, temp11, "temp", "c" )
else:
print("temp error")
if humidity11 is not None:
client.virtualWrite(2, humidity11, "rel_hum", "p")
else:
print("humidity error")
I’m assuming the problem is here. If you get errors printed then it will be confirmed. Your formula is probably failing because one or more of the DHT variables has an error. If that is the case you can put the line that reads the DHT in a loop until you get 2 valid readings. In my project I don’t worry about it because it sends the value every 5 seconds. Even if it goes 10 readings without a valid value no one will notice it. In your case it is required to have both readings.
import cayenne.client as cayenne
import time
import Adafruit_DHT #add math function for VPD calculation
import math #add logging function
import logging
#time.sleep(60) #Sleep to allow wireless to connect before starting MQTT
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" )
else:
print("temp error")
if humidity11 is not None:
client.virtualWrite(2, humidity11, "rel_hum", "p")
else:
print("humidity error")
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, "pressure", "kpa" )
timestamp = time.time()
Type Error:
pi@Bettie-Pi:~/python/Tomato $ python example1.py
Traceback (most recent call last):
File “example1.py”, line 19, in
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO)
TypeError: begin() got an unexpected keyword argument ‘loglevel’
I definitely get readings from the DHT, however I belive it is as @adam mentioned, if it only reads and sends the DHT values then its fine as no one wil notice if you have a error somewhere. The temperature is usually fine but for the DHT11 the humidity readings are pretty bad, however this is besides the point.
But for my case also as @adam mentioned I need two values otherwise there will be an error. What I cant understand is that why does the scipt stop once this error is present ? I would assume the bit stream will return garbage and as soon as there are 2 correct values it will update the value to the calculated one. I can set up a trigger to reset the device every couple of minutes but this is a fools way of doing it.
Surely there must be an easy way of doing this, its like taking volts and current to calculate power ? Why is this so difficult
if you check the error, the DHT is giving temp and humidity error, which means you are not getting any reading. Thus you are getting NoneType error for your calculation.