ESP32 Server hangs when client looses internet connectivity

Hi,
I am making ESP32 a server and connecting it with client on particular port and IP.
I have attached the code with the query.
When i internet connectivity of client get lost the ESP32 doesn’t get the ping that client got disconnected and it remains in the while(client.connected() loop after that.
After that i need to reset the ESP32 to make connection with the client and the communicate with it.
Is there any way to solve this issue?
I have attached my code.
Thanks
Akansha

Final_28_03_20.ino (5.76 KB)

can’t read .ino attachments directly…

Hi,
I am posting my code here

#include "EEPROM.h"
#include <WiFi.h>
#include <Wire.h>
const int RST = 22;
WiFiServer server(80);
WiFiClient client;
const char* host = "192.168.98.159";
const int port = 90;
char ssid [15];
char password[15];
int addr;
#define EEPROM_SIZE 4
int buttonState = 0; 
uint8_t ipBuffer[4];
uint8_t gwayBuffer[4];
uint8_t temp[4];
uint8_t temp3[4];
String inp_cmd = "";
String ID = "";
char temp1 [15];
char temp2 [15];
char IP1[4],IP2[4],IP3[4],IP4[4];
char G1[4], G2[4],G3[4],G4[4];
int count=0;
char data;
uint8_t ind =0;
String serialResponse = "";
char x[10];
char y[10];
int sta = 0;
const long interval = 1000;
unsigned long time_now = 0;

void setup() {
   Serial2.begin(9600);
   Serial.begin(115200);
   pinMode(LED_BUILTIN, OUTPUT);
   pinMode(RST, INPUT);
   pinMode(15,OUTPUT);    
   digitalWrite(15,LOW);
   EEPROM.begin(40);
   if(EEPROM.read(4) ==255)
   {
     uint8_t ipBuffer[4] = {10,108,124,5};
     uint8_t gwayBuffer[4] ={10,108,124,10};
     char ssid[15] = "Smart_Move";
     char password[15] = "addverb@123";
     addr = 0;
     // Write in EEPROM
     for (int i = 0; i < 4; i++) {
       EEPROM.write(addr, ipBuffer[i]);
       addr += 1;}
     EEPROM.commit();
     addr = 5;
     for (int i = 0; i < 12; i++) {
       EEPROM.write(addr, ssid[i]);
       addr += 1;}
     EEPROM.commit();
     addr = 20;
     for (int i = 0; i < 12; i++) {
       EEPROM.write(addr, password[i]);
       addr += 1;}
     EEPROM.commit();
     addr = 35;
     for (int i = 0; i < 4; i++) {
       EEPROM.write(addr, gwayBuffer[i]);
       addr += 1;}
     EEPROM.commit();
     
     //IP Details
     IPAddress local_IP(ipBuffer[0], ipBuffer[1], ipBuffer[2], ipBuffer[3]);
     IPAddress gateway(gwayBuffer[0], gwayBuffer[1],gwayBuffer[2],gwayBuffer[3]);
     IPAddress subnet(255, 255, 255, 0);
     IPAddress primaryDNS(8, 8, 8, 8); 
     IPAddress secondaryDNS(8, 8, 4, 4);
     if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
     Serial.println("STA Failed to configure");} 
    // Connect to Wi-Fi network with SSID and password
     Serial.println("Connecting to ");
     Serial.println(ssid);
     WiFi.begin(ssid, password);
     while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");}
   // Print local IP address and start web server
     Serial.println("");
     Serial.println("WiFi connected.");
     Serial.println("IP address 1st: ");
     Serial.println(WiFi.localIP());
     server.begin();
     uint8_t sta = 111;
     EEPROM.write(4,sta); 
     EEPROM.commit();
     Serial.println(EEPROM.read(4));
   }
   else
   {
     for (int i = 0; i < 4; i++) {
     ipBuffer[i] = EEPROM.read(i);}
     for (int i = 0; i < 12; i++) {
     ssid[i] = EEPROM.read(i+5);}
     for (int i = 0; i < 12; i++) {
     password[i] = EEPROM.read(i+20);}
     for (int i = 0; i < 4; i++) {
     gwayBuffer[i] = EEPROM.read(i+35);}
     IPAddress local_IP(ipBuffer[0], ipBuffer[1], ipBuffer[2], ipBuffer[3]);
     IPAddress gateway(gwayBuffer[0], gwayBuffer[1],gwayBuffer[2],gwayBuffer[3]);
     IPAddress subnet(255, 255, 255, 0);
     IPAddress primaryDNS(8, 8, 8, 8); 
     IPAddress secondaryDNS(8, 8, 4, 4);
     if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
     Serial.println("STA Failed to configure");}
 // Connect to Wi-Fi network with SSID and password
     Serial.print("Connecting to 2nd ");
     Serial.println(ssid);
     WiFi.begin(ssid, password);
     while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");}
   // Print local IP address and start web server
     Serial.println("");
     Serial.println("WiFi connected.");
     Serial.println("IP address 2nd: ");
     Serial.println(WiFi.localIP());
     Serial.println(millis());
     server.begin();
   }
}

void loop() 
{  while (WiFi.status() != WL_CONNECTED)
 { 
     for (int i = 0; i < 4; i++) {
     ipBuffer[i] = EEPROM.read(i);}
     for (int i = 0; i < 12; i++) {
     ssid[i] = EEPROM.read(i+5);}
     for (int i = 0; i < 12; i++) {
     password[i] = EEPROM.read(i+20);}
     for (int i = 0; i < 4; i++) {
     gwayBuffer[i] = EEPROM.read(i+35);}
     IPAddress local_IP(ipBuffer[0], ipBuffer[1], ipBuffer[2], ipBuffer[3]);
     IPAddress gateway(gwayBuffer[0], gwayBuffer[1],gwayBuffer[2],gwayBuffer[3]);
     IPAddress subnet(255, 255, 255, 0);
     IPAddress primaryDNS(8, 8, 8, 8); 
     IPAddress secondaryDNS(8, 8, 4, 4);
     if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
     Serial.println("STA Failed to configure");}
 // Connect to Wi-Fi network with SSID and password
     Serial.print("Reconnecting ");
     Serial.println(ssid);
     WiFi.begin(ssid, password);
     while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");}
   // Print local IP address and start web server
     Serial.println("");
     Serial.println("WiFi connected.");
     Serial.println("IP address 2nd: ");
     Serial.println(WiFi.localIP());
     Serial.println(millis());
     server.begin();
}
WiFiClient client = server.available();
     int count = 0;
     int var = 0;
     int val;
   while(client.connected())
   {
     while(client.available()>0)
     {val = client.read();
       if(val == '1')
       {while(client.connected()) 
         {
          time_now = millis();
          var = count+1;
          client.println(var);
          count++;
          while(millis() < time_now + interval);
          Serial.println(millis());
       }Serial.println("Disconnected");
     }
   } 
     
 }
}

it's probably because of all the italic text and littles smileys with sun glasses 8)...

Hi, i am asking can you please help me out in solving the issue of ESP32 looping in while(client.connected()) loop when client internet connectivity loses.

I'm asking can you please fix your post using proper formatting and read forum rules.

why would you have two nested while(client.available()) ?

Also most examples you would find would also call client.stop();

have you looked at any tutorial at all or sample code from the libraries?

Thanks for your reply! I will go through the guidelines to post query. My doubt regarding WiFi library is that: When my ESP32 is acting as a server and my laptop make connection with it on a particular IP and somehow it looses WiFi connectivity the MY ESP32 is still looping in while(client.connected()) loop. I checked the status of client.connected() function, it return 1 when client is connected and 0 when disconnected but when client got disconnected because of internet connectivity then status is not getting 0. Can you please help me out Thanks Akansha

fix your post. use code tags please.

Sir, I have posted the code as per the forum rules. Can you please suggest me how can i inform my server about the connectivity issues of client? Regards Akansha

see answer in the other post since you double posted.... (bad!)