Test for easier MQTT reconnects

Test for easier MQTT reconnects
This commit is contained in:
Theo Arends 2019-03-31 17:51:05 +02:00
parent d2a1433758
commit b7adb10923
3 changed files with 25 additions and 14 deletions

View File

@ -370,6 +370,14 @@ uint8_t Shortcut(const char* str)
return result;
}
bool ValidIpAddress(const char* str)
{
const char* p = str;
while (*p && ( (*p == '.') || (*p >= '0') || (*p <= '9') )) { p++; }
return (*p == '\0');
}
bool ParseIp(uint32_t* addr, const char* str)
{
uint8_t *part = (uint8_t*)addr;
@ -1247,8 +1255,9 @@ void Syslog(void)
// Destroys log_data
char syslog_preamble[64]; // Hostname + Id
if (syslog_host_hash != GetHash(Settings.syslog_host, strlen(Settings.syslog_host))) {
syslog_host_hash = GetHash(Settings.syslog_host, strlen(Settings.syslog_host));
uint32_t current_hash = GetHash(Settings.syslog_host, strlen(Settings.syslog_host));
if (syslog_host_hash != current_hash) {
syslog_host_hash = current_hash;
WiFi.hostByName(Settings.syslog_host, syslog_host_addr); // If sleep enabled this might result in exception so try to do it once using hash
}
if (PortUdp.beginPacket(syslog_host_addr, Settings.syslog_port)) {

View File

@ -2148,7 +2148,7 @@ void HandleNotFound(void)
/* Redirect to captive portal if we got a request for another domain. Return true in that case so the page handler do not try to handle the request again. */
bool CaptivePortal(void)
{
if ((WifiIsInManagerMode()) && !ValidIpAddress(WebServer->hostHeader())) {
if ((WifiIsInManagerMode()) && !ValidIpAddress(WebServer->hostHeader().c_str())) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_REDIRECTED));
WebServer->sendHeader(F("Location"), String("http://") + WebServer->client().localIP().toString(), true);
@ -2159,16 +2159,6 @@ bool CaptivePortal(void)
return false;
}
/** Is this an IP? */
bool ValidIpAddress(String str)
{
for (uint16_t i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (c != '.' && (c < '0' || c > '9')) { return false; }
}
return true;
}
/*********************************************************************************************/
String UrlEncode(const String& text)

View File

@ -37,6 +37,9 @@ const char kMqttCommands[] PROGMEM =
D_CMND_MQTTUSER "|" D_CMND_MQTTPASSWORD "|" D_CMND_FULLTOPIC "|" D_CMND_PREFIX "|" D_CMND_GROUPTOPIC "|" D_CMND_TOPIC "|" D_CMND_PUBLISH "|"
D_CMND_BUTTONTOPIC "|" D_CMND_SWITCHTOPIC "|" D_CMND_BUTTONRETAIN "|" D_CMND_SWITCHRETAIN "|" D_CMND_POWERRETAIN "|" D_CMND_SENSORRETAIN ;
IPAddress mqtt_host_addr; // MQTT host IP address
uint32_t mqtt_host_hash = 0; // MQTT host name hash
uint16_t mqtt_connect_count = 0; // MQTT re-connect count
uint16_t mqtt_retry_counter = 1; // MQTT connection retry counter
uint8_t mqtt_initial_connection_state = 2; // MQTT connection messages state
@ -448,7 +451,16 @@ void MqttReconnect(void)
}
MqttClient.setCallback(MqttDataHandler);
MqttClient.setServer(Settings.mqtt_host, Settings.mqtt_port);
// MqttClient.setServer(Settings.mqtt_host, Settings.mqtt_port);
// Skip MQTT host DNS lookup if not needed
uint32_t current_hash = GetHash(Settings.mqtt_host, strlen(Settings.mqtt_host));
if (mqtt_host_hash != current_hash) {
mqtt_host_hash = current_hash;
WiFi.hostByName(Settings.mqtt_host, mqtt_host_addr); // Skips DNS lookup if mqtt_host is IP address string as from mDns
}
MqttClient.setServer(mqtt_host_addr, Settings.mqtt_port);
if (MqttClient.connect(mqtt_client, mqtt_user, mqtt_pwd, stopic, 1, true, mqtt_data)) {
MqttConnected();
} else {