DHT11/DHT22 with Raspberry Pi


I am almost now I only get the error below.

pi@raspberrypi:~ $ bash /home/pi/python/tempsensor.py
import-im6.q16: unable to open X server ' @ error/import.c/ImportImageCommand/358. import-im6.q16: unable to open X server’ @ error/import.c/ImportImageCommand/358.
import-im6.q16: unable to open X server ' @ error/import.c/ImportImageCommand/358. import-im6.q16: unable to open X server’ @ error/import.c/ImportImageCommand/358.
/home/pi/python/tempsensor.py: line 6: syntax error near unexpected token 30' /home/pi/python/tempsensor.py: line 6:time.sleep(30) #Sleep to allow wireless to connect before starting

I also created my own sensor in Cayene for my password and that sort of thing.


root@raspberrypi:~# sudo su
root@raspberrypi:~# cd /home/pi/python
root@raspberrypi:/home/pi/python# import tempsensor.py
import-im6.q16: unable to open X server `’ @ error/import.c/ImportImageCommand/358


Looks like you are not running the code correctly. Go up to the first post and read the Code section again. In the end you need to save the code to a file and then run it with python /path/to/file.py


Hello, I’m new to Python code and confused on the saving home/pi/python/ tempsensor.py. I’m I supposed to make the new folders in the Pi desktop or is there a way to do it with terminal commands?


from terminal use sudo nano tempsensor.py to create a file and to execute use sudo python tempsensor.py


Thanks, what should I do about an error I get? It say file “tempsensor.py”,line 70
sys.exit () import Pago.mqtt.clent as mqtt

Syntax error : invalid syntax.


it should be Paho not Pago.

can you copy paste the code you are using.


Oops sorry, I missed typed that on the page here. Im trying to get the code from Ognqn.Chikov
for the DHT22 to work it reads out in the terminal but the device on the Dashboard says wait for live Data?


here’s the code

import cayenne.client as cayenne
import time
import Adafruit_DHT

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


client = cayenne.CayenneMQTTClient()

timestamp = 0

while True:
    if (time.time() > timestamp + 5):
        humidity11, temp11 = Adafruit_DHT.read_retry(11, 18) #11 is the sensor type, 18 is the GPIO pin number that DATA wire is connected to
        humidity22, temp22 = Adafruit_DHT.read_retry(22, 19) #22 is the sensor type, 19 is the GPIO pin number that DATA wire is connected to

        if temp11 is not None:
            client.virtualWrite(1, temp11, cayenne.TYPE_TEMPERATURE, cayenne.UNIT_CELSIUS)
        if humidity11 is not None:
            client.virtualWrite(2, humidity11, cayenne.TYPE_RELATIVE_HUMIDITY, cayenne.UNIT_PERCENT)
        if temp22 is not None:
            client.virtualWrite(3, temp22, cayenne.TYPE_TEMPERATURE, cayenne.UNIT_CELSIUS)
        if humidity22 is not None:
            client.virtualWrite(4, humidity22, cayenne.TYPE_RELATIVE_HUMIDITY, cayenne.UNIT_PERCENT)
        timestamp = time.time()


so you share screenshot of the error.


Ok, sorry for the confusion. I rebooted the Raspberry Pi 3 model B+ I’ve been trying this on the temp widget finally came up on the dashboard with the green plug sign in the corner and then later the humidity widget showed up.I wonder if I’m having some kind of internet problems. the IOS app works fine but the online dashboard is very slow with chrome and will not load with Internet Explorer. Internet speed test are good.


Hello, I am new to the all of this, so please feel free to laugh and point out any of my newb errors.

I’m running this on a Raspberry Pi 3 model B v1.2. I’m running the code thru Thonny IDE as a test. I’m getting

Traceback (most recent call last);
File “/home/pi/python/tempsensor.py”, line 4, in
import Adafruit_DHT
ImportError: No module named ‘Adafruit_DHT’

I verified the folder is there and there are files in it. I also deleted the folder and re-ran the last three commands in the original post.


run this commands:

git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo python setup.py install


I renamed the previous Adafruit_Python_DHT to Adafruit_Python_DHT-old and ran the commands. I did see any errors come up. However I am still getting the same error.


Sorry, need to make a correction. I didn’t see any errors during the process.


@ZeroKB it is working now?


Sorry for the confusion. the installation of the python lib was successful. I am still getting the same import error when trying to run it in Thonny IDE.


can you try running using the command window?


I ran python /home/pi/python/tempsensor.py in a command windows. I am getting “connected with result code 0”. So the same as 6f6326e7 got back Feb. but I’m only getting one line, where 6f6326e7 had multiple lines. This is also waiting for an hour.


this means connection accepted.
lets try a different approach now https://github.com/myDevicesIoT/Cayenne-MQTT-Python . run the following command:

git clone https://github.com/myDevicesIoT/Cayenne-MQTT-Python
cd Cayenne-MQTT-Python
python setup.py install

once done with installation navigate to example folder and open Example-03-CayenneClient.py using any editor.
Next, on your dashboard add a new device using Bring your own thing. copy the MQTT credential into the Example-03-CayenneClient.py file.
Save and exit.
Run this file sudo python Example-03-CayenneClient.py
your cayenne dashboard should be populated with three widgets. do till here and see if you can connect to cayenne succesfully, then let move on to DTH.