DHT11/DHT22 with Raspberry Pi

Did you install all the libraries required as well? See the first post in the “About This Project” section.

hi im using this code:

import cayenne.client as cayenne
import time
import Adafruit_DHT

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)

timestamp = 0

while True:
client.loop()
if (time.time() > timestamp + 5):
humidity22, temp22 = Adafruit_DHT.read_retry(22, 17) #22 is the sensor type, 19 is the GPIO pin number that DATA wire is connected to
if temp22 is not None:
client.virtualWrite(1, temp22, cayenne.TYPE_TEMPERATURE, cayenne.UNIT_CELSIUS)
if humidity22 is not None:
client.virtualWrite(2, humidity22, cayenne.TYPE_RELATIVE_HUMIDITY, cayenne.UNIT_PERCENT)
timestamp = time.time()

yes i have installed, but i get this error when i run the script

from . import Raspberry_Pi_2_Driver as driver
ImportError: cannot import name ‘Raspberry_Pi_2_Driver’ from ‘Adafruit_DHT’

Try this

sudo pip3 install adafruit-circuitpython-dht

i tried it still getting the same error.

but if i use the code below in my python i did received a value.

import adafruit_dht
from board import *

#GPIO17
SENSOR_PIN = D17

dht22 = adafruit_dht.DHT22(SENSOR_PIN, use_pulseio=False)

temperature = dht22.temperature
humidity = dht22.humidity

print(f"Humidity= {humidity:.2f}")
print(f"Temperature= {temperature:.2f}C")

i really wanted it to be at the cayenne dashboard.

is the different library that you are using? then you can add the cayenne code to your code to send data to.

i use the code from the website, after adam ask me to try and install the adafruit-circuitpython-dht

based on the working code you shared.

import adafruit_dht
from board import *
import cayenne.client as cayenne

#GPIO17
SENSOR_PIN = D17

dht22 = adafruit_dht.DHT22(SENSOR_PIN, use_pulseio=False)

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)

timestamp = 0

while True:
client.loop()
if (time.time() > timestamp + 5):
temperature = dht22.temperature
humidity = dht22.humidity
client.virtualWrite(1, temperature, "temp", "c")
client.virtualWrite(2, humidity, "rel_hum", "p")
timestamp = time.time()

thanks for the code. manage to get connected, but no widget appears. :worried:

Im sorry is there anyway that the widget can appear?

The logs you shared earlier showed that the device was sending data on the channel and it should create a widget. can you share a screenshot of your dashboard.

hi, this is the screenshot.

can you share the log file.

hi,

actually i got this log

Connecting to mqtt.mydevices.com:1883
Connected with result code 0
SUB v1/31d40f70-721d-11e9-b4eb-6bf2c2412b24/things/376e4450-ac95-11eb-b767-3f1a8f1211ba/cmd/+

PUB v1/31d40f70-721d-11e9-b4eb-6bf2c2412b24/things/376e4450-ac95-11eb-b767-3f1a8f1211ba/sys/model
Python

PUB v1/31d40f70-721d-11e9-b4eb-6bf2c2412b24/things/376e4450-ac95-11eb-b767-3f1a8f1211ba/sys/version
1.1.0

Disconnected with result code 1
Connected with result code 0
SUB v1/31d40f70-721d-11e9-b4eb-6bf2c2412b24/things/376e4450-ac95-11eb-b767-3f1a8f1211ba/cmd/+

but i saw one of the forumer posted the solution, where he put the script inside cayenne directory. once i insert it, the log above is clear.

and when i run the script, no log appear.

Do you have two devices using same clientID? can you create a new device on cayenne dashboard by clicking Add new ---> Device/Widget ---> Arduino and use the ClientID from here in the code.

Done, still the same, no widget, no logs

If you put some prints in the script and run it in the terminal what do you see?

Put something in each if statement like this:

        if temp11 is not None:
            temp11 = "temp,c=" + str(temp11)
            Print(temp11)
            mqttc.publish(topic_dht11_temp, payload=temp11, retain=True)

hi adam, nothing appear, no logs appear in the terminal, and at cayenne the device just connected but no widget appear.

If you put a print statement in each of the if statements and got no results in the terminal then there is something wrong with the sensor or wiring.

I would suggest starting with a basic script and make sure you get results there first.

4

yes, I have to check it previously using the link that you share, and there’s a result, and I know that the wiring is ok.

tried this code that was shared, the only thing that I cant get is, it does not appear any widget in the cayenne. and no value results in the python.

import adafruit_dht
from board import *
import cayenne.client as cayenne
import time

#GPIO17
SENSOR_PIN = D17

dht22 = adafruit_dht.DHT22(SENSOR_PIN, use_pulseio=False)

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)

timestamp = 0

while True:
client.loop()
if (time.time() > timestamp + 5):
temperature = dht22.temperature
humidity = dht22.humidity
client.virtualWrite(1, temperature, “temp”, “c”)
client.virtualWrite(2, humidity, “rel_hum”, “p”)
timestamp = time.time()

then i change again the code base on your suggestion

import adafruit_dht
from board import *
import cayenne.client as cayenne
import time

#GPIO17
SENSOR_PIN = D17

dht22 = adafruit_dht.DHT22(SENSOR_PIN, use_pulseio=False)

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)

timestamp = 0

while True:
client.loop()
if (time.time() > timestamp + 5):
temperature = dht22.temperature
humidity = dht22.humidity
if temp22 is not None:
temp22 = “temp,c=” + str(temp22)
Print(temp11)
mqttc.publish(topic_dht22_temp, payload=temp22, retain=True)
if humidity22 is not None:
humidity22 = “rel_hum,p=” + str(humidity22)
Print(humidity22)
mqttc.publish(topic_dht22_humidity, payload=humidity22, retain=True)
timestamp = time.time()

and still, I didn’t get to get a widget to appear on the cayenne.