diff --git a/code/espurna/telnet.ino b/code/espurna/telnet.ino index e8c10719..461710d6 100644 --- a/code/espurna/telnet.ino +++ b/code/espurna/telnet.ino @@ -23,6 +23,7 @@ void _telnetDisconnect(unsigned char clientId) { _telnetClients[clientId]->free(); _telnetClients[clientId] = NULL; delete _telnetClients[clientId]; + wifiReconnectCheck(); DEBUG_MSG_P(PSTR("[TELNET] Client #%d disconnected\n"), clientId); } @@ -96,6 +97,7 @@ void _telnetNewClient(AsyncClient *client) { }, 0); DEBUG_MSG_P(PSTR("[TELNET] Client #%d connected\n"), i); + wifiReconnectCheck(); return; } @@ -115,6 +117,13 @@ void _telnetNewClient(AsyncClient *client) { // Public API // ----------------------------------------------------------------------------- +bool telnetConnected() { + for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) { + if (_telnetClients[i] && _telnetClients[i]->connected()) return true; + } + return false; +} + unsigned char telnetWrite(unsigned char ch) { char data[1] = {ch}; return _telnetWrite(data, 1); diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 5f23b630..06f014b5 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -746,12 +746,14 @@ void _wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTy DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u connected, ip: %d.%d.%d.%d, url: %s\n"), client->id(), ip[0], ip[1], ip[2], ip[3], server->url()); _wsStart(client->id()); client->_tempObject = new WebSocketIncommingBuffer(&_wsParse, true); + wifiReconnectCheck(); } else if(type == WS_EVT_DISCONNECT) { DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u disconnected\n"), client->id()); if (client->_tempObject) { delete (WebSocketIncommingBuffer *) client->_tempObject; } + wifiReconnectCheck(); } else if(type == WS_EVT_ERROR) { DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u error(%u): %s\n"), client->id(), *((uint16_t*)arg), (char*)data); diff --git a/code/espurna/wifi.ino b/code/espurna/wifi.ino index acbd50a0..4f72d942 100644 --- a/code/espurna/wifi.ino +++ b/code/espurna/wifi.ino @@ -45,12 +45,23 @@ bool createAP() { return jw.createAP(); } +void wifiReconnectCheck() { + bool connected = false; + #if WEB_SUPPORT + if (wsConnected()) connected = true; + #endif + #if TELNET_SUPPORT + if (telnetConnected()) connected = true; + #endif + jw.setReconnectTimeout(connected ? 0 : WIFI_RECONNECT_INTERVAL); +} + void wifiConfigure() { jw.setHostname(getSetting("hostname").c_str()); jw.setSoftAP(getSetting("hostname").c_str(), getSetting("adminPass", ADMIN_PASS).c_str()); jw.setConnectTimeout(WIFI_CONNECT_TIMEOUT); - jw.setReconnectTimeout(WIFI_RECONNECT_INTERVAL); + wifiReconnectCheck(); jw.setAPMode(WIFI_AP_MODE); jw.cleanNetworks();