Thanks, just wanted to make sure there were’t any errors there and there are not (that I saw). If you run the script below in a terminal with python (filename).py are you getting any errors? What is the connection status 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 = " "
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
mqttc = mqtt.Client(client_id=clientid)
mqttc.username_pw_set(username, password=password)
mqttc.connect("mqtt.mydevices.com", port=1883, keepalive=60)
mqttc.on_connect = on_connect
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, 4) #22 is the sensor type, 4 is the GPIO pin number (not physical pin number)
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()