[CLOSED] Raspberry pi with DHT22 sensor ;NOT GETTING ANY PAYLOAD

i’m able to connect to losant with my raspberry pi . But i can’t see any payloads ;always shows listening in logs .my code is

import json
from time import sleep
from losantmqtt import Device
import Adafruit_DHT


device = Device("ID", "KEY", "SECRET")

humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 13)


def sendDeviceState():
   print("Sending Device State")
   print(temperature)
   device.send_state({"Temperature": temperature,"Humidity":humidity})

if humidity is not None and temperature is not None:
sendDeviceState()

device.connect(blocking=True)

Sensor is working fine.

Hey @Arjun_Biju,

It might be because you are are calling sendDeviceState before device.connect(blocking=True)

So your device is trying to send data before actually connecting to losant. Maybe try something like this:

from time import sleep
from losantmqtt import Device
import Adafruit_DHT

device = Device("ID", "KEY", "SECRET")

humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 13)

def sendDeviceState():
   print("Sending Device State")
   print(temperature)
   device.send_state({"Temperature": temperature,"Humidity":humidity})

device.connect(blocking=False)

while True:
    device.loop()
    if device.is_connected():
        sendDeviceState()
    time.sleep(1)

Or, if you don’t like the loop method, you can always use the add_event_observer to listen for the "connect" event and run your sendDeviceState then.

I grabbed this based on the example on our Github page for the python library:

1 Like

Thanks . i did that now everything is working fine

Awesome! Happy to help :slight_smile:

1 Like