mirror of https://github.com/arendst/Tasmota.git
parent
a0559080b7
commit
2b7fbe22e3
|
@ -76,6 +76,7 @@ const char kMqttCommands[] PROGMEM =
|
||||||
uint16_t mqtt_retry_counter = 1; // MQTT connection retry counter
|
uint16_t mqtt_retry_counter = 1; // MQTT connection retry counter
|
||||||
uint8_t mqtt_initial_connection_state = 2; // MQTT connection messages state
|
uint8_t mqtt_initial_connection_state = 2; // MQTT connection messages state
|
||||||
bool mqtt_connected = false; // MQTT virtual connection status
|
bool mqtt_connected = false; // MQTT virtual connection status
|
||||||
|
bool mqtt_allowed = false; // MQTT enabled and parameters valid
|
||||||
|
|
||||||
/*********************************************************************************************\
|
/*********************************************************************************************\
|
||||||
* MQTT driver specific code need to provide the following functions:
|
* MQTT driver specific code need to provide the following functions:
|
||||||
|
@ -210,9 +211,9 @@ void MqttLoop(void)
|
||||||
|
|
||||||
#ifdef USE_DISCOVERY
|
#ifdef USE_DISCOVERY
|
||||||
#ifdef MQTT_HOST_DISCOVERY
|
#ifdef MQTT_HOST_DISCOVERY
|
||||||
boolean MqttDiscoverServer(void)
|
void MqttDiscoverServer(void)
|
||||||
{
|
{
|
||||||
if (!mdns_begun) { return false; }
|
if (!mdns_begun) { return; }
|
||||||
|
|
||||||
int n = MDNS.queryService("mqtt", "tcp"); // Search for mqtt service
|
int n = MDNS.queryService("mqtt", "tcp"); // Search for mqtt service
|
||||||
|
|
||||||
|
@ -220,26 +221,21 @@ boolean MqttDiscoverServer(void)
|
||||||
AddLog(LOG_LEVEL_INFO);
|
AddLog(LOG_LEVEL_INFO);
|
||||||
|
|
||||||
if (n > 0) {
|
if (n > 0) {
|
||||||
#ifdef MDNS_HOSTNAME
|
uint8_t i = 0; // If the hostname isn't set, use the first record found.
|
||||||
for (int i = 0; i < n; i++) {
|
#ifdef MDNS_HOSTNAME
|
||||||
|
for (i = n; i > 0; i--) { // Search from last to first and use first if not found
|
||||||
if (!strcmp(MDNS.hostname(i).c_str(), MDNS_HOSTNAME)) {
|
if (!strcmp(MDNS.hostname(i).c_str(), MDNS_HOSTNAME)) {
|
||||||
|
break; // Stop at matching record
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif // MDNS_HOSTNAME
|
||||||
snprintf_P(Settings.mqtt_host, sizeof(Settings.mqtt_host), MDNS.IP(i).toString().c_str());
|
snprintf_P(Settings.mqtt_host, sizeof(Settings.mqtt_host), MDNS.IP(i).toString().c_str());
|
||||||
Settings.mqtt_port = MDNS.port(i);
|
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"),
|
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);
|
MDNS.hostname(i).c_str(), Settings.mqtt_host, Settings.mqtt_port);
|
||||||
AddLog(LOG_LEVEL_INFO);
|
AddLog(LOG_LEVEL_INFO);
|
||||||
}
|
}
|
||||||
|
|
||||||
return n > 0;
|
|
||||||
}
|
}
|
||||||
#endif // MQTT_HOST_DISCOVERY
|
#endif // MQTT_HOST_DISCOVERY
|
||||||
#endif // USE_DISCOVERY
|
#endif // USE_DISCOVERY
|
||||||
|
@ -398,7 +394,7 @@ void MqttConnected(void)
|
||||||
{
|
{
|
||||||
char stopic[TOPSZ];
|
char stopic[TOPSZ];
|
||||||
|
|
||||||
if (Settings.flag.mqtt_enabled) {
|
if (mqtt_allowed) {
|
||||||
AddLog_P(LOG_LEVEL_INFO, S_LOG_MQTT, PSTR(D_CONNECTED));
|
AddLog_P(LOG_LEVEL_INFO, S_LOG_MQTT, PSTR(D_CONNECTED));
|
||||||
mqtt_connected = true;
|
mqtt_connected = true;
|
||||||
mqtt_retry_counter = 0;
|
mqtt_retry_counter = 0;
|
||||||
|
@ -520,7 +516,18 @@ void MqttReconnect(void)
|
||||||
{
|
{
|
||||||
char stopic[TOPSZ];
|
char stopic[TOPSZ];
|
||||||
|
|
||||||
if (!Settings.flag.mqtt_enabled) {
|
mqtt_allowed = Settings.flag.mqtt_enabled;
|
||||||
|
if (mqtt_allowed) {
|
||||||
|
#ifdef USE_DISCOVERY
|
||||||
|
#ifdef MQTT_HOST_DISCOVERY
|
||||||
|
MqttDiscoverServer();
|
||||||
|
#endif // MQTT_HOST_DISCOVERY
|
||||||
|
#endif // USE_DISCOVERY
|
||||||
|
if (!strlen(Settings.mqtt_host) || !Settings.mqtt_port) {
|
||||||
|
mqtt_allowed = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!mqtt_allowed) {
|
||||||
MqttConnected();
|
MqttConnected();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -535,12 +542,6 @@ void MqttReconnect(void)
|
||||||
mqtt_retry_counter = Settings.mqtt_retry;
|
mqtt_retry_counter = Settings.mqtt_retry;
|
||||||
global_state.mqtt_down = 1;
|
global_state.mqtt_down = 1;
|
||||||
|
|
||||||
#ifdef USE_DISCOVERY
|
|
||||||
#ifdef MQTT_HOST_DISCOVERY
|
|
||||||
if (!MqttDiscoverServer() && !strlen(Settings.mqtt_host)) { return; }
|
|
||||||
#endif // MQTT_HOST_DISCOVERY
|
|
||||||
#endif // USE_DISCOVERY
|
|
||||||
|
|
||||||
char *mqtt_user = NULL;
|
char *mqtt_user = NULL;
|
||||||
char *mqtt_pwd = NULL;
|
char *mqtt_pwd = NULL;
|
||||||
if (strlen(Settings.mqtt_user) > 0) mqtt_user = Settings.mqtt_user;
|
if (strlen(Settings.mqtt_user) > 0) mqtt_user = Settings.mqtt_user;
|
||||||
|
|
Loading…
Reference in New Issue