Hi pylon,
thank you for your reply.
My fault, I should have posted the minimal example. I have worked it out and the freezing disappeared. So the cause is somewhere in the code that I have not posted, as you expected. Not in the size of the buffer. I will go through the code again and try to find out what's wrong.
No, that code doesn't compile.
Maybe the reason is in that part of the code you're hiding from us.
How do you know if the code freezes there?
The client is ESP32. It just connects to the ESP32 server and downloads the data. The codes for the client and for the server are below.
What kind of client is connecting to your server?
Code of the client:
#include <WiFi.h>
#include <WiFiMulti.h>
#define N 512
WiFiMulti WiFiMulti;
const char* ssid = "ESP32-Access-Point";
const char* pass = "ESP-COME-1";
int status = WL_IDLE_STATUS;
IPAddress myIp;
IPAddress server(192,168,1,1);
// Initialize the client library
WiFiClient client;
int rec [N];
int i (0);
long cas1;
long cas2;
void setup() {
Serial.begin(9600);
Serial.println();
Serial.println();
Serial.println("Attempting to connect to WPA network...");
Serial.print("SSID: ");
Serial.println(ssid);
delay(10);
WiFiMulti.addAP(ssid, pass);
while(WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("Connected to wifi");
Serial.println("\nStarting connection...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
myIp = WiFi.localIP();
Serial.println(myIp);
}
}
void loop() {
// put your main code here, to run repeatedly:
float Dt;
cas1 = micros();
while(client.available()){
while(client.available() >= N*sizeof(int)){
client.read((uint8_t *)rec, N*sizeof(int));
//Serial.println("Data received");
//++i;
}
delay(1);
}
cas2 = micros();
Dt = (cas2-cas1)/1000.f;
if(Dt>0.1){
Serial.print("Data transfer time = ");
Serial.println(Dt);
}
}
Code of the server:
#include <WiFi.h>
#define N 512
WiFiServer server(80);
WiFiClient client1;
IPAddress serverIp(192, 168, 1, 1);
IPAddress NMask(255, 255, 255, 0);
const char* ssid = "ESP32-Access-Point";
const char* pssw = "ESP-COME-1";
int x [N];
int y [N];
int z [N];
long time1;
void setup(){
int k;
IPAddress IP;
Serial.begin(9600); // Start the serial terminal
//*************************** WiFi ***************************
// Connect to Wi-Fi network with SSID and password
Serial.println("Setting AP (Access Point)…");
// Remove the password parameter, if you want the AP (Access Point) to be open
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, pssw);
Serial.println("Wait 100 ms for AP_START...");
delay(100);
Serial.println("Set softAPConfig");
WiFi.softAPConfig(serverIp, serverIp, NMask);
IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
server.begin();
for(k=0;k<N;++k){
x[k] = 2*k;
y[k] = 2*k + N;
z[k] = 2*k + 2*N;
}
time1 = millis();
}
void loop(){
client1 = server.available();
if (client1) { // If a new client connects,
Serial.println("!....New Client...!"); // print a message out in the serial port
while (client1.connected()) { // loop while the client's connected
// odeslani dat klientovi po naplneni pole
if(millis() - time1 > 1000){
Serial.println("Sending data to the client");
time1 = millis();
client1.write( (uint8_t *)x, N*sizeof(int));
client1.write( (uint8_t *)y, N*sizeof(int));
client1.write( (uint8_t *)z, N*sizeof(int));
}
}
client1.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
else{
Serial.println("Waiting for a client ...");
delay(500);
}
}
With the codes above, when the client is disconnected from a power, the server does not disconnect the client and write neither "Waiting for the client" nor "Client disconnected.". Last output of my serial monitor is: Sending data to the client.
So the problem with unexpected disconnection of the client remains. Do you have any suggestion, how to fix it?
Using the client.connected() call. But the server might need some time to realize that the client is gone if the client not actively terminated the connection.