I am running homeassistant on a raspberry pi4 for mosquito MQTT broker add-on and extension installed. I am trying to publish data from an raspberry pico2 w but I cannot get the data to show in homeassistant. I am basing my work off this tutorial (including using the mentioned umqtt library).
The program I have so far is just publishing a running counter to establish that I can get data from the pico to HA. It runs, connects to wifi, says it is publishing to the MQTT broker, and the logs of the MQTT broker say they are receiving messages, but under devices and entities > MQTT says No devices or entities. I have configured it to listen for “#” as well as pico, home assistant, etc. I have created a pico user on Home assistant (not within the MQTT config based on the documentation). I am new with MQTT and micropython, so this might be a simple error, but I am a few hours into truble shooting and not getting anywhere.
Here is my current code:
from machine import Pin
from time import sleep
import network
from math import sin
from umqtt.simple import MQTTClient
import config
MQTT_SERVER = b'192.168.86.6'
MQTT_PORT = 1883
MQTT_CLIENT_ID = b'pico'
MQTT_KEEPALIVE = 7200
MQTT_USER = "pico"
MQTT_PASSWORD = "pico_password"
MQTT_SSL = False
MQTT_SSL_PARAMS = {'server_hostname': MQTT_SERVER}
MQTT_TOPIC_COUNT = "pico/count"
def initialize_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
# Connect to the network
wlan.connect(ssid, password)
# Wait for Wi-Fi connection
connection_timeout = 10
while connection_timeout > 0:
if wlan.status() >= 3:
break
connection_timeout -= 1
print('Waiting for Wi-Fi connection...')
sleep(1)
# Check if connection is successful
if wlan.status() != 3:
return False
else:
print('Connection successful!')
network_info = wlan.ifconfig()
print('IP address:', network_info[0])
return True
def connect_mqtt():
try:
client = MQTTClient(client_id=MQTT_CLIENT_ID,
server=MQTT_SERVER,
port=MQTT_PORT,
user=MQTT_USER,
password=MQTT_PASSWORD,
keepalive=MQTT_KEEPALIVE,
ssl=MQTT_SSL,
ssl_params=MQTT_SSL_PARAMS)
client.connect()
return client
except Exception as e:
print('Error connecting to MQTT:', e)
raise # Re-raise the exception to see the full traceback
def publish_mqtt(topic, value):
client.publish(topic, value)
print(topic)
print(value)
print("Publish Done")
try:
if not initialize_wifi(config.wifi_ssid, config.wifi_password):
print('Error connecting to the network... exiting program')
else:
# Connect to MQTT broker, start MQTT client
client = connect_mqtt()
count = 0
while True:
# Read sensor data
#temperature, humidity, pressure = get_sensor_readings()
# Publish as MQTT payload
#publish_mqtt(MQTT_TOPIC_TEMPERATURE, str(temperature))
#publish_mqtt(MQTT_TOPIC_PRESSURE, str(pressure))
#publish_mqtt(MQTT_TOPIC_HUMIDITY, str(humidity))
publish_mqtt(MQTT_TOPIC_COUNT, str(count))
print("printing")
count = count + 10
# Delay 10 seconds
sleep(10)
except Exception as e:
print('Error:', e)
And here is a sample of the MQTT logging:
time="2025-03-04T10:57:54-05:00" level=debug msg="found in cache: pico"
2025-03-04 10:57:54: Received PUBLISH from pico (d0, q0, r0, m0, 'pico/count', ... (4 bytes))
2025-03-04 10:58:02: Received PINGREQ from 6N4mMAjioIoK1H0nPngOGv
2025-03-04 10:58:02: Sending PINGRESP to 6N4mMAjioIoK1H0nPngOGv
time="2025-03-04T10:58:04-05:00" level=debug msg="checking acl cache for pico"
time="2025-03-04T10:58:04-05:00" level=debug msg="to auth record: [97 99 108 45 112 105 99 111 45 112 105 99 111 47 99 111 117 110 116 45 112 105 99 111 45 50 218 57 163 238 94 107 75 13 50 85 191 239 149 96 24 144 175 216 7 9]\n"
time="2025-03-04T10:58:04-05:00" level=debug msg="found in cache: pico"
2025-03-04 10:58:04: Received PUBLISH from pico (d0, q0, r0, m0, 'pico/count', ... (4 bytes))
time="2025-03-04T10:58:14-05:00" level=debug msg="checking acl cache for pico"
2025-03-04 10:58:14: Received PUBLISH from pico (d0, q0, r0, m0, 'pico/count', ... (4 bytes))
time="2025-03-04T10:58:14-05:00" level=debug msg="to auth record: [97 99 108 45 112 105 99 111 45 112 105 99 111 47 99 111 117 110 116 45 112 105 99 111 45 50 218 57 163 238 94 107 75 13 50 85 191 239 149 96 24 144 175 216 7 9]\n"
time="2025-03-04T10:58:14-05:00" level=debug msg="found in cache: pico"
I have uninstalled and re-installed MQTT, rebooted HA and the pi. I am not sure what I need to do to get the data “count” to appear in my homeassistant instance.