Dht12

Hi,
I’ve got the errno121 with this code, any ideas?:

#This programe reads sensor DHT12 and sends the data to Cayenne trought MQTT

import paho.mqtt.client as mqtt
import time
import sys
import smbus

addr     = 0x5C

bus = smbus.SMBus(1)

time.sleep(1) #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_dht12_temp = "v1/" + username + "/things/" + clientid + "/data/1"
topic_dht12_humidity = "v1/" + username + "/things/" + clientid + "/data/2"

while True:
    try:
        data = bus.read_i2c_block_data(addr,0x00,5)
        temp12=str(data[2]) #+ str(data[3])
        humidity12=str(data[0])# + str(data[1])
       
        if temp12 is not None:
            temp12 = "temp,c=" + str(temp12)
            mqttc.publish(topic_dht12_temp, payload=temp12, retain=True)
        if humidity12 is not None:
            humidity12 = "rel_hum,p=" + str(humidity12)
            mqttc.publish(topic_dht12_humidity, payload=humidity12, retain=True)

        time.sleep(1)
    except (EOFError, SystemExit, KeyboardInterrupt):
        mqttc.disconnect()
        sys.exit()

Errno121

check if your device is detected i2cdetect -y 1

yes, it works, but only one time
I can see the temperature and the humidity on the dashboard but just one
then the error comes up

import adafruit_dht
from board import *
import cayenne.client as cayenne
import time

#GPIO17
SENSOR_PIN = D17

dht22 = adafruit_dht.DHT22(SENSOR_PIN, use_pulseio=False)

time.sleep(30) #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):
temp22 = dht22.temperature
humidity22 = dht22.humidity

if temp22 is not None:
#temp22 = “temp,c=” + str(temp22)
print(temp22)
client.virtualWrite(1, temp22, “temp”, “c”)
else:
print(“Sensor error”)

if humidity22 is not None:
#humidity22 = “rel_hum,p=” + str(humidity22)
print(humidity22)
client.virtualWrite(2, humidity22, “rel_hum”, “p”)
else:
print(“Sensor error”)

timestamp = time.time()

thanks but this is not dht22 is dht12

well, cant you change the code to work with dht12 ?

I mean dht22 works with 1-wire
dht12 works with i2c

they are not the same

DHT11 and DHT22 work the same. Change the line below
dht22 = adafruit_dht.DHT22(SENSOR_PIN, use_pulseio=False)
to
dht11 = adafruit_dht.DHT11(SENSOR_PIN, use_pulseio=False)

I have dht 12

NOT 11

NOT 22

thanks