DHT11/DHT22 with Raspberry Pi

#311

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

#312

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

#313

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!

#314

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

#315

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

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
#317

but it seems to show live data on line chart.

#318

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?

#319

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

#320

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

#321

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.

#322

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

cheers! @shramik_salgaonkar @adam

2 Likes