Setup:
Hassio running in a docker container on a laptop
Mosquitto MQTT broker add-in
Raspberry Pi in shed connected to DHT22 (temp/humidity sensor) by GPIO.
Paho MQTT client
My mqttlogger is using the following code
# Type of sensor, can be Adafruit_DHT.DHT11, Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
DHT_TYPE = Adafruit_DHT.DHT22
# Example of sensor connected to Raspberry Pi pin 4
DHT_PIN = 4
# Example of sensor connected to Beaglebone Black pin P8_11
#DHT_PIN = 'P8_11'
if (len(sys.argv) < 2):
raise ValueError('Input arguments of mqtt channel temperature humidity not passed')
MOSQUITTO_HOST = '192.168.0.150'
MOSQUITTO_PORT = 1883
MOSQUITTO_TEMP_MSG = str(sys.argv[1]) # Old channel name in here
MOSQUITTO_HUMI_MSG = str(sys.argv[2]) # Old channel name now passed by argument
print('Mosquitto Temp MSG {0}'.format(MOSQUITTO_TEMP_MSG))
print('Mosquitto Humidity MSG {0}'.format(MOSQUITTO_HUMI_MSG))
# How long to wait (in seconds) between measurements.
print "Args length: " + str(len(sys.argv))
FREQUENCY_SECONDS = 30
if (len(sys.argv) > 4):
FREQUENCY_SECONDS = float(sys.argv[4])
print('Logging sensor measurements to {0} every {1} seconds.'.format('MQTT', FREQUENCY_SECONDS))
print('Press Ctrl-C to quit.')
print('Connecting to MQTT on {0}'.format(MOSQUITTO_HOST))
mqttc = mqtt.Client("python_pub")
try:
while True:
# Attempt to get sensor reading.
humidity, temp = Adafruit_DHT.read(DHT_TYPE, DHT_PIN)
# Skip to the next reading if a valid measurement couldn't be taken.
# This might happen if the CPU is under a lot of load and the sensor
# can't be reliably read (timing is critical to read the sensor).
if humidity is None or temp is None:
time.sleep(2)
continue
currentdate = time.strftime('%Y-%m-%d %H:%M:%S')
print('Date Time: {0}'.format(currentdate))
print('Temperature: {0:0.2f} C'.format(temp))
print('Humidity: {0:0.2f} %'.format(humidity))
# Publish to the MQTT channel
try:
mqttc.connect(MOSQUITTO_HOST,MOSQUITTO_PORT);
print 'Updating {0}'.format(MOSQUITTO_TEMP_MSG)
(result1,mid) = mqttc.publish(MOSQUITTO_TEMP_MSG,temp)
print 'Updating {0}'.format(MOSQUITTO_HUMI_MSG)
time.sleep(1)
(result2,mid) = mqttc.publish(MOSQUITTO_HUMI_MSG,humidity)
print 'MQTT Updated result {0} and {1}'.format(result1,result2)
if result1 == 1 or result2 == 1:
raise ValueError('Result for one message was not 0')
mqttc.disconnect()
except Exception,e:
# Error appending data, most likely because credentials are stale.
# Null out the worksheet so a login is performed at the top of the loop.
mqttc.disconnect()
print('Append error, logging in again: ' + str(e))
continue
Now it seems to connect to the broker on hassio and says it publishes the values - but I see nothing in integrations, or unused devices. When I look at the log file from the addin, it looks like it connects and then immediately disconnects with a socket error.
1571418576: New client connected from 192.168.0.35 as python_pub (p2, c1, k60).
1571418577: Socket error on client python_pub, disconnecting.
1571418608: New connection from 192.168.0.35 on port 1883.
1571418608: New client connected from 192.168.0.35 as python_pub (p2, c1, k60).
1571418609: Socket error on client python_pub, disconnecting.