Only able to publish 1 message with umqtt.simple

未关闭
#746 8 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
python
领域
networking

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

unicode

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.

主要语言
Python
星标
2.9k
派生
1.1k
平均合并
7 天 6 小时
30 天内合并 PR
3

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

micropython/micropython-lib 的其他 Issue

查看 micropython/micropython-lib 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。