mirror of
https://github.com/wled/WLED.git
synced 2025-07-19 16:56:35 +00:00
Add mDNS support for MQTT server
- fixes wled#4671 - reduce some topic string parsing - moves LWT into onConnect
This commit is contained in:
parent
b7bfd6fc67
commit
79b3bc2573
@ -11,6 +11,11 @@
|
|||||||
#warning "MQTT topics length > 32 is not recommended for compatibility with usermods!"
|
#warning "MQTT topics length > 32 is not recommended for compatibility with usermods!"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static const char* sTopicFormat PROGMEM = "%.*s/%s";
|
||||||
|
|
||||||
|
// parse payload for brightness, ON/OFF or toggle
|
||||||
|
// briLast is used to remember last brightness value in case of ON/OFF or toggle
|
||||||
|
// bri is set to 0 if payload is "0" or "OFF" or "false"
|
||||||
static void parseMQTTBriPayload(char* payload)
|
static void parseMQTTBriPayload(char* payload)
|
||||||
{
|
{
|
||||||
if (strstr(payload, "ON") || strstr(payload, "on") || strstr(payload, "true")) {bri = briLast; stateUpdated(CALL_MODE_DIRECT_CHANGE);}
|
if (strstr(payload, "ON") || strstr(payload, "on") || strstr(payload, "true")) {bri = briLast; stateUpdated(CALL_MODE_DIRECT_CHANGE);}
|
||||||
@ -30,22 +35,18 @@ static void onMqttConnect(bool sessionPresent)
|
|||||||
char subuf[MQTT_MAX_TOPIC_LEN + 9];
|
char subuf[MQTT_MAX_TOPIC_LEN + 9];
|
||||||
|
|
||||||
if (mqttDeviceTopic[0] != 0) {
|
if (mqttDeviceTopic[0] != 0) {
|
||||||
strlcpy(subuf, mqttDeviceTopic, MQTT_MAX_TOPIC_LEN + 1);
|
mqtt->subscribe(mqttDeviceTopic, 0);
|
||||||
|
snprintf_P(subuf, sizeof(subuf)-1, sTopicFormat, MQTT_MAX_TOPIC_LEN, mqttDeviceTopic, "col");
|
||||||
mqtt->subscribe(subuf, 0);
|
mqtt->subscribe(subuf, 0);
|
||||||
strcat_P(subuf, PSTR("/col"));
|
snprintf_P(subuf, sizeof(subuf)-1, sTopicFormat, MQTT_MAX_TOPIC_LEN, mqttDeviceTopic, "api");
|
||||||
mqtt->subscribe(subuf, 0);
|
|
||||||
strlcpy(subuf, mqttDeviceTopic, MQTT_MAX_TOPIC_LEN + 1);
|
|
||||||
strcat_P(subuf, PSTR("/api"));
|
|
||||||
mqtt->subscribe(subuf, 0);
|
mqtt->subscribe(subuf, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mqttGroupTopic[0] != 0) {
|
if (mqttGroupTopic[0] != 0) {
|
||||||
strlcpy(subuf, mqttGroupTopic, MQTT_MAX_TOPIC_LEN + 1);
|
mqtt->subscribe(mqttGroupTopic, 0);
|
||||||
|
snprintf_P(subuf, sizeof(subuf)-1, sTopicFormat, MQTT_MAX_TOPIC_LEN, mqttGroupTopic, "col");
|
||||||
mqtt->subscribe(subuf, 0);
|
mqtt->subscribe(subuf, 0);
|
||||||
strcat_P(subuf, PSTR("/col"));
|
snprintf_P(subuf, sizeof(subuf)-1, sTopicFormat, MQTT_MAX_TOPIC_LEN, mqttGroupTopic, "api");
|
||||||
mqtt->subscribe(subuf, 0);
|
|
||||||
strlcpy(subuf, mqttGroupTopic, MQTT_MAX_TOPIC_LEN + 1);
|
|
||||||
strcat_P(subuf, PSTR("/api"));
|
|
||||||
mqtt->subscribe(subuf, 0);
|
mqtt->subscribe(subuf, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,9 +55,8 @@ static void onMqttConnect(bool sessionPresent)
|
|||||||
DEBUG_PRINTLN(F("MQTT ready"));
|
DEBUG_PRINTLN(F("MQTT ready"));
|
||||||
|
|
||||||
#ifndef USERMOD_SMARTNEST
|
#ifndef USERMOD_SMARTNEST
|
||||||
strlcpy(subuf, mqttDeviceTopic, MQTT_MAX_TOPIC_LEN + 1);
|
snprintf_P(mqttStatusTopic, sizeof(mqttStatusTopic)-1, sTopicFormat, MQTT_MAX_TOPIC_LEN, mqttDeviceTopic, "status");
|
||||||
strcat_P(subuf, PSTR("/status"));
|
mqtt->setWill(mqttStatusTopic, 0, true, "offline"); // LWT message
|
||||||
mqtt->publish(subuf, 0, true, "online"); // retain message for a LWT
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
publishMqtt();
|
publishMqtt();
|
||||||
@ -136,7 +136,7 @@ static void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProp
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Print adapter for flat buffers
|
// Print adapter for flat buffers
|
||||||
namespace {
|
namespace {
|
||||||
class bufferPrint : public Print {
|
class bufferPrint : public Print {
|
||||||
char* _buf;
|
char* _buf;
|
||||||
size_t _size, _offset;
|
size_t _size, _offset;
|
||||||
@ -172,21 +172,21 @@ void publishMqtt()
|
|||||||
char subuf[MQTT_MAX_TOPIC_LEN + 16];
|
char subuf[MQTT_MAX_TOPIC_LEN + 16];
|
||||||
|
|
||||||
sprintf_P(s, PSTR("%u"), bri);
|
sprintf_P(s, PSTR("%u"), bri);
|
||||||
strlcpy(subuf, mqttDeviceTopic, MQTT_MAX_TOPIC_LEN + 1);
|
snprintf_P(subuf, sizeof(subuf)-1, sTopicFormat, MQTT_MAX_TOPIC_LEN, mqttDeviceTopic, "g");
|
||||||
strcat_P(subuf, PSTR("/g"));
|
|
||||||
mqtt->publish(subuf, 0, retainMqttMsg, s); // optionally retain message (#2263)
|
mqtt->publish(subuf, 0, retainMqttMsg, s); // optionally retain message (#2263)
|
||||||
|
|
||||||
sprintf_P(s, PSTR("#%06X"), (colPri[3] << 24) | (colPri[0] << 16) | (colPri[1] << 8) | (colPri[2]));
|
sprintf_P(s, PSTR("#%06X"), (colPri[3] << 24) | (colPri[0] << 16) | (colPri[1] << 8) | (colPri[2]));
|
||||||
strlcpy(subuf, mqttDeviceTopic, MQTT_MAX_TOPIC_LEN + 1);
|
snprintf_P(subuf, sizeof(subuf)-1, sTopicFormat, MQTT_MAX_TOPIC_LEN, mqttDeviceTopic, "c");
|
||||||
strcat_P(subuf, PSTR("/c"));
|
|
||||||
mqtt->publish(subuf, 0, retainMqttMsg, s); // optionally retain message (#2263)
|
mqtt->publish(subuf, 0, retainMqttMsg, s); // optionally retain message (#2263)
|
||||||
|
|
||||||
|
snprintf_P(subuf, sizeof(subuf)-1, sTopicFormat, MQTT_MAX_TOPIC_LEN, mqttDeviceTopic, "status");
|
||||||
|
mqtt->publish(subuf, 0, true, "online"); // retain message for a LWT
|
||||||
|
|
||||||
// TODO: use a DynamicBufferList. Requires a list-read-capable MQTT client API.
|
// TODO: use a DynamicBufferList. Requires a list-read-capable MQTT client API.
|
||||||
DynamicBuffer buf(1024);
|
DynamicBuffer buf(1024);
|
||||||
bufferPrint pbuf(buf.data(), buf.size());
|
bufferPrint pbuf(buf.data(), buf.size());
|
||||||
XML_response(pbuf);
|
XML_response(pbuf);
|
||||||
strlcpy(subuf, mqttDeviceTopic, MQTT_MAX_TOPIC_LEN + 1);
|
snprintf_P(subuf, sizeof(subuf)-1, sTopicFormat, MQTT_MAX_TOPIC_LEN, mqttDeviceTopic, "v");
|
||||||
strcat_P(subuf, PSTR("/v"));
|
|
||||||
mqtt->publish(subuf, 0, retainMqttMsg, buf.data(), pbuf.size()); // optionally retain message (#2263)
|
mqtt->publish(subuf, 0, retainMqttMsg, buf.data(), pbuf.size()); // optionally retain message (#2263)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -213,16 +213,20 @@ bool initMqtt()
|
|||||||
{
|
{
|
||||||
mqtt->setServer(mqttIP, mqttPort);
|
mqtt->setServer(mqttIP, mqttPort);
|
||||||
} else {
|
} else {
|
||||||
mqtt->setServer(mqttServer, mqttPort);
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
|
if (strlen(cmDNS) > 0 && strchr(mqttServer, '.') == nullptr) { // if mDNS is enabled and server does not have domain
|
||||||
|
mqttIP = MDNS.queryHost(mqttServer);
|
||||||
|
if (mqttIP != IPAddress()) // if MDNS resolved the hostname
|
||||||
|
mqtt->setServer(mqttIP, mqttPort);
|
||||||
|
else
|
||||||
|
mqtt->setServer(mqttServer, mqttPort);
|
||||||
|
} else
|
||||||
|
#endif
|
||||||
|
mqtt->setServer(mqttServer, mqttPort);
|
||||||
}
|
}
|
||||||
mqtt->setClientId(mqttClientID);
|
mqtt->setClientId(mqttClientID);
|
||||||
if (mqttUser[0] && mqttPass[0]) mqtt->setCredentials(mqttUser, mqttPass);
|
if (mqttUser[0] && mqttPass[0]) mqtt->setCredentials(mqttUser, mqttPass);
|
||||||
|
|
||||||
#ifndef USERMOD_SMARTNEST
|
|
||||||
strlcpy(mqttStatusTopic, mqttDeviceTopic, MQTT_MAX_TOPIC_LEN + 1);
|
|
||||||
strcat_P(mqttStatusTopic, PSTR("/status"));
|
|
||||||
mqtt->setWill(mqttStatusTopic, 0, true, "offline"); // LWT message
|
|
||||||
#endif
|
|
||||||
mqtt->setKeepAlive(MQTT_KEEP_ALIVE_TIME);
|
mqtt->setKeepAlive(MQTT_KEEP_ALIVE_TIME);
|
||||||
mqtt->connect();
|
mqtt->connect();
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user