From d2d07543b2a3d46fbc2f100ef94c60e816a1f139 Mon Sep 17 00:00:00 2001 From: Mike <7153163+hackbar@users.noreply.github.com> Date: Tue, 15 Jan 2019 17:26:51 -0800 Subject: [PATCH 1/3] Do an MDNS resolve before using the cached host. MDNS shouldn't be aggressively cached, since the IP could change. This makes using DHCP for the host a lot easier. --- sonoff/xdrv_02_mqtt.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonoff/xdrv_02_mqtt.ino b/sonoff/xdrv_02_mqtt.ino index 1d3c3a4b8..ae84d1940 100644 --- a/sonoff/xdrv_02_mqtt.ino +++ b/sonoff/xdrv_02_mqtt.ino @@ -516,7 +516,7 @@ void MqttReconnect(void) #ifndef USE_MQTT_TLS #ifdef USE_DISCOVERY #ifdef MQTT_HOST_DISCOVERY - if (!strlen(Settings.mqtt_host) && !MqttDiscoverServer()) { return; } + if (!MqttDiscoverServer() && !strlen(Settings.mqtt_host)) { return; } #endif // MQTT_HOST_DISCOVERY #endif // USE_DISCOVERY #endif // USE_MQTT_TLS From 5e06ae1d8197b17069c92a3b7e61f4a0621d8c6c Mon Sep 17 00:00:00 2001 From: Mike <7153163+hackbar@users.noreply.github.com> Date: Tue, 15 Jan 2019 18:08:28 -0800 Subject: [PATCH 2/3] Do MDNS even if TLS is enabled. I imagine this was disabled due to memory issues, but this seems to work fine on a Sonoff Basic. --- sonoff/xdrv_02_mqtt.ino | 4 ---- 1 file changed, 4 deletions(-) diff --git a/sonoff/xdrv_02_mqtt.ino b/sonoff/xdrv_02_mqtt.ino index ae84d1940..58204dd9a 100644 --- a/sonoff/xdrv_02_mqtt.ino +++ b/sonoff/xdrv_02_mqtt.ino @@ -513,13 +513,11 @@ void MqttReconnect(void) mqtt_retry_counter = Settings.mqtt_retry; global_state.mqtt_down = 1; -#ifndef USE_MQTT_TLS #ifdef USE_DISCOVERY #ifdef MQTT_HOST_DISCOVERY if (!MqttDiscoverServer() && !strlen(Settings.mqtt_host)) { return; } #endif // MQTT_HOST_DISCOVERY #endif // USE_DISCOVERY -#endif // USE_MQTT_TLS char *mqtt_user = NULL; char *mqtt_pwd = NULL; @@ -585,13 +583,11 @@ void MqttCheck(void) if (!MqttIsConnected()) { global_state.mqtt_down = 1; if (!mqtt_retry_counter) { -#ifndef USE_MQTT_TLS #ifdef USE_DISCOVERY #ifdef MQTT_HOST_DISCOVERY if (!strlen(Settings.mqtt_host) && !mdns_begun) { return; } #endif // MQTT_HOST_DISCOVERY #endif // USE_DISCOVERY -#endif // USE_MQTT_TLS MqttReconnect(); } else { mqtt_retry_counter--; From 838b113fa34a128c90bf26a66c60cb9886706ed0 Mon Sep 17 00:00:00 2001 From: Mike <7153163+hackbar@users.noreply.github.com> Date: Tue, 15 Jan 2019 21:48:07 -0800 Subject: [PATCH 3/3] Add a define for mDNS hostname to connect to for MQTT. --- sonoff/xdrv_02_mqtt.ino | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/sonoff/xdrv_02_mqtt.ino b/sonoff/xdrv_02_mqtt.ino index 58204dd9a..4321cb7b0 100644 --- a/sonoff/xdrv_02_mqtt.ino +++ b/sonoff/xdrv_02_mqtt.ino @@ -220,9 +220,19 @@ boolean MqttDiscoverServer(void) AddLog(LOG_LEVEL_INFO); if (n > 0) { - // Note: current strategy is to get the first MQTT service (even when many are found) + #ifdef MDNS_HOSTNAME + for (int i = 0; i < n; i++) { + if (!strcmp(MDNS.hostname(i).c_str(), MDNS_HOSTNAME)) { + snprintf_P(Settings.mqtt_host, sizeof(Settings.mqtt_host), MDNS.IP(i).toString().c_str()); + Settings.mqtt_port = MDNS.port(i); + break; // stop at the first matching record + } + } + #else + // If the hostname isn't set, use the first record found. snprintf_P(Settings.mqtt_host, sizeof(Settings.mqtt_host), MDNS.IP(0).toString().c_str()); Settings.mqtt_port = MDNS.port(0); + #endif // MDNS_HOSTNAME snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_MDNS D_MQTT_SERVICE_FOUND " %s, " D_IP_ADDRESS " %s, " D_PORT " %d"), MDNS.hostname(0).c_str(), Settings.mqtt_host, Settings.mqtt_port);