DHT11/DHT22 with Raspberry Pi


#169

Mine is working ok.


#170

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


#171

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()

#172

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.


#173

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


#174

Thanks a lot guys :relaxed::+1:


#175

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.


#176

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…


#178

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:

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.


#179

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)


#180

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.


#181

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:


#182

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.


#183

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

Daniel


#184

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


#185

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


#186

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()

#187

@michal any luck with that code?


#188

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


#189

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