user11033: I have an ESP8266 project that requires setting the SSID and password for a network via an Access Point.  Everything works fine, the only issue is if you send incorrect credentials to the access point, the server returns an empty response before the timeout period elapses, and the error message is never sent to the client.
If the connection is successful then the success message is appropriately sent to the client.  It's almost like the WiFi.begin() function detects the connection fails on its own, and something there is preventing me from sending my own response.
%  curl -X POST "http://192.168.1.1/authenticate?ssid=FakeNetworkpassword=87654321" 
curl: (52) Empty reply from server

Did some research and saw some mentions to keep the loop running, so I attempted to do so below, but to no avail.  Would appreciate any ideas, thanks.
#include "ESP8266WiFi.h"
#include "ESP8266HTTPClient.h"
#include "ESP8266WebServer.h"

HTTPClient http;
WiFiClient client;

ESP8266WebServer server(80);
IPAddress ipAP(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

unsigned long previousMillisConnection = 0;
unsigned long startingMillisConnection = 0;
const unsigned long CONNECTION_PERIOD = 1000;
const unsigned long CONNECTION_TIMEOUT = 10000;

int DELAY = 40;

char ssid[64] = { 0 };
char password[64] = { 0 };

char ssidNew[64];
char passNew[64];

const int STATE_DEFAULT = 0;
const int STATE_CREDS = 1;
int state = STATE_DEFAULT;

void setup() {
  Serial.begin(9600);
  
  WiFi.softAPConfig(ipAP, gateway, subnet);
  WiFi.softAP("ESP8266", "12345678");
  delay(100);
  
  server.on("/authenticate", HTTP_POST, onSetCredentials);
  server.begin();
  
  performWifiConnect(ssid, password);
  WiFi.waitForConnectResult(CONNECTION_TIMEOUT);
  
  Serial.println("Initialization complete");
}

void loop() {
  if (state == STATE_DEFAULT) {
    server.handleClient();
    if (WiFi.status() == WL_CONNECTED) {
      Serial.println("Connected!");
    }
  }

  else if (state == STATE_CREDS) {
    whileConnecting();
  }
  
  delay(DELAY);
}

void performWifiConnect(char ssid[], char password[]) {
  Serial.println("Attempting to connect to " + String(ssid) + "...");
  WiFi.setAutoReconnect(false);
  WiFi.persistent(false);
  WiFi.begin(ssid, password);
}

void onWifiConnected() {
  Serial.println("WiFi connected " + WiFi.localIP().toString());
  WiFi.setAutoReconnect(true);
  WiFi.persistent(true);
}

void onSetCredentials() {
  Serial.println("onSetCredentials");
  
  server.arg("ssid").toCharArray(ssidNew, 64);
  server.arg("password").toCharArray(passNew, 64);

  if (String(ssidNew) == String(ssid) && WiFi.status() == WL_CONNECTED) {
    Serial.println("Already connected to " + String(ssidNew));
    server.send(200, "application/json", "{ \"success\": true, \"changed\": false }");
  } else {
    Serial.println("Switching from " + String(ssid) + " to " + String(ssidNew));
    state = STATE_CREDS;
    performWifiConnect(ssidNew, passNew);
    startingMillisConnection = millis();
  }
}

void whileConnecting() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillisConnection >= CONNECTION_PERIOD) {
    previousMillisConnection = currentMillis;

    Serial.println("checking connection...");
    
    // Success
    if (WiFi.status() == WL_CONNECTED) {
      Serial.println("Connected to " + String(ssidNew) + " successfully!");
      
      strcpy(ssid, ssidNew);
      strcpy(password, passNew);
      
      server.send(200, "application/json", "{ \"success\": true, \"changed\": true }");

      state = STATE_DEFAULT;
    }
    
    // Failure
    if (currentMillis - startingMillisConnection >= CONNECTION_TIMEOUT) {
      Serial.println("connection failed, attempting to reconnect to old network!");
      Serial.println("ssidOld: " + String(ssid));
      Serial.println("passOld: " + String(password));

      String errorMessage = "Timed out while attempting to connect to " + String(ssidNew);
      Serial.println(errorMessage);
      server.send(500, "application/json", "{\"errorMessage\":\"" + errorMessage + "\"}");
      
      performWifiConnect(ssid, password);
      
      WiFi.waitForConnectResult(CONNECTION_TIMEOUT);

      state = STATE_DEFAULT;
      
      if (WiFi.status() != WL_CONNECTED) {
        Serial.println("Could not return to old network!");
      }
    }
  }
}


user37622: I recommend you to use the WiFiManager library. It has the WiFi provisioning solved.

To join an AP esp8266 must change the WiFi radio channel to the channel of the AP. Since the esp8266 has only one radio, the SoftAP must change the channel too. This causes the SoftAP clients to disconnect.
The solution is respond to the configuration request before connecting to AP, then let the configuration page do a new request for the result.
To illustrate here is a simple example:
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif

void setup() {

  Serial.begin(115200);
  delay(500);

#ifdef ESP32
//  WiFi.begin(); // use SSID and password stored by SDK. commented out to test the Configuration AP
#else
//  WiFi.disconnect(); // forget the persistent connection to test the Configuration AP
#endif

  // waiting for connection to remembered  Wifi network
  Serial.println("Waiting for connection to WiFi");
  WiFi.waitForConnectResult();

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println();
    Serial.println("Could not connect to WiFi. Starting configuration AP...");
    configAP();
  } else {
    Serial.println("WiFi connected");
  }
}

void loop() {
}

void configAP() {

  WiFiServer configWebServer(80);

  WiFi.mode(WIFI_AP_STA); // starts the default AP (factory default or setup as persistent)

  Serial.print("Connect your computer to the WiFi network ");
#ifdef ESP32
  Serial.print("to SSID of you ESP32"); // no getter for SoftAP SSID
#else
  Serial.print(WiFi.softAPSSID());
#endif
  Serial.println();
  IPAddress ip = WiFi.softAPIP();
  Serial.print("and enter http://");
  Serial.print(ip);
  Serial.println(" in a Web browser");

  configWebServer.begin();

  while (true) {

    WiFiClient client = configWebServer.available();
    if (client) {
      char line[64];
      int l = client.readBytesUntil('\n', line, sizeof(line));
      line[l] = 0;
      client.find((char*) "\r\n\r\n");
      if (strncmp_P(line, PSTR("POST"), strlen("POST")) == 0) {
        l = client.readBytes(line, sizeof(line));
        line[l] = 0;

        // parse the parameters sent by the html form
        const char* delims = "=&";
        strtok(line, delims);
        const char* ssid = strtok(NULL, delims);
        strtok(NULL, delims);
        const char* pass = strtok(NULL, delims);

        // send a response before attemting to connect to the WiFi network
        // because it will reset the SoftAP and disconnect the client station
        client.println(F("HTTP/1.1 200 OK"));
        client.println(F("Connection: close"));
        client.println(F("Refresh: 10")); // send a request after 10 seconds
        client.println();
        client.println(F("<html><body><h3>Configuration AP</h3><br>connecting...</body></html>"));
        client.stop();

        Serial.println();
        Serial.print("Attempting to connect to WPA SSID: ");
        Serial.println(ssid);
        WiFi.persistent(true);
        WiFi.setAutoConnect(true);
        WiFi.begin(ssid, pass);
        WiFi.waitForConnectResult();

        // configuration continues with the next request

      } else {

        client.println(F("HTTP/1.1 200 OK"));
        client.println(F("Connection: close"));
        client.println();
        client.println(F("<html><body><h3>Configuration AP</h3><br>"));

        int status = WiFi.status();
        if (status == WL_CONNECTED) {
          client.println(F("Connection successful. Ending AP."));
        } else {
          client.println(F("<form action='/' method='POST'>WiFi connection failed. Enter valid parameters, please.<br><br>"));
          client.println(F("SSID:<br><input type='text' name='i'><br>"));
          client.println(F("Password:<br><input type='password' name='p'><br><br>"));
          client.println(F("<input type='submit' value='Submit'></form>"));
        }
        client.println(F("</body></html>"));
        client.stop();

        if (status == WL_CONNECTED) {
          delay(1000); // to let the SDK finish the communication
          Serial.println("Connection successful. Ending AP.");
          configWebServer.stop();
          WiFi.mode(WIFI_STA);
          return;
        }
      }
    }
  }
}


user47164: There are several intertwined issues in this one.
Firstly, setup() has some code running before it should be.
That first problem is here:
  server.on("/authenticate", HTTP_POST, onSetCredentials);
  server.begin();
  
  performWifiConnect(ssid, password);
  WiFi.waitForConnectResult(CONNECTION_TIMEOUT);

You've already set ssid and password to be null, and server.begin() won't run the server yet (I'll get to that issue in a minute), so this is setting your credentials to be null at the start of the program. There's no point in running waitForConnectResult() then, because there is nothing to connect. There may be logic in setting performWifiConnect() though, since if you've ever set the Wi-Fi credentials with wifi.begin() before turning off persistent, those credentials are still remembered (it just won't remember new ones).
The next issue is that you're only handling server requests while the connection is not invalid. Thus, when there is an invalid credential for the network, the server doesn't run, and you don't get a response.
void loop() {
  if (state == STATE_DEFAULT) {
    server.handleClient();
    if (WiFi.status() == WL_CONNECTED) {
      Serial.println("Connected!");
    }
  }

  else if (state == STATE_CREDS) {
    whileConnecting();
  }
  
  delay(DELAY);
}

As you can see, server.handleclient() (the actual server; the ability to respond to requests) is only running while it does not have state == STATE_DEFAULT. That is then changed, state = STATE_CREDS;, during the callback for getting network credentials. You'll probably get a reply from the request handler that's currently running, but this will otherwise cause you problems since the server then never responds to any future requests. You must continually call server.handleclient() or nothing server-related will work.
But, something else is also wrong. You can only pass replies back from the server if you actually send them while the server is still processing a request.
else {
    Serial.println("Switching from " + String(ssid) + " to " + String(ssidNew));
    state = STATE_CREDS;
    performWifiConnect(ssidNew, passNew);
    startingMillisConnection = millis();
  }

As you can see, there is no server.send() in this branch, so you'll get no reply. This is likely the most immediate cause of your problem with an empty response.
Instead, you have that in the function whileConnecting(). As noted earlier, the server cannot send replies that aren't in the function it calls when it gets a request, or in functions called by that function. The server does not call whileConnecting(), loop() does. So it can't see or send that reply.
(Side note: This requirement of the server is implemented pretty weirdly, since you're calling server methods while one of its methods calls your code, only for the server to then ask itself what you told it while it was busy with your request, grab that buffer, and return it to the client. But it only does that check somewhere inside server.handleClient(), and other send() calls will either be ignored or get queued to send the next time the client makes a request.)
If you were to wait for a connection inside the server's request handler instead, and then send the error/success messages there too, this might work. It's still bad practice since the server (that part I said you always want to not hold up) and the entire rest of your code will be on hold while it waits to connect, but you'll probably have it work as long as there's nothing else time-critical or important going on.
The best approach would be to have your client poll a second address (or just the same address with no POST values, then have the server check whether there are no args) and have that say whether it is currently connected.
On that note, your connection checking code also has a problem. loop() has a delay in it, which is already going to bog things down. But you also have the whileConnecting function check for elapsed time. This is the proper way to coordinate multiple independent check-back-and-do-stuff operations, so you're aware of it, but this hasn't been transferred to the rest of the code.
Except, also in whileConnecting, you then call WiFi.waitForConnectResult(CONNECTION_TIMEOUT);. This will block that function until a result is present, which thus halts everything else for however long that timeout is. The only place you "should" use this (again, probably bad practice) is inside the server if you were trying to have it directly reply with the connection status or if there's not anything else important going on (which there is, so this is an issue). Since you're already running code periodically on a timer, just have this code check for the timeout directly (easiest method: call the connect function with the credentials, set the check-back time by timeout, then check again), and just reconnect then instead of forcing a wait. This will leave the server and the rest of your code responsive.
Now, with all (well, most) of that put together:
(NOTE: This code will need some changes made to the rest of your code, and may require adjustment itself, but it should get you very close. I suggest making a backup copy of your code first in case you need to reference or fix something. I also wrote this in a text editor, as I don't currently have access to the IDE to test this. There might be minor errors.)
Solution A (have the server wait and tell you the results):
(NOTE: also, delete whileConnecting() and all the state if you use this method; you want to make sure server.handleClient() is always being called until the connection is actually successful, or just plain always.)
void onSetCredentials() {
  Serial.println("onSetCredentials");
  
  server.arg("ssid").toCharArray(ssidNew, 64);
  server.arg("password").toCharArray(passNew, 64);

  if (String(ssidNew) == String(ssid) && WiFi.status() == WL_CONNECTED) {
    Serial.println("Already connected to " + String(ssidNew));
    server.send(200, "application/json", "{ \"success\": true, \"changed\": false }");
  } else {
    Serial.println("Switching from " + String(ssid) + " to " + String(ssidNew));
    performWifiConnect(ssidNew, passNew);
    startingMillisConnection = millis();
    //code to detect the server connection status and respond starts here//
    WiFi.waitForConnectResult(CONNECTION_TIMEOUT);
    if (WiFi.status() == WL_CONNECTED) {
      Serial.println("Connected to " + String(ssidNew) + " successfully!");
      
      strcpy(ssid, ssidNew);
      strcpy(password, passNew);
      
      server.send(200, "application/json", "{ \"success\": true, \"changed\": true }");
    }
    
    // Failure
    if (currentMillis - startingMillisConnection >= CONNECTION_TIMEOUT) {
      Serial.println("connection failed, attempting to reconnect to old network!");
      Serial.println("ssidOld: " + String(ssid));
      Serial.println("passOld: " + String(password));

      String errorMessage = "Timed out while attempting to connect to " + String(ssidNew);
      Serial.println(errorMessage);
      server.send(500, "application/json", "{\"errorMessage\":\"" + errorMessage + "\"}");
    }
  }
}

This solution (A) will probably give you the connection status back, assuming the Wi-Fi connection attempt times out before the cURL request/server does. I haven't actually tested this and I did mention it's kinda bad practice.
Solution B (Poll the server for connection status rather than waiting for a reply):
(Note: Again, delete any state logic and whileConnecting() from the other code.)
void onSetCredentials() {
  Serial.println("onSetCredentials");
  if (!server.hasArg("ssid")){ //check for no-arg request and return conn status//
    server.send("200", "text/plain", (WiFi.status() != WL_CONNECTED)? "Connected" : "Not Connected"); //or whatever other message(s) you want
    //You may also want to add some sort of Serial.print()-type logging here too, since I didn't.//
  }
  else{ //existing connection setup request handler resumes here//
    server.arg("ssid").toCharArray(ssidNew, 64);
    server.arg("password").toCharArray(passNew, 64);

    if (String(ssidNew) == String(ssid) && WiFi.status() == WL_CONNECTED) {
      Serial.println("Already connected to " + String(ssidNew));
    } else {
      Serial.println("Switching from " + String(ssid) + " to " + String(ssidNew));
      performWifiConnect(ssidNew, passNew);
      startingMillisConnection = millis();
    }
  }
}

With Solution B, you can call the cURL command to the ESP8266 without the ssid and password set, and you'll get a connection status back instead. This may take a few attempts while it's still in the process of connecting, but if it remains not connected, you know the credentials are wrong.

11033: <p>I have an ESP8266 project that requires setting the SSID and password for a network via an Access Point.  Everything works fine, the only issue is if you send incorrect credentials to the access point, the server returns an empty response before the timeout period elapses, and the error message is never sent to the client.</p>
<p>If the connection is successful then the success message is appropriately sent to the client.  It's almost like the <code>WiFi.begin()</code> function detects the connection fails on its own, and something there is preventing me from sending my own response.</p>
<pre><code>%  curl -X POST &quot;http://192.168.1.1/authenticate?ssid=FakeNetworkpassword=87654321&quot; 
curl: (52) Empty reply from server
</code></pre>
<p>Did some research and saw some mentions to keep the <code>loop</code> running, so I attempted to do so below, but to no avail.  Would appreciate any ideas, thanks.</p>
<pre><code>#include &quot;ESP8266WiFi.h&quot;
#include &quot;ESP8266HTTPClient.h&quot;
#include &quot;ESP8266WebServer.h&quot;

HTTPClient http;
WiFiClient client;

ESP8266WebServer server(80);
IPAddress ipAP(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

unsigned long previousMillisConnection = 0;
unsigned long startingMillisConnection = 0;
const unsigned long CONNECTION_PERIOD = 1000;
const unsigned long CONNECTION_TIMEOUT = 10000;

int DELAY = 40;

char ssid[64] = { 0 };
char password[64] = { 0 };

char ssidNew[64];
char passNew[64];

const int STATE_DEFAULT = 0;
const int STATE_CREDS = 1;
int state = STATE_DEFAULT;

void setup() {
  Serial.begin(9600);
  
  WiFi.softAPConfig(ipAP, gateway, subnet);
  WiFi.softAP(&quot;ESP8266&quot;, &quot;12345678&quot;);
  delay(100);
  
  server.on(&quot;/authenticate&quot;, HTTP_POST, onSetCredentials);
  server.begin();
  
  performWifiConnect(ssid, password);
  WiFi.waitForConnectResult(CONNECTION_TIMEOUT);
  
  Serial.println(&quot;Initialization complete&quot;);
}

void loop() {
  if (state == STATE_DEFAULT) {
    server.handleClient();
    if (WiFi.status() == WL_CONNECTED) {
      Serial.println(&quot;Connected!&quot;);
    }
  }

  else if (state == STATE_CREDS) {
    whileConnecting();
  }
  
  delay(DELAY);
}

void performWifiConnect(char ssid[], char password[]) {
  Serial.println(&quot;Attempting to connect to &quot; + String(ssid) + &quot;...&quot;);
  WiFi.setAutoReconnect(false);
  WiFi.persistent(false);
  WiFi.begin(ssid, password);
}

void onWifiConnected() {
  Serial.println(&quot;WiFi connected &quot; + WiFi.localIP().toString());
  WiFi.setAutoReconnect(true);
  WiFi.persistent(true);
}

void onSetCredentials() {
  Serial.println(&quot;onSetCredentials&quot;);
  
  server.arg(&quot;ssid&quot;).toCharArray(ssidNew, 64);
  server.arg(&quot;password&quot;).toCharArray(passNew, 64);

  if (String(ssidNew) == String(ssid) &amp;&amp; WiFi.status() == WL_CONNECTED) {
    Serial.println(&quot;Already connected to &quot; + String(ssidNew));
    server.send(200, &quot;application/json&quot;, &quot;{ \&quot;success\&quot;: true, \&quot;changed\&quot;: false }&quot;);
  } else {
    Serial.println(&quot;Switching from &quot; + String(ssid) + &quot; to &quot; + String(ssidNew));
    state = STATE_CREDS;
    performWifiConnect(ssidNew, passNew);
    startingMillisConnection = millis();
  }
}

void whileConnecting() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillisConnection &gt;= CONNECTION_PERIOD) {
    previousMillisConnection = currentMillis;

    Serial.println(&quot;checking connection...&quot;);
    
    // Success
    if (WiFi.status() == WL_CONNECTED) {
      Serial.println(&quot;Connected to &quot; + String(ssidNew) + &quot; successfully!&quot;);
      
      strcpy(ssid, ssidNew);
      strcpy(password, passNew);
      
      server.send(200, &quot;application/json&quot;, &quot;{ \&quot;success\&quot;: true, \&quot;changed\&quot;: true }&quot;);

      state = STATE_DEFAULT;
    }
    
    // Failure
    if (currentMillis - startingMillisConnection &gt;= CONNECTION_TIMEOUT) {
      Serial.println(&quot;connection failed, attempting to reconnect to old network!&quot;);
      Serial.println(&quot;ssidOld: &quot; + String(ssid));
      Serial.println(&quot;passOld: &quot; + String(password));

      String errorMessage = &quot;Timed out while attempting to connect to &quot; + String(ssidNew);
      Serial.println(errorMessage);
      server.send(500, &quot;application/json&quot;, &quot;{\&quot;errorMessage\&quot;:\&quot;&quot; + errorMessage + &quot;\&quot;}&quot;);
      
      performWifiConnect(ssid, password);
      
      WiFi.waitForConnectResult(CONNECTION_TIMEOUT);

      state = STATE_DEFAULT;
      
      if (WiFi.status() != WL_CONNECTED) {
        Serial.println(&quot;Could not return to old network!&quot;);
      }
    }
  }
}
</code></pre>
 37622: <p>I recommend you to use the WiFiManager library. It has the WiFi provisioning solved.</p>
<hr />
<p>To join an AP esp8266 must change the WiFi radio channel to the channel of the AP. Since the esp8266 has only one radio, the SoftAP must change the channel too. This causes the SoftAP clients to disconnect.</p>
<p>The solution is respond to the configuration request before connecting to AP, then let the configuration page do a new request for the result.</p>
<p>To illustrate here is a simple example:</p>
<pre><code>#ifdef ESP32
#include &lt;WiFi.h&gt;
#else
#include &lt;ESP8266WiFi.h&gt;
#endif

void setup() {

  Serial.begin(115200);
  delay(500);

#ifdef ESP32
//  WiFi.begin(); // use SSID and password stored by SDK. commented out to test the Configuration AP
#else
//  WiFi.disconnect(); // forget the persistent connection to test the Configuration AP
#endif

  // waiting for connection to remembered  Wifi network
  Serial.println(&quot;Waiting for connection to WiFi&quot;);
  WiFi.waitForConnectResult();

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println();
    Serial.println(&quot;Could not connect to WiFi. Starting configuration AP...&quot;);
    configAP();
  } else {
    Serial.println(&quot;WiFi connected&quot;);
  }
}

void loop() {
}

void configAP() {

  WiFiServer configWebServer(80);

  WiFi.mode(WIFI_AP_STA); // starts the default AP (factory default or setup as persistent)

  Serial.print(&quot;Connect your computer to the WiFi network &quot;);
#ifdef ESP32
  Serial.print(&quot;to SSID of you ESP32&quot;); // no getter for SoftAP SSID
#else
  Serial.print(WiFi.softAPSSID());
#endif
  Serial.println();
  IPAddress ip = WiFi.softAPIP();
  Serial.print(&quot;and enter http://&quot;);
  Serial.print(ip);
  Serial.println(&quot; in a Web browser&quot;);

  configWebServer.begin();

  while (true) {

    WiFiClient client = configWebServer.available();
    if (client) {
      char line[64];
      int l = client.readBytesUntil('\n', line, sizeof(line));
      line[l] = 0;
      client.find((char*) &quot;\r\n\r\n&quot;);
      if (strncmp_P(line, PSTR(&quot;POST&quot;), strlen(&quot;POST&quot;)) == 0) {
        l = client.readBytes(line, sizeof(line));
        line[l] = 0;

        // parse the parameters sent by the html form
        const char* delims = &quot;=&amp;&quot;;
        strtok(line, delims);
        const char* ssid = strtok(NULL, delims);
        strtok(NULL, delims);
        const char* pass = strtok(NULL, delims);

        // send a response before attemting to connect to the WiFi network
        // because it will reset the SoftAP and disconnect the client station
        client.println(F(&quot;HTTP/1.1 200 OK&quot;));
        client.println(F(&quot;Connection: close&quot;));
        client.println(F(&quot;Refresh: 10&quot;)); // send a request after 10 seconds
        client.println();
        client.println(F(&quot;&lt;html&gt;&lt;body&gt;&lt;h3&gt;Configuration AP&lt;/h3&gt;&lt;br&gt;connecting...&lt;/body&gt;&lt;/html&gt;&quot;));
        client.stop();

        Serial.println();
        Serial.print(&quot;Attempting to connect to WPA SSID: &quot;);
        Serial.println(ssid);
        WiFi.persistent(true);
        WiFi.setAutoConnect(true);
        WiFi.begin(ssid, pass);
        WiFi.waitForConnectResult();

        // configuration continues with the next request

      } else {

        client.println(F(&quot;HTTP/1.1 200 OK&quot;));
        client.println(F(&quot;Connection: close&quot;));
        client.println();
        client.println(F(&quot;&lt;html&gt;&lt;body&gt;&lt;h3&gt;Configuration AP&lt;/h3&gt;&lt;br&gt;&quot;));

        int status = WiFi.status();
        if (status == WL_CONNECTED) {
          client.println(F(&quot;Connection successful. Ending AP.&quot;));
        } else {
          client.println(F(&quot;&lt;form action='/' method='POST'&gt;WiFi connection failed. Enter valid parameters, please.&lt;br&gt;&lt;br&gt;&quot;));
          client.println(F(&quot;SSID:&lt;br&gt;&lt;input type='text' name='i'&gt;&lt;br&gt;&quot;));
          client.println(F(&quot;Password:&lt;br&gt;&lt;input type='password' name='p'&gt;&lt;br&gt;&lt;br&gt;&quot;));
          client.println(F(&quot;&lt;input type='submit' value='Submit'&gt;&lt;/form&gt;&quot;));
        }
        client.println(F(&quot;&lt;/body&gt;&lt;/html&gt;&quot;));
        client.stop();

        if (status == WL_CONNECTED) {
          delay(1000); // to let the SDK finish the communication
          Serial.println(&quot;Connection successful. Ending AP.&quot;);
          configWebServer.stop();
          WiFi.mode(WIFI_STA);
          return;
        }
      }
    }
  }
}
</code></pre>
 47164: <p>There are several intertwined issues in this one.</p>
<p>Firstly, <code>setup()</code> has some code running before it should be.</p>
<p>That first problem is here:</p>
<pre><code>  server.on(&quot;/authenticate&quot;, HTTP_POST, onSetCredentials);
  server.begin();
  
  performWifiConnect(ssid, password);
  WiFi.waitForConnectResult(CONNECTION_TIMEOUT);
</code></pre>
<p>You've already set <code>ssid</code> and <code>password</code> to be null, and server.begin() won't run the server yet (I'll get to that issue in a minute), so this is setting your credentials to be null at the start of the program. There's no point in running <code>waitForConnectResult()</code> then, because there is nothing to connect. There may be logic in setting <code>performWifiConnect()</code> though, since if you've ever set the Wi-Fi credentials with <code>wifi.begin()</code> <em>before</em> turning off persistent, those credentials are still remembered (it just won't remember new ones).</p>
<p>The next issue is that you're only handling server requests while the connection <em>is not invalid</em>. Thus, when there is an invalid credential for the network, the server doesn't run, and you don't get a response.</p>
<pre><code>void loop() {
  if (state == STATE_DEFAULT) {
    server.handleClient();
    if (WiFi.status() == WL_CONNECTED) {
      Serial.println(&quot;Connected!&quot;);
    }
  }

  else if (state == STATE_CREDS) {
    whileConnecting();
  }
  
  delay(DELAY);
}
</code></pre>
<p>As you can see, <code>server.handleclient()</code> (the actual server; the ability to respond to requests) is only running while it does not have <code>state == STATE_DEFAULT</code>. <em>That</em> is then changed, <code>state = STATE_CREDS;</code>, during the callback for getting network credentials. You'll probably get a reply from the request handler that's currently running, but this will otherwise cause you problems since the server then never responds to any future requests. You <strong>must</strong> continually call <code>server.handleclient()</code> or nothing server-related will work.</p>
<p>But, something else is also wrong. You can only pass replies back from the server if you actually send them <em>while the server is still processing a request.</em></p>
<pre><code>else {
    Serial.println(&quot;Switching from &quot; + String(ssid) + &quot; to &quot; + String(ssidNew));
    state = STATE_CREDS;
    performWifiConnect(ssidNew, passNew);
    startingMillisConnection = millis();
  }
</code></pre>
<p>As you can see, there is no <code>server.send()</code> in this branch, so you'll get no reply. This is likely the most immediate cause of your problem with an empty response.</p>
<p>Instead, you have that in the function <code>whileConnecting()</code>. As noted earlier, the server cannot send replies that aren't in the function it calls when it gets a request, or in functions called by that function. The server does not call <code>whileConnecting()</code>, <code>loop()</code> does. So it can't see or send that reply.</p>
<p>(Side note: This requirement of the server is implemented pretty weirdly, since you're calling server methods while one of its methods calls your code, only for the server to then ask itself what you told it while it was busy with your request, grab that buffer, and return it to the client. But it <strong>only</strong> does that check somewhere inside <code>server.handleClient()</code>, and other <code>send()</code> calls will either be ignored or get queued to send the next time the client makes a request.)</p>
<p>If you were to wait for a connection inside the server's request handler instead, and then send the error/success messages there too, this might work. It's still bad practice since the server (that part I said you always want to <strong>not</strong> hold up) and the entire rest of your code will be on hold while it waits to connect, but you'll <em>probably</em> have it work <strong>as long as there's nothing else time-critical or important going on</strong>.</p>
<p>The best approach would be to have your client poll a second address (or just the same address with no POST values, then have the server check whether there are no args) and have that say whether it is currently connected.</p>
<p>On that note, your connection checking code also has a problem. <code>loop()</code> has a delay in it, which is already going to bog things down. But you also have the whileConnecting function check for elapsed time. This <em>is</em> the proper way to coordinate multiple independent check-back-and-do-stuff operations, so you're aware of it, but this hasn't been transferred to the rest of the code.</p>
<p>Except, <strong>also</strong> in whileConnecting, you then call <code>WiFi.waitForConnectResult(CONNECTION_TIMEOUT);</code>. This will <strong>block</strong> that function until a result is present, which thus halts everything else for however long that timeout is. The only place you &quot;should&quot; use this (again, probably bad practice) is inside the server if you were trying to have it directly reply with the connection status <strong>or</strong> if there's not anything else important going on (which there is, so this is an issue). Since you're already running code periodically on a timer, just have this code check for the timeout directly (easiest method: call the connect function with the credentials, set the check-back time by <code>timeout</code>, then check again), and just reconnect then instead of forcing a wait. This will leave the server and the rest of your code responsive.</p>
<p>Now, with all (well, most) of that put together:
(NOTE: This code will need some changes made to the rest of your code, and may require adjustment itself, but it should get you very close. I suggest making a backup copy of your code first in case you need to reference or fix something. I also wrote this in a text editor, as I don't currently have access to the IDE to test this. There might be minor errors.)</p>
<p>Solution A (have the server wait and tell you the results):
(NOTE: also, delete <code>whileConnecting()</code> and all the <code>state</code> if you use this method; you want to make sure server.handleClient() is always being called until the connection is actually successful, or just plain always.)</p>
<pre><code>void onSetCredentials() {
  Serial.println(&quot;onSetCredentials&quot;);
  
  server.arg(&quot;ssid&quot;).toCharArray(ssidNew, 64);
  server.arg(&quot;password&quot;).toCharArray(passNew, 64);

  if (String(ssidNew) == String(ssid) &amp;&amp; WiFi.status() == WL_CONNECTED) {
    Serial.println(&quot;Already connected to &quot; + String(ssidNew));
    server.send(200, &quot;application/json&quot;, &quot;{ \&quot;success\&quot;: true, \&quot;changed\&quot;: false }&quot;);
  } else {
    Serial.println(&quot;Switching from &quot; + String(ssid) + &quot; to &quot; + String(ssidNew));
    performWifiConnect(ssidNew, passNew);
    startingMillisConnection = millis();
    //code to detect the server connection status and respond starts here//
    WiFi.waitForConnectResult(CONNECTION_TIMEOUT);
    if (WiFi.status() == WL_CONNECTED) {
      Serial.println(&quot;Connected to &quot; + String(ssidNew) + &quot; successfully!&quot;);
      
      strcpy(ssid, ssidNew);
      strcpy(password, passNew);
      
      server.send(200, &quot;application/json&quot;, &quot;{ \&quot;success\&quot;: true, \&quot;changed\&quot;: true }&quot;);
    }
    
    // Failure
    if (currentMillis - startingMillisConnection &gt;= CONNECTION_TIMEOUT) {
      Serial.println(&quot;connection failed, attempting to reconnect to old network!&quot;);
      Serial.println(&quot;ssidOld: &quot; + String(ssid));
      Serial.println(&quot;passOld: &quot; + String(password));

      String errorMessage = &quot;Timed out while attempting to connect to &quot; + String(ssidNew);
      Serial.println(errorMessage);
      server.send(500, &quot;application/json&quot;, &quot;{\&quot;errorMessage\&quot;:\&quot;&quot; + errorMessage + &quot;\&quot;}&quot;);
    }
  }
}
</code></pre>
<p>This solution (A) will <em>probably</em> give you the connection status back, assuming the Wi-Fi connection attempt times out before the cURL request/server does. I haven't actually tested this and I did mention it's kinda bad practice.</p>
<p>Solution B (Poll the server for connection status rather than waiting for a reply):
(Note: Again, delete any <code>state</code> logic and <code>whileConnecting()</code> from the other code.)</p>
<pre><code>void onSetCredentials() {
  Serial.println(&quot;onSetCredentials&quot;);
  if (!server.hasArg(&quot;ssid&quot;)){ //check for no-arg request and return conn status//
    server.send(&quot;200&quot;, &quot;text/plain&quot;, (WiFi.status() != WL_CONNECTED)? &quot;Connected&quot; : &quot;Not Connected&quot;); //or whatever other message(s) you want
    //You may also want to add some sort of Serial.print()-type logging here too, since I didn't.//
  }
  else{ //existing connection setup request handler resumes here//
    server.arg(&quot;ssid&quot;).toCharArray(ssidNew, 64);
    server.arg(&quot;password&quot;).toCharArray(passNew, 64);

    if (String(ssidNew) == String(ssid) &amp;&amp; WiFi.status() == WL_CONNECTED) {
      Serial.println(&quot;Already connected to &quot; + String(ssidNew));
    } else {
      Serial.println(&quot;Switching from &quot; + String(ssid) + &quot; to &quot; + String(ssidNew));
      performWifiConnect(ssidNew, passNew);
      startingMillisConnection = millis();
    }
  }
}
</code></pre>
<p>With Solution B, you can call the cURL command to the ESP8266 <em>without</em> the <code>ssid</code> and <code>password</code> set, and you'll get a connection status back instead. This may take a few attempts while it's still in the process of connecting, but if it <em>remains</em> not connected, you know the credentials are wrong.</p>
