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

Miksior
Posts: 5
Joined: Thu Jan 09, 2020 7:52 pm

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

Postby Miksior » Tue Jun 16, 2020 10:36 pm

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:
  1. #include <WiFi.h>
  2.  
  3. WiFiServer server(80);                // Set a object server as a WiFiServer class
  4. IPAddress IP(192,168,4,1);           // Select ip and mask
  5. IPAddress mask = (255, 255, 255, 0);
  6.  
  7. void setup() {
  8.   Serial.begin(115200);
  9.   WiFi.mode(WIFI_AP);                     // Set Wi-Fi as access point/server
  10.   WiFi.softAP("ESP32", "123456789");                // SSID and Password for the AP
  11.   WiFi.softAPConfig(IP, IP, mask);        // Set our own desired IP address
  12.   server.begin();  // Begin the server
  13.  
  14.   Serial.println("Server started.");
  15.   Serial.print("IP: ");  
  16.   Serial.println(WiFi.softAPIP());        // .softAPIP calls for the IP of the access point which we set earlier
  17.   Serial.print("MAC:");  
  18.   Serial.println(WiFi.softAPmacAddress());  // Calls for the mac address
  19.   }
  20.  
  21. void loop() {
  22.   WiFiClient client = server.available(); // Return a client object to the class if there is a client available
  23.   client.setNoDelay(1);
  24.   if (!client) {return;}    // Return cuts the function (loop) if client class is not connected
  25.  
  26. //  String request = client.readStringUntil('\r');    // Reads string received until \r and saves as string
  27. //  Serial.print("From ");
  28. //  Serial.print(client.remoteIP());
  29. //  Serial.print(", port ");
  30. //  Serial.println(client.remotePort());
  31.  
  32.   if (client.remoteIP()[3] == 2){ // check the last byte of IP adress to check which esp connected
  33.   String request = client.readStringUntil('\r');
  34.   Serial.println(" Dostalem po TCP od 1 : " + request);                              
  35.   client.println(" Dostalem po TCP: " + request + "\r");   // Send the data with the \r so the client knows when to stop
  36.   }
  37.  
  38.     if (client.remoteIP()[3] == 3){
  39.   String request = client.readStringUntil('\r');
  40.   Serial.println(" Dostalem po TCP od 2 : " + request);                              
  41.   client.println(" Dostalem po TCP: " + request + "\r");   // Send the data with the \r so the client knows when to stop
  42.   }
  43.     }
and code for client:
  1. include <WiFi.h>
  2.  
  3. IPAddress ip(192, 168, 4, 1);
  4. const char* host = "192.168.4.1";
  5. const uint16_t port = 80;      
  6. int buffer;    
  7. int sygnal[100];  /
  8.  
  9. void setup()
  10. {
  11.   Serial.begin(115200);
  12.   Serial.println();
  13.   WiFi.begin("ESP32", "123456789");
  14.   Serial.print("Connecting");
  15.   while (WiFi.status() != WL_CONNECTED)
  16.   {
  17.     delay(500);
  18.     Serial.print(".");
  19.   }
  20.   Serial.println();
  21.   Serial.print("Connected, IP address: ");
  22.   Serial.println(WiFi.localIP());
  23.   for (int i=0; i <100; i++){  
  24.     sygnal[i]=i;
  25.   }
  26.   delay(2500);
  27. }
  28. void loop() {
  29.   Serial.print("connecting to ");
  30.   Serial.print(host);
  31.   Serial.print(':');
  32.   Serial.println(port);
  33.  
  34.   for (int i=0; i <100; i++){  //send 100 times
  35.     buffer=sygnal[i];
  36.   // Use WiFiClient class to create TCP connections
  37.     WiFiClient client;
  38.     if (!client.connect(host, port)) {
  39.       Serial.println("connection failed");
  40.       Serial.println("wait 3 sec...");
  41.       delay(3000);
  42.       return;
  43.     }
  44.     // send buffer to server
  45.   client.print(buffer);
  46.   client.println("\r");
  47.   Serial.print("Wyslalem po TCP: ");
  48.   Serial.println(buffer);
  49.   //odczytaj odpowiedz serwera
  50.   Serial.print("Odpowiedz: ");
  51.   String line = client.readStringUntil('\r');
  52.   Serial.println(line);
  53.   Serial.println("closing connection");
  54.   client.stop();
  55.   }
  56. //5 seconds delay
  57.   Serial.println("wait 5 sec...");
  58.   delay(5000);
  59. }
Maybe there is any working example of communications like this - with many ESP32, or maybe someone can give me a tip how to deal with it

lbernstone
Posts: 96
Joined: Mon Jul 22, 2019 3:20 pm

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

Postby lbernstone » Wed Jun 17, 2020 2:46 am

You will need to decode your backtrace using the tool at https://github.com/me-no-dev/EspExceptionDecoder .

Miksior
Posts: 5
Joined: Thu Jan 09, 2020 7:52 pm

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

Postby Miksior » Wed Jun 17, 2020 8:16 am

Hey, i did what u linked me and the result is:
Attachments
result.JPG
result.JPG (101.97 KiB) Viewed 519 times

Miksior
Posts: 5
Joined: Thu Jan 09, 2020 7:52 pm

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

Postby Miksior » Wed Jun 17, 2020 8:55 am

I updated result, in previous one i did mistake

lbernstone
Posts: 96
Joined: Mon Jul 22, 2019 3:20 pm

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

Postby lbernstone » Wed Jun 17, 2020 2:04 pm

I am unable to reproduce your problem, but it doesn't really look like it is the remoteIP call causing the crash.
Turn on Core Debug Level: Verbose to see if any additional information is available.
Put in more debug prints so you can see exactly where the error occurs (log_v is better for this than Serial.print)
This code works quite reliably for me without crash:

Code: Select all

void loop() {
  delay(100);
  WiFiClient client = server.available();
  if (!client) {return;}
  Serial.println(client.remoteIP()); 
}

Who is online

Users browsing this forum: No registered users and 11 guests