title: ESP8266 - Setting Wifi Credentials programmatically and then checking they are valid, and then change them if they are not (without reset)
body: <p>I have an NodeMCU chip that needs to connect to my home wifi and post an http request. I use the chip in <strong>WIFI_STA_AP</strong> mode as I need the chip to both accept requests via http and issue requests by http.</p>
  
  <p>I do not want to hard-code my home's SSID/Password into the chip, so I have written some code that places the ESP (NodeMCU) into AP mode, receives the SSID/Pass via an http request and saves it on EEPROM. </p>
  
  <p>This works great.</p>
  
  <p>In the code below, <strong>onTestWifi()</strong> is called when I call <strong><a href="http://192.168.4.1/test_wifi?wifi_ssid=mySsid&amp;wifi_password=myPassword" rel="nofollow noreferrer">http://192.168.4.1/test_wifi?wifi_ssid=mySsid&amp;wifi_password=myPassword</a></strong>. The ssid and password are provided to the <strong>WiFi.begin()</strong> function. However, if I accidentally type in the wrong password and use it in <strong>WiFi.begin()</strong>, the connection will always fail until reset the chip and then insert the correct password.</p>
  
  <p>What am I missing? Is it possible to change the ESP's wifi credentials programmatically, without having to reset the chip? Resetting the chip causes the client (in the case, an iPhone app) to disconnect from the chip and this breaks the entire program flow.</p>
  
  <p>Here's the experimentation code I am using:</p>
  
  <pre><code>#include &lt;ESP8266WiFiMulti.h&gt;
  #include &lt;WiFiClient.h&gt;
  #include &lt;ESP8266HTTPClient.h&gt;
  #include &lt;ESP8266WebServer.h&gt;
  #include &lt;ESP8266mDNS.h&gt;
  
  ESP8266WebServer server(80);
  ESP8266WiFiMulti WiFiMulti;
  
  void setup() {
    Serial.begin(115200);
    Serial.println("");
    delay(100);
    Serial.println("Starting...");
    WiFi.persistent(false);
    WiFi.mode(WIFI_AP_STA);
    WiFi.softAP("APSSID");
    IPAddress ip = WiFi.softAPIP();
    server.on("/test_wifi", onTestWifi);
    server.begin();
    yield();
  }
  
  void onTestWifi()
  {
    char ssid[30] = "";
    char password[30] = "";
    for (int i = 0; i &lt; server.args(); i++)
    {
      String n = server.argName(i);
      String v = server.arg(i);
      if (n == "wifi_ssid")
        v.toCharArray(ssid, 30);
      else if (n == "wifi_password")
        v.toCharArray(password, 30);
      yield();
    }
    Serial.print("Connecting to: ");
    Serial.print(ssid);
    Serial.print(" ");
    Serial.println(password);
    delay(100);
    WiFi.begin(ssid, password);
    yield();
    int counter = 0;
    while (WiFi.status() != WL_CONNECTED &amp;&amp; counter &lt; 10)
    { // Wait for the Wi-Fi to connect
      delay(1000);
      Serial.print(++counter);
      Serial.print(' ');
    }
  
    if (WiFi.status() != WL_CONNECTED)
    { // Failed to connect.
      Serial.println("Connection failed!");
  
    }
    else {
      Serial.println("Connection succeeded!");
    }
    yield();
  }
  
  void loop() {
    server.handleClient();
  }```
  
  </code></pre>