micropython / micropython/micropython-lib

Only able to publish 1 message with umqtt.simple

Open
#746 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

unicode
Dominant language
Python
Stars
2.9k
Forks
1.1k
Avg merge
7d 6h
Merged PRs (30d)
3

Description

I'm trying to send config info to Home Assistant from a Pico W with the following code:

import time
import config
from network import WLAN,STA_IF
import ubinascii
# import gc
# import micropython

from machine import Pin, UART
from pimoroni import RGBLED, Button
# from pms5003 import PMS5003
from pimoroni_i2c import PimoroniI2C
from breakout_bme68x import BreakoutBME68X, STATUS_HEATER_STABLE
from breakout_ltr559 import BreakoutLTR559

from umqtt.simple import MQTTException, MQTTClient

# set up the LED
led = RGBLED(6, 7, 10, invert=True)
led.set_rgb(200,100,0)

# micropython.mem_info()
# Connect to the wifi
wlan = WLAN(STA_IF)
wlan.active(True)
wlan.connect(config.SSID, config.PASSWORD)
unique_id = ubinascii.hexlify(wlan.config('mac')).decode().upper()

# Connect to the MQTT Server
mqtt_connected = False;
try:
    mqtt = MQTTClient("enviro_plus", config.MQTT_SERVER, config.MQTT_PORT, config.MQTT_USER, config.MQTT_PASSWORD)
    mqtt.DEBUG = True
    mqtt.connect()
    mqtt_connected = True;
except MQTTException as e:
    print(f"Unable to connect to MQTT: {e}")
except OSError as e:
    print(f"Unable to connect to MQTT: {e}")


# Initialise Home Assistant.
if mqtt_connected:
    # print('Initial free: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc()))
    mqtt.publish(f"homeassistant/sensor/enviroplus_{unique_id}_gas/config1", f'{{ "name":  "Gas resistance",   "state_topic": "homeassistant/sensor/enviroplus_{unique_id}/state",  "value_template": "{{{{ value_json.temperature }}}}",  "unique_id": "enviroplus_{unique_id}_temp", "unit_of_measurement": "Ω",  "device": {{"identifiers": ["enviroplus_{unique_id}"], "name": "Workshop Environment"}}}}   ')
    mqtt.publish(f"homeassistant/sensor/enviroplus_{unique_id}_pm1/config1", f'{{ "name": "PM1", "device_class": "pm1", "stat_t": "homeassistant/sensor/enviroplus_{unique_id}/state", "value_template": "{{{{ value_json.pm1 }}}}", "unique_id": "enviroplus_{unique_id}_pm1", "unit_of_measurement": "µg/m³","device": {{ "identifiers": "enviroplus_{unique_id}", "name": "Workshop Environment"}}}}   ')
    mqtt.publish(f"homeassistant/sensor/enviroplus_{unique_id}_pm25/config1", f'{{ "name": "PM2.5", "device_class": "pm25", "stat_t": "homeassistant/sensor/enviroplus_{unique_id}/state", "value_template": "{{{{ value_json.pm25 }}}}", "unique_id": "enviroplus_{unique_id}_pm25", "unit_of_measurement": "µg/m³","device": {{ "identifiers": "enviroplus_{unique_id}", "name": "Workshop Environment"}}}}   ')
    mqtt.publish(f"homeassistant/sensor/enviroplus_{unique_id}_pm10/config1", f'{{ "name":  "PM10",  "device_class": "pm10",  "state_topic": "homeassistant/sensor/enviroplus_{unique_id}/state",  "value_template": "{{{{ value_json.pm10 }}}}",  "unique_id": "enviroplus_{unique_id}_pm10", "unit_of_measurement": "µg/m³",  "device": {{"identifiers": ["enviroplus_{unique_id}"], "name": "Workshop Environment"}}}}   ')
    mqtt.publish(f"homeassistant/sensor/enviroplus_{unique_id}_T/config1", f'{{ "name":  "Temperature",  "device_class": "temperature",  "state_topic": "homeassistant/sensor/enviroplus_{unique_id}/state",  "value_template": "{{{{ value_json.temperature }}}}",  "unique_id": "enviroplus_{unique_id}_temperature", "unit_of_measurement": "°C",  "device": {{"identifiers": ["enviroplus_{unique_id}"], "name": "Workshop Environment"}}}}   ')
    mqtt.publish(f"homeassistant/sensor/enviroplus_{unique_id}_H/config1", f'{{ "name":  "Humidity",  "device_class": "humidity",  "state_topic": "homeassistant/sensor/enviroplus_{unique_id}/state",  "value_template": "{{{{ value_json.humidity }}}}",  "unique_id": "enviroplus_{unique_id}_humidity", "unit_of_measurement": "%",  "device": {{"identifiers": ["enviroplus_{unique_id}"], "name": "Workshop Environment"}}}}   ')
    mqtt.disconnect()
    # print('Initial free: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc()))
else:
    print("MWTT not connected.")

There are a couple of issues, firstly I have to pad the paylod with 3 extra characters or the json that's sent in missing it's terminating brackets.

The other issue is that it only seems to send the first message, I have MQTT Explorer watching what's published and I only ever see the first of those published messages, no matter what order I put them in.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the umqtt.simple MQTTClient.publish entry point and reproduce the behavior using the provided Pico W script, including the missing JSON terminators and multiple publishes. Confirm that each publish sends a complete payload and that MQTT Explorer receives all six messages before disconnecting.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.