DHT11/DHT22 with Raspberry Pi


Adam ,
I was just able to run the commands you sent me they worked python was not installed after they completed , i ran the driver install and it worked :slight_smile: If you could direct me to the next step it would be greatly appricated. the OS: raspbian 8 (debian) .


Just follow the rest of the steps in post 1. Let me know if you get hung up anywhere.


i started from your first post as you instructed . I installed the adafruit libary and created a nano file named tempsensor.py with your code and put in my information user id, password, client id. Whats the next step ? your instructions are very good and detailed i just have no knowledge or idea of what to do next . Do i need to install cayenne SDK ?

when i type python tempsensor.py i get this error .


Run sudo pip install paho-mqtt

Also, I changed the code yesterday so make sure to erase that file and recreate it


I just tried to run it sudo pip install paho-mqtt but it was not found please see photo. Thank you for the heads up on the update to the code I just re-copied and updated.


sudo apt-get install pip


was unable to locate package .


sudo apt-get install python-pip
sudo apt-get install python3-pip


sudo pip install paho-mqtt


it has added to my dashboard but there is no values .

i also noticed that when i type pyhton tempsenor.py

my pi seems to get locked up not sure if this helps . i fel like i am very close to getting this to work.


Yes, you are very close. Try the code below for your python code, it should spit out some values if it can actually read the sensor. keep in mind you should comment out the lines for the sensor you are not using or youll get a lot of could not read sensor prints. I have a feeling that there is an issue with the sensor or the wiring. Note that in the original post I had in the code that it is using GPIO pins 17 and 18. That is actually physical pin 11 and 12 (I have updated the post to reflect).

import paho.mqtt.client as mqtt
import time
import sys
import Adafruit_DHT

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

mqttc = mqtt.Client(client_id="")
mqttc.username_pw_set("username", password="")
mqttc.connect("mqtt.mydevices.com", port=1883, keepalive=60)

topic_dht11_temp = "v1/username/things/clientid/data/1"
topic_dht11_humidity = "v1/username/things/clientid/data/2"
topic_dht22_temp = "v1/username/things/clientid/data/3"
topic_dht22_humidity = "v1/username/things/clientid/data/4"

while True:
        humidity11, temp11 = Adafruit_DHT.read_retry(11, 17) #11 is the sensor type, 17 is the GPIO pin number (physical pin 11)
        humidity22, temp22 = Adafruit_DHT.read_retry(22, 18) #22 is the sensor type, 18 is the GPIO pin number (physical pin 12)
        if temp11 is not None:
            temp11 = "temp,c=" + str(temp11)
            print temp11
            mqttc.publish(topic_dht11_temp, payload=temp11, retain=True)
            print "DHT11 Temp not read"
        if humidity11 is not None:
            humidity11 = "rel_hum,p=" + str(humidity11)
            print humidity11
            mqttc.publish(topic_dht11_humidity, payload=humidity11, retain=True)
            print "DHT11 Humidity not read"
        if temp22 is not None:
            temp22 = "temp,c=" + str(temp22)
            print temp22
            mqttc.publish(topic_dht22_temp, payload=temp22, retain=True)
            print "DHT22 Temp not read"
        if humidity22 is not None:
            humidity22 = "rel_hum,p=" + str(humidity22)
            print humidity22
            mqttc.publish(topic_dht22_humidity, payload=humidity22, retain=True)
            print "DHT22 Humidity not read"
    except (EOFError, SystemExit, KeyboardInterrupt):


Thanks Adam,
Could you structure your Code(if possible) (for starters) like @ognqn.chikov did?
I was trying to merge your and ogngn-chikov Files into one Version with variables for USERNAME, PASSWORD & ClientID at the beginning of the Code, but it did not work…

Thx in Advance…


I think that soon Cayenne will have official support of DHT sensors:)


Yeah, no problem. I’ll try to do that tonight.


i still cant get mine not sure if i uncommented correctly , but can not add the sensor again , note i am replacing the mqtt info each time in the code when it has changed.


Hello @austin.rodriguez210 I suggest you to copy the code in some kind of IDE and there fix your indentations. They are very important for python to running! Without proper indentation, you can’t run the code.


adam I am having the same issues as he is having.
Even with these commands, can’t seem to install python. Getting these errors: (raspbian 8 (Debian))
Any help is much appreciated.

pi@raspberrypi:~ $ sudo apt-get install python3-dev
Reading package lists… Done
Building dependency tree
Reading state information… Done
python3-dev is already the newest version.
python3-dev set to manually installed.
You might want to run ‘apt-get -f install’ to correct these:
The following packages have unmet dependencies:
expect : Depends: tcl8.5 (>= 8.5.0) but it is not going to be installed
E: Unmet dependencies. Try ‘apt-get -f install’ with no packages (or specify a solution).

I have long way to go :slight_smile:


I forgot to put in the colons after the else statements.


According to your output there, you already have python installed. Are you trying to install python 2? sudo apt-get install python-dev


Yes, tried that but same output.
I can’t see python folder, there is only python_games folder :slight_smile: