尝试在 arduino mqtt 中动态构建主题字符串导致断开连接 [重复]

问题描述 投票:0回答:0

我正在使用 arduino PubSubClient 发送 mqtt 消息。这是一组共享相同主题格式的传感器,除了一个中缀,像这样:

const char *state_topic = "homeassistant/master_bedroom/blind"; // device 1
const char *state_topic = "homeassistant/guest_bedroom/blind"; // device 2

所以,我认为这样做可能是明智的:

const char *mqttTopic = "master_bedroom";
const char *state_topic = (String("homeassistant/") + mqttTopic + "/blind").c_str();

有很多这样的话题,所以改变

mqttTopic
变量比改变所有
*_topic
变量更容易。

使用这些值会导致客户端立即断开连接。如果将一个用作 willTopic,则连接超时,如果将一个用作要发布的主题,则会立即断开客户端连接。然而,硬编码主题工作正常。

我错过了什么?

c++ arduino mqtt
© www.soinside.com 2019 - 2024. All rights reserved.