TCP/IP communication between few ESP32 - memory crash with client.remoteIP()

Hello everyone!

I am trying to find solution to my problem for few days but i almost gave up…

i try to set up communication via TCP/IP between few ESP32 modules, (1 is server and rest are clients sending data). I did it succesfully but when i try to recognize each ESP32 by IP (by calling client.remoteIP()) i got memory crash like this:
Guru Meditation Error: Core 0 panic’ed (InstrFetchProhibited). Exception was unhandled.
Core 0 register dump:
PC : 0x00000000 PS : 0x00060e30 A0 : 0x801100ad A1 : 0x3ffb3d30
A2 : 0x3ffccd7c A3 : 0x3ffccfb0 A4 : 0x3ffcb340 A5 : 0x3ffcb320
A6 : 0x0204a8c0 A7 : 0x0c04a8c0 A8 : 0x8010ff50 A9 : 0x3ffb3cf0
A10 : 0x3ffccd8c A11 : 0x3ffccfb0 A12 : 0x3ffb3d3c A13 : 0x00000044
A14 : 0x00000001 A15 : 0x00000006 SAR : 0x00000010 EXCCAUSE: 0x00000014
EXCVADDR: 0x00000000 LBEG : 0x4000c349 LEND : 0x4000c36b LCOUNT : 0x00000000

Backtrace: 0x00000000:0x3ffb3d30 0x401100aa:0x3ffb3d70 0x4011d011:0x3ffb3d90 0x40122019:0x3ffb3dd0 0x401272b6:0x3ffb3df0 0x401109cf:0x3ffb3e10 0x40088b7d:0x3ffb3e40

Like i said if i dont call for client.remoteIP function i am able to send data - but i need this to recognize every ESP and sord received data properly
When i call client.remoteIP() only once it works properly (but not always, sometimes i need to restart clients few times to stabilize)

I attach code for server:

#include <WiFi.h>
 
WiFiServer server(80);                // Set a object server as a WiFiServer class
IPAddress IP(192,168,4,1);           // Select ip and mask
IPAddress mask = (255, 255, 255, 0);
 
void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_AP);                     // Set Wi-Fi as access point/server
  WiFi.softAP("ESP32", "123456789");                // SSID and Password for the AP
  WiFi.softAPConfig(IP, IP, mask);        // Set our own desired IP address
  server.begin();  // Begin the server
 
  Serial.println("Server started.");
  Serial.print("IP: ");  
  Serial.println(WiFi.softAPIP());        // .softAPIP calls for the IP of the access point which we set earlier
  Serial.print("MAC:");  
  Serial.println(WiFi.softAPmacAddress());  // Calls for the mac address
  }
 
void loop() {
  WiFiClient client = server.available(); // Return a client object to the class if there is a client available
  client.setNoDelay(1);
  if (!client) {return;}    // Return cuts the function (loop) if client class is not connected
 
//  String request = client.readStringUntil('\r');    // Reads string received until \r and saves as string
//  Serial.print("From ");
//  Serial.print(client.remoteIP());
//  Serial.print(", port ");
//  Serial.println(client.remotePort());
 
  if (client.remoteIP()[3] == 2){ // check the last byte of IP adress to check which esp connected
  String request = client.readStringUntil('\r');
  Serial.println(" Dostalem po TCP od 1 : " + request);                              
  client.println(" Dostalem po TCP: " + request + "\r");   // Send the data with the \r so the client knows when to stop
  }
 
    if (client.remoteIP()[3] == 3){
  String request = client.readStringUntil('\r');
  Serial.println(" Dostalem po TCP od 2 : " + request);                              
  client.println(" Dostalem po TCP: " + request + "\r");   // Send the data with the \r so the client knows when to stop
  }
    }

And client:

include <WiFi.h>
 
IPAddress ip(192, 168, 4, 1);
const char* host = "192.168.4.1";
const uint16_t port = 80;      
int buffer;    
int sygnal[100];  /
 
void setup()
{
  Serial.begin(115200);
  Serial.println();
  WiFi.begin("ESP32", "123456789");
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
  for (int i=0; i <100; i++){  
    sygnal[i]=i;
  }
  delay(2500);
}
void loop() {
  Serial.print("connecting to ");
  Serial.print(host);
  Serial.print(':');
  Serial.println(port);
 
  for (int i=0; i <100; i++){  //send 100 times
    buffer=sygnal[i];
  // Use WiFiClient class to create TCP connections
    WiFiClient client;
    if (!client.connect(host, port)) {
      Serial.println("connection failed");
      Serial.println("wait 3 sec...");
      delay(3000);
      return;
    }
    // send buffer to server
  client.print(buffer);
  client.println("\r");
  Serial.print("Wyslalem po TCP: ");
  Serial.println(buffer);
  //odczytaj odpowiedz serwera
  Serial.print("Odpowiedz: ");
  String line = client.readStringUntil('\r');
  Serial.println(line);
  Serial.println("closing connection");
  client.stop();
  }
//5 seconds delay
  Serial.println("wait 5 sec...");
  delay(5000);
}

You'll gain more information by installing the ESP Exception decoder GitHub - me-no-dev/EspExceptionDecoder: Exception Stack Trace Decoder for ESP8266 and ESP32 into your Arduino IDE.

Hey, ty for advice, i did it. here is the result:

Why not post the image in img code tags?

Like so:

?

I am not interested in downloading an image to my computer just so I can view it and delete it.

I modified it, now its in my post, sorry for that

To me, the error look like an array is going out of bounds. Does the client.remoteIP()[3] call fail the first time or the 2nd time its called?

1st time: if (client.remoteIP()[3] == 2)

2nd time: if (client.remoteIP()[3] == 3)

As you cannot guarantee that client.remoteIP()[3] will be the same value between calls you might want to consider putting client.remoteIP()[3] into a variable.

int something = client.remoteIP()[3];
 if ( something == 2 );
 if ( something == 3 );

Ty for reply

I did the same what u proposed, still there is the same crash...

That's all I got for you.

#include <WiFi.h>
 
WiFiServer server(80);                // Set a object server as a WiFiServer class
IPAddress IP(192,168,4,1);           // Select ip and mask
IPAddress mask = (255, 255, 255, 0);
 
void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_AP);                     // Set Wi-Fi as access point/server
  WiFi.softAP("ESP32", "123456789");                // SSID and Password for the AP
  WiFi.softAPConfig(IP, IP, mask);        // Set our own desired IP address
  server.begin();  // Begin the server
 
  Serial.println("Server started.");
  Serial.print("IP: "); 
  Serial.println(WiFi.softAPIP());        // .softAPIP calls for the IP of the access point which we set earlier
  Serial.print("MAC:"); 
  Serial.println(WiFi.softAPmacAddress());  // Calls for the mac address
  }
 
void loop() {
  WiFiClient client = server.available(); // Return a client object to the class if there is a client available
  client.setNoDelay(1);
  if (!client) {return;}    // Return cuts the function (loop) if client class is not connected
 
//  String request = client.readStringUntil('\r');    // Reads string received until \r and saves as string
//  Serial.print("From ");
//  Serial.print(client.remoteIP());
//  Serial.print(", port ");
//  Serial.println(client.remotePort());
int x = client.remoteIP()[3] ;
Serial.println( x);
  //if (client.remoteIP()[3] == 2){ // check the last byte of IP adress to check which esp connected
  //String request = client.readStringUntil('\r');
  //Serial.println(" Dostalem po TCP od 1 : " + request);                             
  //client.println(" Dostalem po TCP: " + request + "\r");   // Send the data with the \r so the client knows when to stop
  //}
 
    //if (client.remoteIP()[3] == 3){
  //String request = client.readStringUntil('\r');
  // Serial.println(" Dostalem po TCP od 2 : " + request);                             
  //client.println(" Dostalem po TCP: " + request + "\r");   // Send the data with the \r so the client knows when to stop
  //}
    }

does that fail?
if not

if ( x == 2){ // check the last byte of IP adress to check which esp connected
  //String request = client.readStringUntil('\r');
  //Serial.println(" Dostalem po TCP od 1 : " + request);                             
  //client.println(" Dostalem po TCP: " + request + "\r");   // Send the data with the \r so the client knows when to stop
  }

does that fail?

if not

if ( x == 2){ // check the last byte of IP adress to check which esp connected
  String request = client.readStringUntil('\r');
  //Serial.println(" Dostalem po TCP od 1 : " + request);                             
  //client.println(" Dostalem po TCP: " + request + "\r");   // Send the data with the \r so the client knows when to stop
  }

does that fail?
if not

if ( x == 2){ // check the last byte of IP adress to check which esp connected
  String request = client.readStringUntil('\r');
  Serial.println(" Dostalem po TCP od 1 : " + request);                             
  //client.println(" Dostalem po TCP: " + request + "\r");   // Send the data with the \r so the client knows when to stop
  }

does that fail?
if not

if ( x == 2){ // check the last byte of IP adress to check which esp connected
  String request = client.readStringUntil('\r');
  Serial.println(" Dostalem po TCP od 1 : " + request);                             
  client.println(" Dostalem po TCP: " + request + "\r");   // Send the data with the \r so the client knows when to stop
  }

does that fail?