我正在使用 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,则连接超时,如果将一个用作要发布的主题,则会立即断开客户端连接。然而,硬编码主题工作正常。
我错过了什么?