DHT11/DHT22 with Raspberry Pi

About the error when you try to run the script using python3 - it is strange :frowning:

About the wrong measurements, Do you use pullup resistor?

yes, I`m using a 4,7k Resistor

Here is a Picture of the wiring:

I just switched from 3,3v to 5v…and the measurements seam to be Correct.

1 Like

Ok, so far so good.
I`ve edited the sudo crontab and rebooted the Pi, everything shows up just fine.

Now, the DHT22 just has to stay online…

@ognqn.chikov
I want to thank you for your help. I was struggling with this sensor for Weeks (even month)
As it seams it is working great…for now…Thank you very much!

You`ve done a great job.

Hello,
It is ok. In Monday I will receive mine, I will update you also with my results. The great job was done by everyone here :wink:
Cheers. :slight_smile:

2 Likes

Hello everyone,
Sadly my DHT22 went Offline 13hours ago.

Mine is working very good. Ah…did you added the starting of the file in crontab? One time I was running the file using sudo python3 filename and after a time the connection through Putty was interrupted and also the running of the file?

Yeah,… you’re right.
i started the script manually and forgot to edit the crontab…

I changed it from:
@reboot python /home/pi/python/tempsensor.py
to
@reboot python2 /home/pi/python/tempsensor.py

This is how it should look like, right?

Not exactly,
I think the “&” sign has to be included at the end. This will let to process other scripts.

@reboot python /home/pi/python/tempsensor.py &

or if you run it with python3:

@reboot python3 /home/pi/python/tempsensor.py &

EDIT: I still wait for my sensor to test :frowning:

Hello new to cayenne and the community I want to start of saying thanks to all ! I am also trying to use this sensors humidity reading to control my relay that I have installed . I have already successfully installed a DS18B20 . How difficult is it use use mattq and install the dht22. Any help is greatly appreciated

Austin

It is very easy. Let’s start from somewhere? Do you need help with wiring?

Hello,

this time i used:
@reboot python /home/pi/python/tempsensor.py &

…And the DHT22 stayed Online the last 24h. \o/

1 Like

Mine is ready to be fired up :slight_smile:

Hello,

Actually…It`s really easy…even in this config (MQTT Device & Running Script on the Pi)

You just connect the Sensor to the Pi:

  • 3,3v (or 5v)
  • GPIO xy
  • GND

Step 1: You create a MQTT-Device in the Cayenne Dashboard and Write down (or copy) the MQTT Informations.
Step 2: You put the Code from above into a *.py File
Step 3: You Insert your MQTT-Informations & the GPO Pin you used & save the File
Step 4: You edit the “Crontab” of the Pi so the Script(File) will start when the Pi boots up.

This is just a basic overview of the needed Steps but all in all it`s easy to set up…

Thank you for the response . I will be receiving my sensor this Friday from amazon , is this the correct sensor to purchase ? i already have a 4.7 K resistor and jumper wires . What exactly is a MQTT device ?

-Austin

Hello @austin.rodriguez210, I am using 10K pullUp resistor. I don’t know which is better and correct, but Adafruit recommend 10K resistor. Arduino recommend 4.7. It will work with both, for sure :wink:

I can describe the MQTT Device as a technology capable of connecting remote data-collecting devices. If you are curious, you can read this publication → HERE

I edited the first post to reflect the mqttc.loop() change.

Finally had some time at home to look at this a bit more. I do indeed have the mqttc.loop() line in my code (at the bottom of the try, but that should not matter), so I’m still not sure why it is still not working for me. I’ll leave this Pi on for a few days to see how it goes.

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="280a0f40-d51b-11e6-b089-9f6bfa78ab33")
mqttc.username_pw_set("95d334c0-a90b-11e6-a7c1-b395fc8a1540", password="5c5a35f990e18e06f068d19d5f56f169cce24c48")
mqttc.connect("mqtt.mydevices.com", port=1883, keepalive=60)

topic_dht11_temp = "v1/95d334c0-a90b-11e6-a7c1-b395fc8a1540/things/280a0f40-d51b-11e6-b089-9f6bfa78ab33/data/1"
topic_dht11_humidity = "v1/95d334c0-a90b-11e6-a7c1-b395fc8a1540/things/280a0f40-d51b-11e6-b089-9f6bfa78ab33/data/2"
topic_dht22_temp = "v1/95d334c0-a90b-11e6-a7c1-b395fc8a1540/things/280a0f40-d51b-11e6-b089-9f6bfa78ab33/data/3"
topic_dht22_humidity = "v1/95d334c0-a90b-11e6-a7c1-b395fc8a1540/things/280a0f40-d51b-11e6-b089-9f6bfa78ab33/data/4"

while True:
    try:
        humidity11, temp11 = Adafruit_DHT.read_retry(11, 17)
        humidity22, temp22 = Adafruit_DHT.read_retry(22, 18)

        if temp11 is not None:
            temp11 = "temp,c=" + str(temp11)
            mqttc.publish(topic_dht11_temp, payload=temp11, retain=True)
        if humidity11 is not None:
            humidity11 = "rel_hum,p=" + str(humidity11)
            mqttc.publish(topic_dht11_humidity, payload=humidity11, retain=True)
        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)
        mqttc.loop()
    except (EOFError, SystemExit, KeyboardInterrupt):
        mqttc.disconnect()
        sys.exit()
2 Likes

The issue might be the time.sleep(5) keeping the loop from processing. You could try removing that and setting the timeout to 5 seconds in the mqttc.loop() call, or using a check for elapsed time in the while loop and only running the sensor code every 5 seconds.

@adam I can test with your code for a couple of days. I can start the tests immediately :slight_smile: