Part 2 of Many DHT11 and some Math

Was working but now, I get

Disconnected with result code 1
Connected with result code 0
SUB v1/da9e1390-e819-11e9-8221-599f77add412/things/11c78520-f5cd-11e9-a38a-d57172a4b4d4/cmd/+
PUB v1/da9e1390-e819-11e9-8221-599f77add412/things/11c78520-f5cd-11e9-a38a-d57172a4b4d4/sys/model Python
PUB v1/da9e1390-e819-11e9-8221-599f77add412/things/11c78520-f5cd-11e9-a38a-d57172a4b4d4/sys/version 1.1.0
PUB v1/da9e1390-e819-11e9-8221-599f77add412/things/11c78520-f5cd-11e9-a38a-d57172a4b4d4/data/1 temp,c=13
PUB v1/da9e1390-e819-11e9-8221-599f77add412/things/11c78520-f5cd-11e9-a38a-d57172a4b4d4/data/2 lum,lux=130
PUB v1/da9e1390-e819-11e9-8221-599f77add412/things/11c78520-f5cd-11e9-a38a-d57172a4b4d4/data/3 bp,hpa=813
Disconnected with result code 1
Caught exception in on_connect: Connection failed, not authorized
Disconnected with result code 5
Caught exception in on_connect: Connection failed, not authorized
Disconnected with result code 5
Caught exception in on_connect: Connection failed, not authorized
Disconnected with result code 5
Caught exception in on_connect: Connection failed, not authorized
Disconnected with result code 5
Caught exception in on_connect: Connection failed, not authorized
Disconnected with result code 5
Caught exception in on_connect: Connection failed, not authorized
Disconnected with result code 5
Caught exception in on_connect: Connection failed, not authorized
Disconnected with result code 5
Caught exception in on_connect: Connection failed, not authorized
Disconnected with result code 5
Caught exception in on_connect: Connection failed, not authorized
Disconnected with result code 5
Caught exception in on_connect: Connection failed, not authorized
Disconnected with result code 5
Caught exception in on_connect: Connection failed, not authorized
Disconnected with result code 5

Ok I deleted my origional sensor widger and added a new widget for the example. Working again now

great. now try this code and see if you get DHt reading:

ok working, humidity is way off but the sensor works…

Temp=22.0*C Humidity=17.0%

now combine both the codes:

#!/usr/bin/env python
import cayenne.client
import time
import logging

import Adafruit_DHT

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = "xxx"
MQTT_PASSWORD  = "xxxx"
MQTT_CLIENT_ID = "xxx"


sensor = Adafruit_DHT.DHT11

pin = 23

# The callback for when a message is received from Cayenne.
def on_message(message):
print("message received: " + str(message))
# If there is an error processing the message return an error string, otherwise return nothing.

client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO)
# For a secure connection use port 8883 when calling client.begin:
# client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883, loglevel=logging.INFO)

i=0
timestamp = 0

while True:
client.loop()
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

if (time.time() > timestamp + 10):
    client.virtualWrite(3, 10, "temp", "c" )
        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")
    timestamp = time.time()
    i = i+1

the final code works for me:

#!/usr/bin/env python
import cayenne.client
import time
import logging
import math

import Adafruit_DHT

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = "xxxx"
MQTT_PASSWORD  = "xxxx"
MQTT_CLIENT_ID = "xxx"


sensor = Adafruit_DHT.DHT11

pin = 18

# The callback for when a message is received from Cayenne.
def on_message(message):
    print("message received: " + str(message))
    # If there is an error processing the message return an error string, otherwise return nothing.

client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO)
# For a secure connection use port 8883 when calling client.begin:
# client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883, loglevel=logging.INFO)

i=0
timestamp = 0

while True:
    client.loop()
    humidity11, temp11 = Adafruit_DHT.read_retry(sensor, pin)
#    client.virtualWrite(1, temp11, "temp", "c" )

    if (time.time() > timestamp + 10):
        client.virtualWrite(1, 10, "temp", "c" )
        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")
        if temp11 is not None and humidity11 is not None:
            VPDME=((6.1078*math.exp(17.08085*temp11/(234.175+temp11)))-(6.1078*math.exp(17.08085*temp11/(234.175+temp11))*(humidity11/100)))/10.
            print(VPDME)
            client.virtualWrite(2, humidity11, "pressure", "kpa")
        else:
            print("VPDME error")
        timestamp = time.time()
        i = i+1

Quick Question:

I see both the temp and timer is writing the channel 1:

    if (time.time() > timestamp + 10):
        client.virtualWrite(1, 10, "temp", "c" )
        if temp11 is not None:
            client.virtualWrite(1, temp11, "temp", "c" )

Also, the next to statements are also writing to the same channel:

        if humidity11 is not None:
            client.virtualWrite(2, humidity11, "rel_hum", "p")
        else:
            print("humidity error")
        if temp11 is not None and humidity11 is not None:
            VPDME=((6.1078*math.exp(17.08085*temp11/(234.175+temp11)))-(6.1078*math.exp(17.08085*temp11/(234.175+temp11))*(humidity11/100)))/10.
            print(VPDME)
            client.virtualWrite(2, humidity11, "pressure", "kpa")

Is it safe to say I can leave the first if as is and change the humidity and vpdme to:

        if humidity11 is not None:
            client.virtualWrite(2, humidity11, "rel_hum", "p")
        else:
            print("humidity error")
        if temp11 is not None and humidity11 is not None:
            VPDME=((6.1078*math.exp(17.08085*temp11/(234.175+temp11)))-(6.1078*math.exp(17.08085*temp11/(234.175+temp11))*(humidity11/100)))/10.
            print(VPDME)
            client.virtualWrite(3, VPDME, "pressure", "kpa")

I get this traceback when I run the script:

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

how is that possible. you said it was working previously. Can you just copy and paste code from combine code to the code that was working.

it is not included in the code, it is commented.

Sorry, my bad. You need to use a different channel numbers for each widget and change humidity and vpdme.

Ill run through the last code again and let you know in a moment.

Ok so what I found was"

running script with sudo python shramik.py i get (same without sudo)

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

running script with sudo python3 shramik.py i get:

Traceback (most recent call last):
File “shramik.py”, line 6, in
import Adafruit_DHT
ImportError: No module named ‘Adafruit_DHT’

running sudo python3 Example-03-CayenneClient.py works perfectly no issues.

adafruit/Adafruit_Python_DHT/blob/master/examples/simpletest.py Also works perfectly

Another difference is that I am running the two scripts from different location, one is in /home/pi/python/ and the other is /home/pi/Cayenne-MQTT-Python

more info:

pi@Bettie-Pi:~ $ python
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
[GCC 6.3.0 20170516] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

maybe something to do with the shebang #!/usr/bin/env python ?

try sudo pip3 install Adafruit_DHT and then run sudo python3 shramik.py

Do I run that command in the existing Adafruit folder ? Also, I see my version of python is 2.7 will the python3 command worlk ?

if this works then?? you can run it anywhere you want.

Thanks for the help I really do appreciate it, just frustrating that the seperaly the work perfectly but combined there is a issue.

did you try this?

Not yet, will try when I get home from work work. Will let you know.