DHT,MQTT,Cayenne help

Hi there,
I am new in the community so please forgive my stupidity…maybe
I adopted script from Adam for measurement temperature and humidity using cayenne and dht22 sensors, I have a big terrarium now working automatically on raspberry 3 after my previous system started to make mistakes and I want to add sprayers reacting automatically to humidity…

There are 4 DHT22 sensors conntected to GPIO 16,20,21,26 and I altered the code according to your manual bud I keep getting some error, the script never started to send data to cayenne…

I hope you will give me a hint , if possible, where should I keep searching for solution.
Thank you in advance, nice day!
eaf9ad048a7359bdf1b81bad20d6f83bede4325f_1_690x390

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_dht1_temp = “v1/” + username + “/things/” + clientid + "/data/1"
topic_dht1_humidity = “v1/” + username + “/things/” + clientid + "/data/2"
topic_dht2_temp = “v1/” + username + “/things/” + clientid + "/data/3"
topic_dht2_humidity = “v1/” + username + “/things/” + clientid + "/data/4"
topic_dht3_temp = “v1/” + username + “/things/” + clientid + "/data/5"
topic_dht3_humidity = “v1/” + username + “/things/” + clientid + "/data/6"
topic_dht4_temp = “v1/” + username + “/things/” + clientid + "/data/7"
topic_dht4_humidity = “v1/” + username + “/things/” + clientid + “/data/8”

while True:
try:
humidity1, temp1 = Adafruit_DHT.read_retry(22, 16) #11 is the sensor type, 17 is the GPIO pin number (not physical pin number)
humidity2, temp2 = Adafruit_DHT.read_retry(22, 20) #22 is the sensor type, 18 is the GPIO pin number (not physical pin number)
humidity3, temp3 = Adafruit_DHT.read_retry(22, 21) #11 is the sensor type, 17 is the GPIO pin number (not physical pin number)
humidity4, temp4 = Adafruit_DHT.read_retry(22, 26) #22 is the sensor type, 18 is the GPIO pin number (not physical pin number)

    if temp1 is not None:
        temp1 = "temp,c=" + str(temp1)
        mqttc.publish(topic_dht1_temp, payload=temp1, retain=True)
    if humidity1 is not None:
        humidity1 = "rel_hum,p=" + str(humidity1)
        mqttc.publish(topic_dht1_humidity, payload=humidity1, retain=True)
    if temp2 is not None:
        temp2 = "temp,c=" + str(temp2)
        mqttc.publish(topic_dht2_temp, payload=temp2, retain=True)
    if humidity2 is not None:
        humidity2 = "rel_hum,p=" + str(humidity2)
        mqttc.publish(topic_dht2_humidity, payload=humidity2, retain=True)
 if temp3 is not None:
        temp3 = "temp,c=" + str(temp3)
        mqttc.publish(topic_dht3_temp, payload=temp3, retain=True)
    if humidity3 is not None:
        humidity3 = "rel_hum,p=" + str(humidity3)
        mqttc.publish(topic_dht3_humidity, payload=humidity3, retain=True)
    if temp4 is not None:
        temp4 = "temp,c=" + str(temp4)
        mqttc.publish(topic_dht4_temp, payload=temp4, retain=True)
    if humidity4 is not None:
        humidity4 = "rel_hum,p=" + str(humidity4)
        mqttc.publish(topic_dht4_humidity, payload=humidity4, retain=True)
    time.sleep(5)
except (EOFError, SystemExit, KeyboardInterrupt):
    mqttc.disconnect()
    sys.exit()

@adam :slight_smile:

It could be just the way you pasted the code in, but if you look back you can see some of your quotes are “” and some are “” (there is a difference) Also, your indentation is not correct. One other thing I noticed is that you deleted the end of the try statement. It’s fine if you don;t want it in there but it does close down cleanly on reboots, etc if it;s in there. 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_dht1_temp = "v1/" + username + "/things/" + clientid + "/data/1"
topic_dht1_humidity = "v1/" + username + "/things/" + clientid + "/data/2"
topic_dht2_temp = "v1/" + username + "/things/" + clientid + "/data/3"
topic_dht2_humidity = "v1/" + username + "/things/" + clientid + "/data/4"
topic_dht3_temp = "v1/" + username + "/things/" + clientid + "/data/5"
topic_dht3_humidity = "v1/" + username + "/things/" + clientid + "/data/6"
topic_dht4_temp = "v1/" + username + "/things/" + clientid + "/data/7"
topic_dht4_humidity = "v1/" + username + "/things/" + clientid + "/data/8"

while True:
	humidity1, temp1 = Adafruit_DHT.read_retry(22, 16) #11 is the sensor type, 17 is the GPIO pin number (not physical pin number)
	humidity2, temp2 = Adafruit_DHT.read_retry(22, 20) #22 is the sensor type, 18 is the GPIO pin number (not physical pin number)
	humidity3, temp3 = Adafruit_DHT.read_retry(22, 21) #11 is the sensor type, 17 is the GPIO pin number (not physical pin number)
	humidity4, temp4 = Adafruit_DHT.read_retry(22, 26) #22 is the sensor type, 18 is the GPIO pin number (not physical pin number)

	if temp1 is not None:
		temp1 = "temp,c=" + str(temp1)
		mqttc.publish(topic_dht1_temp, payload=temp1, retain=True)
	if humidity1 is not None:
		humidity1 = "rel_hum,p=" + str(humidity1)
		mqttc.publish(topic_dht1_humidity, payload=humidity1, retain=True)
	if temp2 is not None:
		temp2 = "temp,c=" + str(temp2)
		mqttc.publish(topic_dht2_temp, payload=temp2, retain=True)
	if humidity2 is not None:
		humidity2 = "rel_hum,p=" + str(humidity2)
		mqttc.publish(topic_dht2_humidity, payload=humidity2, retain=True)
	if temp3 is not None:
		temp3 = "temp,c=" + str(temp3)
		mqttc.publish(topic_dht3_temp, payload=temp3, retain=True)
	if humidity3 is not None:
		humidity3 = "rel_hum,p=" + str(humidity3)
		mqttc.publish(topic_dht3_humidity, payload=humidity3, retain=True)
	if temp4 is not None:
		temp4 = "temp,c=" + str(temp4)
		mqttc.publish(topic_dht4_temp, payload=temp4, retain=True)
	if humidity4 is not None:
		humidity4 = "rel_hum,p=" + str(humidity4)
		mqttc.publish(topic_dht4_humidity, payload=humidity4, retain=True)
		
	time.sleep(5)
1 Like

Let us know how that works @j.lastuvka :slight_smile:

~B