micropython / micropython/micropython-lib

Only able to publish 1 message with umqtt.simple

Offen
#746 8 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

unicode
Vorherrschende Sprache
Python
Sterne
2.9k
Forks
1.1k
Ø Merge
7 T. 6 Std.
Gemergte PRs (30 T.)
3

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
networking
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.