DHT11/DHT22 with Raspberry Pi

I have to save and check all the others systems, to put it again then. Now I can’t, but I will try again later

when you delete a device all the data related to it will also be deleted.

Hi! thanks for the post, It was really straightforward to implement, everything worked as expected, but after installing I wanted to see the charts and I added them to two devices one for temp and one for humidity, One of the two worked just fine but the other always shows “No data available for this period” but it does show the live data. I reseted the project and installed them again but got the same result, any idea on what can be happening?

Thanks!

are you using the correct data type? can you share the code you are using?

Hi @shramik_salgaonkar, thank you very much for the answer, this is the python code on my pi, literally copied from this thread, as you see I’ve duplicated the code to support 2 sensors, but the issue was present before.
Let me know If I can provide more info to help you help me solve the issue :slight_smile:

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 = "xxx"
password = "xxx"
clientid = "xxx"

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_dht11_temp = "v1/" + username + "/things/" + clientid + "/data/1"
topic_dht11_humidity = "v1/" + username + "/things/" + clientid + "/data/2"
topic_dht11_temp_2 = "v1/" + username + "/things/" + clientid + "/data/3"
topic_dht11_humidity_2 = "v1/" + username + "/things/" + clientid + "/data/4"
#topic_dht22_temp = "v1/" + username + "/things/" + clientid + "/data/3"
#topic_dht22_humidity = "v1/" + username + "/things/" + clientid + "/data/4"

while True:
    try:
        humidity11, temp11 = Adafruit_DHT.read_retry(11, 17) #11 is the sensor type, 17 is the GPIO pin number (not physical pin number)
        humidity11_2, temp11_2 = Adafruit_DHT.read_retry(11, 27) #11 is the sensor type, 17 is the GPIO pin number (not physical pin number)
        #humidity22, temp22 = Adafruit_DHT.read_retry(22, 18) #22 is the sensor type, 18 is the GPIO pin number (not physical pin number)
        
        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 temp11_2 is not None:
            temp11_2 = "temp,c=" + str(temp11_2)
            mqttc.publish(topic_dht11_temp_2, payload=temp11_2, retain=True)
        if humidity11_2 is not None:
            humidity11_2 = "rel_hum,p=" + str(humidity11_2)
            mqttc.publish(topic_dht11_humidity_2, payload=humidity11_2, 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)
    except (EOFError, SystemExit, KeyboardInterrupt):
        mqttc.disconnect()
        sys.exit()

Try this, it should print out the values from the sensor so you can see if it is actually reading the values or not

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 = "xxx"
password = "xxx"
clientid = "xxx"

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_dht11_temp = "v1/" + username + "/things/" + clientid + "/data/1"
topic_dht11_humidity = "v1/" + username + "/things/" + clientid + "/data/2"
topic_dht11_temp_2 = "v1/" + username + "/things/" + clientid + "/data/3"
topic_dht11_humidity_2 = "v1/" + username + "/things/" + clientid + "/data/4"
#topic_dht22_temp = "v1/" + username + "/things/" + clientid + "/data/3"
#topic_dht22_humidity = "v1/" + username + "/things/" + clientid + "/data/4"

while True:
    try:
        humidity11, temp11 = Adafruit_DHT.read_retry(11, 17) #11 is the sensor type, 17 is the GPIO pin number (not physical pin number)
        humidity11_2, temp11_2 = Adafruit_DHT.read_retry(11, 27) #11 is the sensor type, 17 is the GPIO pin number (not physical pin number)
        #humidity22, temp22 = Adafruit_DHT.read_retry(22, 18) #22 is the sensor type, 18 is the GPIO pin number (not physical pin number)
        
        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)
        print("temp11_2: " + str(temp11_2) + "humidity11_2: " + str(humidity11_2))
        if temp11_2 is not None:
            temp11_2 = "temp,c=" + str(temp11_2)
            mqttc.publish(topic_dht11_temp_2, payload=temp11_2, retain=True)
        if humidity11_2 is not None:
            humidity11_2 = "rel_hum,p=" + str(humidity11_2)
            mqttc.publish(topic_dht11_humidity_2, payload=humidity11_2, 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)
    except (EOFError, SystemExit, KeyboardInterrupt):
        mqttc.disconnect()
        sys.exit()
1 Like

but it seems to show live data on line chart.

Actually, I think I misunderstood what your problem is at first. Does the live data always show the correct value but the week, month, etc not work?

Indeed, live data works all the time but never get the historical data, it seems like its not being stored hee you can see a clip of the issue https://youtu.be/2ks9EX4xRuY @shramik_salgaonkar @adam

can you first try this code, for both your DHT and get the reading https://github.com/adafruit/DHT-sensor-library/blob/master/examples/DHTtester/DHTtester.ino

I have fixed the issue. can you check it now. do not use two widget on same channel. if you want use two separate channel for each.

That worked, understood, two widgets on the same channel can create conflicts, than you for the awsome support!

cheers! @shramik_salgaonkar @adam

2 Likes

Hello,

I have followed the instructions perfectly however I’m not getting anything to show in my dashboard.

This is what I have uploaded:

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 = ""
password = ""
clientid = ""

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_dht11_temp = "v1/" + username + "/things/" + clientid + "/data/1"
topic_dht11_humidity = "v1/" + username + "/things/" + clientid + "/data/2"
topic_dht22_temp = "v1/" + username + "/things/" + clientid + "/data/3"
topic_dht22_humidity = "v1/" + username + "/things/" + clientid + "/data/4"

while True:
    try:
        humidity11, temp11 = Adafruit_DHT.read_retry(11, 18) #11 is the sensor type, 17 is the GPIO pin number (not physical pin number)
        humidity22, temp22 = Adafruit_DHT.read_retry(22, 17) #22 is the sensor type, 18 is the GPIO pin number (not physical pin number)
        
        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)
    except (EOFError, SystemExit, KeyboardInterrupt):
        mqttc.disconnect()
        sys.exit()

Im running this on a fresh install on both my RPI and Cayenne.
my DHT22 is connected to 5v, Ground and GPIO17.

Any help would be greatly appreciated!

Try this:

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 = ""
password = ""
clientid = ""

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_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, 17) #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)
            print("Temp: ")
            print(temp22)
            mqttc.publish(topic_dht22_temp, payload=temp22, retain=True)
        if humidity22 is not None:
            humidity22 = "rel_hum,p=" + str(humidity22)
            print("Hum: ")
            print(humidity22)
            mqttc.publish(topic_dht22_humidity, payload=humidity22, retain=True)
        time.sleep(5)
    except (EOFError, SystemExit, KeyboardInterrupt):
        mqttc.disconnect()
        sys.exit()

Save that to dht22.py. cd to the directory you have that file in then run it with python dht22.py Do you get any temps printed?

Also, just saw you are using 5v for power. Should be using 3.3v.

Hello Adam,

Thank you for getting back to me so quickly!
Im getting nothing printed and nothing showing on the dash.

I’ve switched the voltage from 5v to 3v, thank you for pointing that out for me :smile:

55

Ok, so that means you’re not getting any readings from the DHT22 in the script. Can you take a pic of how you have everything set up? Also, I’m on the myDevices slack if you want to connect there.

you may want to follow this setup https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging and see if you are getting reading from the DHT11