wificlient not running after 1 try - HTTP over AP esp32

Hi Guys,

I have established the AP (access point) on my esp32 so that i can make a GET request to my PC (connected to AP) and receive the payload. The issue is, the connection only run once and doesn't run again and I have to restart the esp32 or the localhost URL (on my PC) to initiate the connection again.
Can someone help me with the issue?
Is there something that i have to do from the client end (PC)

WiFiServer server1(80);
HTTPClient http;
String seerverini = "http://";
String serverName = ":5000/RRP/";
void setup() {
  /* 
   *  initialise the Serial output, the Network connection, the MQTT client
   *  and specify input/output pins.
   */
  // begin the serial monitor.
  // Set console and Sim baud rate
  SerialMon.begin(115200);
  delay(1000);
  SerialAT.begin(115200, SERIAL_8N1, 26, 27);
  delay(6000);
  
  // You can remove the password parameter if you want the AP to be open.
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  SerialMon.print("AP IP address: ");
  SerialMon.println(myIP);
void HTTP() {
  server1.begin();
  SerialMon.println("Server started");
  delay(1000);
  
  WiFiClient client = server1.available();   // listen for incoming clients
  SerialMon.println("Client available");
  if (client) {                             // if you get a client,
    SerialMon.println("assigned IP address: ");
    String Client_Server = client.remoteIP().toString();
    //String Client_Server = "192.168.4.2";
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {
        SerialMon.println(Client_Server);
        SerialMon.println("establishing API connection");
        String serverPath = seerverini+Client_Server+serverName;

        Serial.println(serverPath);
        http.begin(serverPath.c_str());
        
        int httpResponseCode = http.GET();
        if (httpResponseCode>0) {
          Serial.print("HTTP Response code: ");
          Serial.println(httpResponseCode);
          String payload = http.getString();
          Serial.println(payload);
          http.end();  
          client.stop();
          SerialMon.println("Client Disconnected.");
          server1.stop();
          delay(1000);
        }
        else {
          Serial.print("Error code: ");
          Serial.println(httpResponseCode);
          http.end();
          client.stop();
          server1.stop();   
          delay(1000);
          SerialMon.println("Client Disconnected.");
          }
      
    }

The issue is, the connection only run once and doesn't run again and I have to restart the esp32 or the localhost URL (on my PC) to initiate the connection again.

That's exactly what you programmed:

          server1.stop();

Why do you stop the server?

BTW, you code is rather special. You do a HTTP request if a request to port 80 of your ESP is made. What's the purpose of this code?

As @pylon pointed out, server1.stop() link needs to be removed and the code will work as expected.

Hi @pylon and @chall3ng3r

Thanks for the reply.

This code is just a part of the complete program. The http request is only initiated when i send the request to esp via mqtt from my server and so I thought about stopping the wifi client when the http request is completed. It works the first time but when i repeat the process the client is never found to be available even when the PC is connected to the esp.
I stopped the wifi server (server1) because the esp communicates to my server via 4G.
As proposed, I removed the server1.stop() but it still doesn't work as expected.

To clarify, my thought process is to start wifi server, make the http request, receive payload and stop the server whenever I initiate the http request (via mqtt from my server).

Thanks,
Zohair

Stopping and starting HTTP server is bad practice I believe. There's chance for leaving one instance running while another request comes in, which can lead to similar issues or runtime errors.

Take out the server starting process from client handling logic, start both MQTT and HTTP servers in Setup function and just manage and serve the clients when requests comes in.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.