Offline/online raspberry DHT22 every second


#1

Hi
i have a raspberry and i installed a DHT22
i receive values normally but the device keep connecting/disconnecting all the time…
can someone help?
because of this problem i cannot control any other GPIO from the panel!!..
i run the default code :

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, 04)

        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(10)
    except (EOFError, SystemExit, KeyboardInterrupt):
        mqttc.disconnect()
        sys.exit()

#2

you can use the cayenne python library https://github.com/myDevicesIoT/Cayenne-MQTT-Python in case of the above to send DHT data to cayenne.


#3

Hi Shramik.
I use this library already…
Do you see any issues concerning the timers of my script?
do you see any other issues concerning the code?


#4

i am not sure why your code is disconnecting. it would be better if you could provide some log. is the disconnect still happening with cayenne python library code?


#5

Looks like he is using the code from my project. If I had to guess @raceableability has another device using the same MQTT credentials. Try making a new device and using those.