DHT11/DHT22 with Raspberry Pi

Mine is working ok.

Hello againā€¦

My DHT22 stopped working again and wonā€™t come online, even after a reboot.
I want to start over with the latest code but iā€™m not sure if the first post was updated with the changes mentioned in this thread.

can someone link me to the right Post? or can i follow the steps in the First Post?

Greetings

Try with this:

import cayenne.client as cayenne
import time
import Adafruit_DHT


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

MQTT_USERNAME  = ""
MQTT_PASSWORD  = ""
MQTT_CLIENT_ID = ""

client = cayenne.CayenneMQTTClient()
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)

timestamp = 0

while True:
    client.loop()
    if (time.time() > timestamp + 5):
        humidity22, temp22 = Adafruit_DHT.read_retry(22, 18) #22 is the sensor type, 18 is the GPIO pin number


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

Hey @doerek! The first post is up to date, but now that I look at it it doesnā€™t include the username/password/clientid mod I wanted to make. You can certainly use it as it is though. Iā€™ll update it here in a couple minutes after I test and make sure itā€™s working.

1 Like

@doerek I updated the code in the first post to make it easier to insert your username/password/clientID

1 Like

Thanks a lot guys :relaxed::+1:

1 Like

My DHT22 is working again, thank you.
unfortunately since a few Weeks ago, the sensor sends only false measurementsā€¦

Is there anything i can do to except shortening the Cable?
In the moment Iā€™m using a 3-4m Cable.

1 Like

Maybe the sensor has gone? Try to hook it directly. If the measurements are the same, maybe the sensor has gone and you need a new oneā€¦

Hello again

indeedā€¦
iā€™ve tested the sensor with a ~2m Cable and itā€™s working fine. shouldā€™ve tried that earlierā€¦

But i have another Question. I recently came across this article:
https://www.raspinews.com/dht11-raspberry-pi-cayenne/

Compared to the code in the first Postā€¦wich approach should new User take?
just curiousā€¦because the script seems to be different at some points.

Being a newbie myself I used the code above for my first MQTT application for DHT11 last night - I can recommend it for new users ( just watch the indents in python code as pointed out earlier in this thread)

1 Like

The code in the first post is the most up to date, use that. The code in the link looks good too, just another way to do it.

I suggest to you to use the code in the first post. There are a lot ways to do it, but for a new user I think it is the best :wink:

By the way, I updated the first post a bit over the last couple days. I realized it probably wasnā€™t as clear as it could be in some places so I added pictures and better descriptions.

2 Likes

Adam you are the Man!! I got it working thanks to youā€¦ This is great stuff. Keep it up.

Daniel

2 Likes

Hi, could you copy here code for 4 sensors? I am trying but it is not workingā€¦;/ thank you

I agree @ccdanieldb, @adam is the man :slight_smile:

@michal code for 4 dht11 sensors? Also curious also how you are wiring those up?

~B

1 Like

I didnā€™t test this so apologies if there are errors. Give it a try and let me know. You will need to edit lines 8, 9, 10, 28, 29, 30, and 31.

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

username = "MQTT Username From Dashboard"
password = "MQTT Passsword From Dashboard"
clientid = "MQTT Client ID From Dashboard"

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

topic_dht22_1_temp = "v1/" + username + "/things/" + clientid + "/data/1"
topic_dht22_1_humidity = "v1/" + username + "/things/" + clientid + "/data/2"
topic_dht22_2_temp = "v1/" + username + "/things/" + clientid + "/data/3"
topic_dht22_2_humidity = "v1/" + username + "/things/" + clientid + "/data/4"
topic_dht22_3_temp = "v1/" + username + "/things/" + clientid + "/data/5"
topic_dht22_3_humidity = "v1/" + username + "/things/" + clientid + "/data/6"
topic_dht22_4_temp = "v1/" + username + "/things/" + clientid + "/data/7"
topic_dht22_4_humidity = "v1/" + username + "/things/" + clientid + "/data/8"

while True:
    try:
        humidity22_1, temp22_1 = Adafruit_DHT.read_retry(22, x) #22 is the sensor type, x is the GPIO pin number (not physical pin number)
        humidity22_2, temp22_2 = Adafruit_DHT.read_retry(22, x) #22 is the sensor type, x is the GPIO pin number (not physical pin number)
        humidity22_3, temp22_3 = Adafruit_DHT.read_retry(22, x) #22 is the sensor type, x is the GPIO pin number (not physical pin number)
        humidity22_4, temp22_4 = Adafruit_DHT.read_retry(22, x) #22 is the sensor type, x is the GPIO pin number (not physical pin number)
        
        if temp22_1 is not None:
            temp22_1 = "temp,c=" + str(temp22_1)
            mqttc.publish(topic_dht22_1_temp, payload=temp22_1, retain=True)
        if humidity2_1 is not None:
            humidity22_1 = "rel_hum,p=" + str(humidity22_1)
            mqttc.publish(topic_dht22_1_humidity, payload=humidity22_1, retain=True)
        if temp22_2 is not None:
            temp22_2 = "temp,c=" + str(temp22_2)
            mqttc.publish(topic_dht22_2_temp, payload=temp22_2, retain=True)
        if humidity22_2 is not None:
            humidity22_2 = "rel_hum,p=" + str(humidity22_2)
            mqttc.publish(topic_dht22_2_humidity, payload=humidity22_2, retain=True)
        if temp22_3 is not None:
            temp22_3 = "temp,c=" + str(temp22_3)
            mqttc.publish(topic_dht22_3_temp, payload=temp22_3, retain=True)
        if humidity22_3 is not None:
            humidity22_3 = "rel_hum,p=" + str(humidity22_3)
            mqttc.publish(topic_dht22_3_humidity, payload=humidity22_3, retain=True)
        if temp22_4 is not None:
            temp22_4 = "temp,c=" + str(temp22_4)
            mqttc.publish(topic_dht22_4_temp, payload=temp22_4, retain=True)
        if humidity22_4 is not None:
            humidity22_4 = "rel_hum,p=" + str(humidity22_4)
            mqttc.publish(topic_dht22_4_humidity, payload=humidity22_4, retain=True)
        time.sleep(5)
    except (EOFError, SystemExit, KeyboardInterrupt):
        mqttc.disconnect()
        sys.exit()
1 Like

@michal any luck with that code?

1 Like

I only have two sensors, so I tested with two. It is working properly.

1 Like

just what i needed to clear just what i needed to clear some confusion.
Thanks

2 Likes