Local server stops working

Hello to everyone! I am very new to community and to arduino environment. I have started a small project using my arduino GIGA R1 WiFi.
I do not know if I have to post my code as it is almost 1000 lines. The logic behind it is that when it is powered on if there are no saved datas in its memory then it starts an access point where a user can connect to it and pass al the necessaries parameters to login in a local network. When it is connecting then the AP stops and it creates a server in a specific ip and port. At first I do get/post requests with success. But after some time, all the requests I do fails. I use some logs and I verify that: 1) my server is running, 2) Arduino is connecting to local network, 3) the flow do not enter in if (client.connected() { .... }, 4) I use client.flush(); client.stop(); .
I post the void where the requests for server are coming.

void AccessPointRequests() {
  WiFiClient client;
  
  if (isServerRunning) {
    client = elevatorServer.available();
  
  } else {
    client = server.available();
  }

  if (status == WL_CONNECTED) {

    if (client.connected()) {
      TurnOnBlueLed();

      String request = "";

      if (Serial) Serial.println("There is an incoming message!");
      
      if (Serial) Serial.println("New Client connected for Elevator Module!");
      request = client.readStringUntil('\r');

      // Handle GET Request
      if (request.indexOf("GET / ") >= 0) {
        client.print("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n");
        client.print("Hello, this is a development from Poseidon Software!");

      } else if (request.indexOf("GET /question1") >= 0) {
        sendResponse1(client);

      } else if (request.indexOf("GET /question2") >= 0) {
        sendResponse2(client);

      } else if (request.indexOf("GET /question3") >= 0) {
        sendResponse3(client);

      }
      // Handle POST Request
      else if (request.indexOf("POST / ") >= 0) {
        handlePost(client, request);

      } else {
        client.print("HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n404: Not Found");
      }

      client.flush();
      client.stop();

      contentType = "";
      AuthorType = "";
      if (Serial) Serial.println("Client disconnected from Elevator Module!");

      TurnOnGreenLed();
    }

    return;
  }
...
...
}

I have tried almost anything and I do not understand why all the requests after some times 30 minutes, or 20 minutes, in general in random times, my requests fail. I remind you that when arduino starts, all the requests are working with no problem.

you could try these changes

I will check it! Thank you @Juraj !

Can anyone else suggest something? The problem still exists. The only update is that I left my arduino connecting to plug using an AC adapter and after almost one day it was still worked (usually after half day it stopped to work). But now it is not working again...
Just a remind, my program in arduino turn on some leds. If I sent a post command i turn on some leds and when i do get i receive the leds that they are turned on. There are also some buttons in my arduino wiring. The buttons are working all the time. I have never faced a problem using my buttons, however the server seems to stop even if it was worked with no problem before.

Hi, have you tried making your WiFiClient global?

No, I will give it a try.

@steve9 that did not help. I did the necessaries changes but the problem insists.

@techadmin3 my next suggestion would be to create a cutdown version of your sketch that is as small as possible but still reproduces the failure. Others can then review/test and maybe find the problem

1 Like

@steve9 Yes, I have come to that solution as I can not detect any other problem. Thank you very much for the help!