handling WiFi.begin and esp8266server.begin

Hi,
Trying to onfigure a WiFi connection through webpage.
So I initially configure an AP.
Then open a webpage for entering WiFi ssid .

Issue is that whenever I call WiFi.begin my webpage does not open as it hangs in following while loop after

WiFi.begin();
While(WiFi.status!=WL_CONNECTED)
{Serial.print(".");}

webServer.begin(); is not reached,hence although AP is configured webpage is not loaded.

I want a solution so that my webpage gets loaded and whenever I enter WiFi ssid wifi gets configured.

Moving webServer.begin() before WiFi.begin() does not help either.
Moving WiFi.begin() into loop() resets the device frequently.
Kindly provide inputs.
Regards
Uday

why not use WiFiManager library?

Have almost completed all coding steps ...but for this issue ..got it working but esp8266 resets...if there is no work around then will opt for WiFi manager.
is it a problem as I am using same port 80 for WiFi.begin and esp8266webserver.
There should be some solution .

Yes there is, had my had strung over it for a while and found it after googling for quite... a while, can't remember where
Anyway once you have an AP and are connected to it, you enter the details, all is good.
Then you call

WiFi.begin();
While(WiFi.status!=WL_CONNECTED) Serial.print(".");

(honestly if it's only 1 command i'd either stick it on the same line and leave the braces out or leave the braces and spread out over a few)
anyway you then need to call WiFi.reconnect();That's all !
you can even do the thing A-SYNC but you have to call reconnect after the connection with the STA is established (or not and give up) to get the AP working again. a few yield(); around there never hurts so

WiFi.begin();
While(WiFi.status!=WL_CONNECTED) {
  Serial.print(".");
  yield();
}
WiFi.reconnect();

within setup() (or a function called in setup() or even for switching to a different network)

Juraj:
why not use WiFiManager library?

Once the WiFiManager is connected i never seem to be able to use the AP to access my pages i can only do so using the network. And then there are the layout issues.

Thanks will try putting yield and WiFi.reconnect,...at least my device should not be reset repeatedly after using this..If that happens then my code is ok.

Regards
Uday

The repeated reset is probably caused by the wdt in all the examples the Serial.print("."); is always followed by delay(x): which also incurs a yield() and resets the wdt.

udaym:
Hi,
Trying to onfigure a WiFi connection through webpage.
So I initially configure an AP.
Then open a webpage for entering WiFi ssid .

Issue is that whenever I call WiFi.begin my webpage does not open as it hangs in following while loop after

WiFi.begin();
While(WiFi.status!=WL_CONNECTED)
{Serial.print(".");}

webServer.begin(); is not reached,hence although AP is configured webpage is not loaded.

I want a solution so that my webpage gets loaded and whenever I enter WiFi ssid wifi gets configured.

Moving webServer.begin() before WiFi.begin() does not help either.
Moving WiFi.begin() into loop() resets the device frequently.
Kindly provide inputs.
Regards
Uday

why do you call WiFi.begin() if you don't have the parameters? WiFi.begins joins a WiFi network in station role.. WiFi.status() checks the connection state of the station, not of the SoftAP.

start the AP, start the server, get the parameters and the call WiFi.begin with the parameters

Exactly…I am trying to get parameters from user through webpage which is sent on AP…afterwards I do wifi scanning …here it resets when parameters are empty.an exception is thrown…yield() should handle this I guess…then WiFi.reconnect() will probably try to reconnect when user enters parameters on webpage.

udaym:
Exactly…I am trying to get parameters from user through webpage which is sent on AP…afterwards I do wifi scanning …here it resets when parameters are empty.an exception is thrown…yield() should handle this I guess…then WiFi.reconnect() will probably try to reconnect when user enters parameters on webpage.

Ah sorry yeah overlooked that, actually thought you would have parameters in the actual program.
The proper sequence of events is

WiFi.softAP(apname,appass);
// give users time to connect and enter details
WiFi.begin(staname,stapass); // this actually causes the AP to disconnect
Serial.print("Connecting ");
uint32_t moment=millis();
while ((WiFi.status()!=WL_CONNECTED) && (millis()-moment<8000)) {  // make sure to do a timeout in
                                                               // case of incorrect details to prevent eternal loop
  Serial.print(".");
  delay(100);  // or yield();  (delay() includes a yield();)
}
Serial.println();
if (WiFi.status()!=WL_CONNECTED) {
  Serial.print("Connected to network: ");
  Serial.println(staname);
}
else Serial.println("Connecting Failed");
WiFi.reconnect(); // this reconnects the AP so the user can stay on the page

This is the method i use (or an a-sync version of it)

Since i submit a form but have to wait for connection and re-connection i tried to get the page to do an automatic refresh after the timeout with the HTML, but alas that i never got to work, so if you manage let me know.

Ok.

WiFi.mode(AP_STA)
For me calling
WiFi.begin(staname,stapass);
in setup() although device doesn't reset but
program hangs in while loop of WiFi.status.

calling it in loop() I get AP webpage for entering user parameters but device resets repeatedly.as suggestions above yield() can help.yet to try it.

udaym:
although device doesn't reset but
program hangs in while loop of WiFi.status.

if you don't timeout the connection attempt there is no exit of the loop in case of incorrect credentials.
Anyway, you have my method which works.

I just finished the ConfigurationAP example for my new WiFi library over AT commands.
It is a very basic minimal Configuration AP.

I ported it to esp8266 core for you.

#include <ESP8266WiFi.h>

void setup() {

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

  //WiFi.disconnect(); // forget the persistent connection to test the Configuration AP

  // 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 ");
  Serial.print(WiFi.softAPSSID());
  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>
connecting...</body></html>"));
        client.stop();

        Serial.println();
        Serial.print("Attempting to connect to WPA SSID: ");
        Serial.println(ssid);
        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>
"));

        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.

"));
          client.println(F("SSID:
<input type='text' name='i'>
"));
          client.println(F("Password:
<input type='password' name='p'>

"));
          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);
        }
      }
    }
  }
}

This looks fine… actually the other way around…configAP in loop()…and WiFi.begin in setup()… should work.

Thanks…
Uday