DHT11/DHT22 with Raspberry Pi

That’s what keeps the script running.

Can you post some command output for me? You can also PM me if you want.

cat /home/pi/python/tempsensor.py
cd /home/pi/python
ls -al
crontab -l
sudo crontab -l

Here we go :wink:

pi@raspberrypi:~ $ cat /home/pi/python/tempsensor.py

import paho.mqtt.client as mqtt

import time

import sys

import Adafruit_DHT

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

username = “”

password = “”

clientid = “”

mqttc = mqtt.Client(client_id=clientid)

mqttc.username_pw_set(username, password=password)

mqttc.connect(“mqtt.mydevices.comhttp://mqtt.mydevices.com”, port=1883, keepalive=60)

mqttc.loop_start()

topic_dht22_temp = “v1/” + username + “/things/” + clientid + “/data/3”

topic_dht22_humidity = “v1/” + username + “/things/” + clientid + “/data/4”

while True:

try:

    humidity22, temp22 = Adafruit_DHT.read_retry(22, 4) #22 is the sensor type, 18 is the GPIO pin number (not physical pin number)



    if temp22 is not None:

        temp22 = "temp,c=" + str(temp22)

        mqttc.publish(topic_dht22_temp, payload=temp22, retain=True)

    if humidity22 is not None:

        humidity22 = "rel_hum,p=" + str(humidity22)

        mqttc.publish(topic_dht22_humidity, payload=humidity22, retain=True)

    time.sleep(5)

except (EOFError, SystemExit, KeyboardInterrupt):

    mqttc.disconnect()

    sys.exit()

pi@raspberrypi:~ $ cd /home/pi/python

pi@raspberrypi:~/python $

pi@raspberrypi:~/python $ ls -al

total 28

drwxr-xr-x 6 pi pi 4096 Dec 27 19:53 .

drwxr-xr-x 28 pi pi 4096 Dec 27 20:16 …

drwxr-xr-x 10 pi pi 4096 Dec 27 00:46 Adafruit_Python_DHT

drwxr-xr-x 6 pi pi 4096 Feb 28 2017 Adafruit_Python_DHT-master

drwxr-xr-x 2 pi pi 4096 Dec 26 23:56 Code

drwxr-xr-x 2 pi pi 4096 Dec 26 23:55 Mydevices

-rw-r–r-- 1 pi pi 1234 Dec 27 01:11 tempsensor.py

pi@raspberrypi:~/python $

You have new mail in /var/mail/pi

pi@raspberrypi:~/python $

pi@raspberrypi:~/python $ crontab -l

@reboot python /home/pi/python/Adafruit_Python_DHT/examples/tempsensor.py & >/dev/null 2>&1 # JOB_ID_1

          • /etc/myDevices/crontab.sh # JOB_ID_2

pi@raspberrypi:~/python $

pi@raspberrypi:~/python $ sudo crontab -l

pi@raspberrypi:~/python $

pi@raspberrypi:~/python $

Note : the code is also in /home/pi/python as you asked.

and I got this « ugly » log file in /var/mail that shows are weird this is….

hope it helps

Might be easier to read this way

btw /code and /Mydevices are empty.

I can’t see the log. If you tried to attach it to your email I think it get’s stripped out.

The only thing I’m noticing is that your cron is going to the wrong location. /home/pi/python/Adafruit_Python_DHT/examples/tempsensor.py

@pierre.gauvin any luck?

Hi, I’m new to the community.

I can successfully monitor the values on the dashboard.

How can i write a python script using mqtt to subscribe to the temperature topic and retrieve data from the dashboard?

Does this work for Raspi Zero W?

When i try running the python script i get an error about the Raspberry Pi 2 library:

pi@raspberrypi:~/Adafruit_Python_DHT $ python tempsensor.py
Traceback (most recent call last):
File “tempsensor.py”, line 23, in
humidity22, temp22 = Adafruit_DHT.read_retry(22, 18) #22 is the sensor type, 18 is the GPIO pin number (not physical pin number)
File “/home/pi/Adafruit_Python_DHT/Adafruit_DHT/common.py”, line 94, in read_retry
humidity, temperature = read(sensor, pin, platform)
File “/home/pi/Adafruit_Python_DHT/Adafruit_DHT/common.py”, line 80, in read
platform = get_platform()
File “/home/pi/Adafruit_Python_DHT/Adafruit_DHT/common.py”, line 48, in get_platform
from . import Raspberry_Pi
File “/home/pi/Adafruit_Python_DHT/Adafruit_DHT/Raspberry_Pi.py”, line 22, in
from . import Raspberry_Pi_Driver as driver
ImportError: cannot import name Raspberry_Pi_Driver
Exception in thread Thread-1 (most likely raised during interpreter shutdown):
Traceback (most recent call last):
File “/usr/lib/python2.7/threading.py”, line 801, in __bootstrap_inner
File “/usr/lib/python2.7/threading.py”, line 754, in run
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py”, line 2650, in _thread_main
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py”, line 1481, in loop_forever
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py”, line 1003, in loop
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py”, line 1277, in loop_read
<type ‘exceptions.TypeError’>: ‘NoneType’ object is not callable

I don’t have a DHT sensor with me right now but I just tried on a Zero W and it ran fine. I found this stack page that suggests you may have not installed the library, can you try reinstalling? If that doesn’t help try running the scrip outside the Adafruit library directory as mentioned here

@brandonczx I’m assuming you mean you want to connect a device that uploads data and then another device that connects and reads that data? If so, you can only have one device connected per MQTT device. If you have more than one you will see issues with disconnects. Unfortunately there’s no way to do it right now that I know of, but maybe someone else has an idea. I’ll ask around.

Hello, I cant seem to get this to work. Has anyone had trouble getting it to work with Raspbian Stretch? I get an error code import error: No Module named “adafruit_DHT”
thanks.

Did you install the library with the following commands?

sudo pip install paho-mqtt
sudo apt-get install build-essential python-dev python-openssl
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo python setup.py install

Yes, can the RaspberryPi also be running the cayenne agent also or just the MQTT?

adam Leader
January 14 |

Did you install the library with the following commands?
sudo pip install paho-mqtt
sudo apt-get install build-essential python-dev python-openssl
git clone GitHub - adafruit/Adafruit_Python_DHT: Python library to read the DHT series of humidity and temperature sensors on a Raspberry Pi or Beaglebone Black.
cd Adafruit_Python_DHT
sudo python setup.py install
Visit Topic or reply to this email to respond.

In Reply To

adbrig4
January 14 |

Hello, I cant seem to get this to work. Has anyone had trouble getting it to work with Raspbian Stretch? I get an error code import error: No Module named “adafruit_DHT” thanks.
Visit Topic or reply to this email to respond.

To unsubscribe from these emails, click here.

As far as I know it can, I don’t think I have tested. I’ll try it out and let you know.

Got it working, i didnt have the MQTT credentials entered in the various locations. Thanks for the tutorial!!!

Colby

1 Like

I’ve got the Cayenne agent running along MQTT on multiple Pi zero’s and it works great. I am running the 11.27.2017 version of Stretch.

2 Likes

Hello thanks for all the good info here! I got my pi up and running with my DHT22 (using the code from the first post ) it ran for about 20 hours. Now I can not get it to work(no data on my Cayenne dashboard for temp/humidity) The temp/humidity on my dashboard flashes offline then it seems like its connected and flashes offline again it just repeats this. I have Crontab setup, rebooting did not fix anything I tried manually starting it but that didn’t work either.

Thanks

Glad to hear it @c0lby !

~Benny

Did you use the same mqtt credentials on another device or another raspberry Pi? I’ve seen this happen where someone used the same credentials across multiple devices, which can mess with the connectivity.

~Benny

I’m thinking the same thing Benny said. If you reuse the client ID it will disconnect/reconnect often.