Part 2 of Many DHT11 and some Math

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

Ah, yeah…sorry was looking at 2 different libraries and mixed them together.

Try this instead:

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
counter = 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
               
            
        while temp11 is not None and humidity11 is not None and counter < 50:
            counter += 1
            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" )      

        counter = 0
        timestamp = time.time()
1 Like

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

Traceback (most recent call last):
File “AdamDHT11.py”, line 18, in
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO)
TypeError: begin() got an unexpected keyword argument ‘loglevel’

With: client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=1883)

pi@Bettie-Pi:~/python/Tomato $ python AdamDHT11.py
Connecting to mqtt.mydevices.com:1883
Connected with result code 0
Disconnected with result code 1

I don’t know about the log level, I was just using your code from above. Add some print statements to make sure the DHT is even working. Adding time.sleep(0.1) to the end of the while loop that checks the DHT values will also give you more time to check the sensor.

try this:

cd Cayenne-MQTT-Python
sudo python3 setup.py install

then navigate to cd Cayenne-MQTT-Python/examples
open sudo nano Example-03-CayenneClient.py
Add your MQTT credentials into it.
and run using sudo python3 Example-03-CayenneClient.py

once done see if can connect successfully. then lets move forward with sensor code.

Hey Adam, I know the DHT is working as I have used the code you proposed and the code @shramik_salgaonkar proposed, both of which runs smooth. The whole mess started with me adding some math to the script.

I have working code for the sensor, that is not the issue. It’s just the equation at the bottom that tends to stop the script.

This code works beatifully:

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  = "da9e1390-e819-11e9-8221-599f77add412"
MQTT_PASSWORD  = "e47c058455fd5cea016746d4b6e90a8f3bf66978"
MQTT_CLIENT_ID = "11c78520-f5cd-11e9-a38a-d57172a4b4d4"

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
          
        print('Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(temp11, humidity11))  
            
        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")
         
        # 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 =
        #VPDME = AVP - SVP
 
        
        #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()

Displays:

Disconnected with result code 1
Disconnected with result code 5
Disconnected with result code 5
Temp=22.0* Humidity=17.0%
Disconnected with result code 5
Disconnected with result code 5
Disconnected with result code 5
Disconnected with result code 5
Temp=22.0* Humidity=17.0%
Disconnected with result code 5
Disconnected with result code 5
Disconnected with result code 5
Temp=22.0* Humidity=23.0%
Disconnected with result code 5
Disconnected with result code 5
Disconnected with result code 5
Temp=22.0* Humidity=21.0%
Disconnected with result code 5
Disconnected with result code 5

Hmm:

ok so this happens after a while:

Disconnected with result code 1
Connected with result code 0
SUB v1/MQTT_USERNAME2/things/MQTT_CLIENT_ID/cmd/+

PUB v1/MQTT_USERNAME2/things/MQTT_CLIENT_ID/sys/model

Traceback (most recent call last):
  File "TestDHT11.py", line 28, in <module>
    print('Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(temp11, humidity11))  
ValueError: Unknown format code 'f' for object of type 'str'

can you try what i suggested? then lets move ahead step by step.

Yes, I have it working

is your device connected now?