Fix wifi connect/disconnect bug

This commit is contained in:
fvanroie 2021-08-01 16:41:48 +02:00
parent 4103535aac
commit 287f1f1e0b
2 changed files with 40 additions and 20 deletions

View File

@ -1175,24 +1175,28 @@ void dispatch_exec(const char*, const char* payload, uint8_t source)
return;
}
char buffer[1024];
// char buffer[512]; // use stack
String buffer((char*)0); // use heap
buffer.reserve(256);
ReadBufferingStream bufferedFile{cmdfile, 256};
cmdfile.seek(0);
while(bufferedFile.available()) {
// int l = bufferedFile.readBytesUntil('\n', buffer, sizeof(buffer) - 1);
size_t index = 0;
while(index < sizeof(buffer) - 1) {
size_t index = 0; buffer="";
// while(index < sizeof(buffer) - 1) {
while(index < MQTT_MAX_PACKET_SIZE) {
int c = bufferedFile.read();
if(c < 0 || c == '\n' || c == '\r') { // CR or LF
break;
}
buffer[index] = (char)c;
// buffer[index] = (char)c;
buffer += (char)c;
index++;
}
buffer[index] = 0; // terminate string
if(index > 0 && buffer[0] != '#') dispatch_text_line(buffer, TAG_FILE); // # for comments
// buffer[index] = 0; // terminate string
// if(index > 0 && buffer[0] != '#') dispatch_text_line(buffer.c_str(), TAG_FILE); // # for comments
if(index > 0 && buffer.charAt(0) != '#') dispatch_text_line(buffer.c_str(), TAG_FILE); // # for comments
}
cmdfile.close();

View File

@ -51,6 +51,7 @@ char wifiPassword[64] = "";
char wifiIpAddress[16] = "";
uint16_t wifiReconnectCounter = 0;
bool wifiOnline = false;
bool haspOnline = false;
// const byte DNS_PORT = 53;
// DNSServer dnsServer;
@ -84,11 +85,11 @@ static void wifiConnected(IPAddress ipaddress)
wifiOnline = true; // now we are connected
wifiReconnectCounter = 0;
dispatch_exec(NULL, "/online.cmd", TAG_WIFI);
// dispatch_exec(NULL, "/online.cmd", TAG_WIFI);
LOG_VERBOSE(TAG_WIFI, F("Connected = %s"),
WiFi.status() == WL_CONNECTED ? PSTR(D_NETWORK_ONLINE) : PSTR(D_NETWORK_OFFLINE));
networkStart();
// networkStart();
}
static void wifiDisconnected(const char* ssid, uint8_t reason)
@ -287,8 +288,10 @@ static void wifiDisconnected(const char* ssid, uint8_t reason)
else
wifiOnline = false; // now we are disconnected
dispatch_exec(NULL, "/offline.cmd", TAG_WIFI);
networkStop();
// dispatch_exec(NULL, "/offline.cmd", TAG_WIFI);
LOG_VERBOSE(TAG_WIFI, F("Connected = %s"),
WiFi.status() == WL_CONNECTED ? PSTR(D_NETWORK_ONLINE) : PSTR(D_NETWORK_OFFLINE));
// networkStop();
}
static void wifiSsidConnected(const char* ssid)
@ -470,18 +473,31 @@ bool wifiEvery5Seconds()
#if defined(STM32F4xx)
if(wifiShowAP()) { // no ssid is set yet wait for user on-screen input
return false;
} else if(WiFi.status() == WL_CONNECTED) {
}
#else
if(WiFi.getMode() != WIFI_STA) {
return false;
} else if(WiFi.status() == WL_CONNECTED) {
#endif
return true;
} else {
LOG_WARNING(TAG_WIFI, F("No Connection... retry %d"), wifiReconnectCounter);
wifiReconnect();
return false;
}
#endif
if(wifiOnline != haspOnline) {
if(wifiOnline) {
dispatch_exec(NULL, "/online.cmd", TAG_WIFI);
networkStart();
} else {
dispatch_exec(NULL, "/offline.cmd", TAG_WIFI);
networkStop();
}
haspOnline = wifiOnline;
}
if(WiFi.status() == WL_CONNECTED) {
return true;
}
LOG_WARNING(TAG_WIFI, F("No Connection... retry %d"), wifiReconnectCounter);
wifiReconnect();
return false;
}
bool wifiValidateSsid(const char* ssid, const char* pass)