mirror of
https://github.com/arendst/Tasmota.git
synced 2025-04-25 07:17:16 +00:00
Add more redundancy to lost MQTT server
This commit is contained in:
parent
93bb0d23b8
commit
8ba2733cf7
@ -732,7 +732,8 @@ void wifiKeepAlive(void) {
|
|||||||
bool WifiPollDns(void) {
|
bool WifiPollDns(void) {
|
||||||
// WiFi.hostByName takes over ten seconds if no DNS server found
|
// WiFi.hostByName takes over ten seconds if no DNS server found
|
||||||
// This function checks to find the DNS server within 1 second
|
// This function checks to find the DNS server within 1 second
|
||||||
// This is an alternative for ping using less resources
|
// This is an alternative for ping using less resources and some DNS server do not respond to pings (ICMP)
|
||||||
|
// See https://github.com/letscontrolit/ESPEasy/issues/1494#issuecomment-397872538
|
||||||
WiFiClient DnsClient;
|
WiFiClient DnsClient;
|
||||||
|
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
@ -747,12 +748,25 @@ bool WifiPollDns(void) {
|
|||||||
DnsClient.stop();
|
DnsClient.stop();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// AddLog(LOG_LEVEL_DEBUG, PSTR("DNS: Disconnected %_I"), dns_address);
|
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("DNS: Disconnected"));
|
|
||||||
// }
|
// }
|
||||||
|
AddLog(LOG_LEVEL_DEBUG, PSTR("DNS: Disconnected"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int WifiHostByName(const char* aHostname, IPAddress& aResult) {
|
||||||
|
// Use this instead of WiFi.hostByName or connect(host_name,.. to block less if DNS server is not found
|
||||||
|
aResult = (uint32_t)(0);
|
||||||
|
if (aResult.fromString(aHostname)) {
|
||||||
|
// Host name is already an IP address so use it!
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else if (WifiPollDns() && WiFi.hostByName(aHostname, aResult)) {
|
||||||
|
// Host name resolved
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void WifiPollNtp() {
|
void WifiPollNtp() {
|
||||||
static uint8_t ntp_sync_minute = 0;
|
static uint8_t ntp_sync_minute = 0;
|
||||||
static uint32_t ntp_run_time = 0;
|
static uint32_t ntp_run_time = 0;
|
||||||
@ -809,6 +823,7 @@ uint32_t WifiGetNtp(void) {
|
|||||||
}
|
}
|
||||||
if (strlen(ntp_server)) {
|
if (strlen(ntp_server)) {
|
||||||
resolved_ip = (WiFi.hostByName(ntp_server, time_server_ip) == 1); // DNS timeout set to (ESP8266) 10s / (ESP32) 14s
|
resolved_ip = (WiFi.hostByName(ntp_server, time_server_ip) == 1); // DNS timeout set to (ESP8266) 10s / (ESP32) 14s
|
||||||
|
// resolved_ip = (WifiHostByName(ntp_server, time_server_ip) == 1); // DNS timeout set to (ESP8266) 10s / (ESP32) 14s
|
||||||
if ((255 == time_server_ip[0]) || // No valid name resolved (255.255.255.255)
|
if ((255 == time_server_ip[0]) || // No valid name resolved (255.255.255.255)
|
||||||
((255 == time_server_ip[1]) && (255 == time_server_ip[2]) && (255 == time_server_ip[3]))) { // No valid name resolved (x.255.255.255)
|
((255 == time_server_ip[1]) && (255 == time_server_ip[2]) && (255 == time_server_ip[3]))) { // No valid name resolved (x.255.255.255)
|
||||||
resolved_ip = false;
|
resolved_ip = false;
|
||||||
|
@ -1083,12 +1083,12 @@ void MqttReconnect(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
IPAddress mqtt_host_ip;
|
||||||
if (!WifiPollDns()) {
|
if (!WifiHostByName(SettingsText(SET_MQTT_HOST), mqtt_host_ip)) {
|
||||||
MqttDisconnected(-5); // MQTT_DNS_DISCONNECTED
|
MqttDisconnected(-5); // MQTT_DNS_DISCONNECTED
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
MqttClient.setServer(SettingsText(SET_MQTT_HOST), Settings->mqtt_port);
|
MqttClient.setServer(mqtt_host_ip, Settings->mqtt_port);
|
||||||
|
|
||||||
uint32_t mqtt_connect_time = millis();
|
uint32_t mqtt_connect_time = millis();
|
||||||
#if defined(USE_MQTT_TLS)
|
#if defined(USE_MQTT_TLS)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user