DHT11/DHT22 with Raspberry Pi


#210

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

#211

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.


#212

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

hope it helps


#213

Might be easier to read this way

btw /code and /Mydevices are empty.


#214

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


#215

@pierre.gauvin any luck?


#216

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?


#217

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


#218

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


#219

@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.


#220

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.


#221

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

#222

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 https://github.com/adafruit/Adafruit_Python_DHT.git
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.


#223

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