Part 2 of Many DHT11 and some Math

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 :slight_smile:

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 !

Thanks @adam and @shramik_salgaonkar

1 Like

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

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

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

MQTT_USERNAME = “”
MQTT_PASSWORD = “”
MQTT_CLIENT_ID = “”

client = cayenne.CayenneMQTTClient()
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=1883)

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()

well, i guess there is no issue with the above code.

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.

the code looks fine. remove the 60-second sleep from the top. Add the following line in your code and run the script using python name.py

import logging
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO)

see if you get any disconnect in the logs.

Change your code to this:

    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.

1 Like

My script:

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

MQTT_USERNAME = “”
MQTT_PASSWORD = “”
MQTT_CLIENT_ID = “”

client = cayenne.CayenneMQTTClient()
#client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=1883)

client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO)

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" )
    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’

Connecting to mqtt.mydevices.com:1883
temp error
humidity error
Traceback (most recent call last):
File “example1.py”, line 39, in
VPDME=((6.1078math.exp(17.08085temp11/(234.175+temp11)))-(6.1078math.exp(17.08085temp11/(234.175+temp11))*(humidity11/100)))/10.
TypeError: unsupported operand type(s) for *: ‘float’ and ‘NoneType’

that is because your return NoneType from the DHT. first check if you are getting reading from DHT.

Morning @shramik_salgaonkar,

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 :slight_smile:

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.

Try this:

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

dht = adafruit_dht.DHT11(17)

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

MQTT_USERNAME  = ""
MQTT_PASSWORD  = ""
MQTT_CLIENT_ID = ""

client = cayenne.CayenneMQTTClient()
#client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=1883)

client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO)

timestamp = 0

while True:
    client.loop()
    if (time.time() timestamp + 5):
        temp11 = dht.temperature
        humidity11 = dht.humidity
        
        while temp11 is None:
            temp11 = dht.temperature
        while humidity11 is None
            humidity11 = dht.humidity

        client.virtualWrite(1, temp11, "temp", "c" )
        client.virtualWrite(2, humidity11, "rel_hum", "p")

        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()
1 Like

Hey @adam,

Fixed a small syntax error, but I still get the following in the code:

pi@Bettie-Pi:~/python/Tomato $ python AdamDHT11.py
Traceback (most recent call last):
File “AdamDHT11.py”, line 9, in
dht = Adafruit_DHT.DHT11(17)
TypeError: ‘int’ object is not callable