0

What I am trying to do is while connected locally in the esp8266 and give it a ssid and password, save them in a global variable and use them to start a connection inside loop function with a flag so it happens only once and let the program run while I check when the connection is complete. Do you foresee any problems with this application?:

setup(){
    wifi.mode(WIFI_AP_STA);
    setup softAP
    server.on("/",handleroot)
    server.begin()
}
loop(){
    server.handleClient()
    if(credential_submited){
        Wifi.begin(credentials);
        flag_connection_started =true;
        credential_submited = false;
        flag_for_internet_server=true;
    }
    if(WiFi.status()==WL_CONNECTED && flag_for_internet_server==true){
        server.on("/",handleroot)
        server.begin()
        flag_for_internet_server=false;
    }

I know there is also WifiManager But I am trying to do it on my own. The thing is some wifi networks take a lot of time to connect. More precisely they connect but then the esp doesn't get an ip and disconnects. Thank you for your time

4
  • Is the esp in access point or in station mode? Probably it's possible, but I don't really understand the scenario. You could make it clearer by at least providing some pseudo code
    – Sim Son
    Sep 24, 2019 at 13:57
  • 1
    Don't see problems there, since this is somewhat standard. Have you tried anything?
    – chrisl
    Sep 24, 2019 at 14:04
  • Take a look at github.com/tzapu/WiFiManager it does exactly what you want.
    – Dougie
    Sep 24, 2019 at 19:40
  • @Dougie I am trying to create it on my own to learn mostly. Sep 25, 2019 at 8:33

2 Answers 2

0

WiFi.begin() in ESP8266WiFi library doesn't wait until the connection completes, it returns immediately.

You can check if the connection to WiFi network is established using WiFi.status() as most of the examples show. And it is not mandatory to wait for the connection in setup().

BTW: You can use a separate sketch to set the SSID and password in esp8266 and then in all other sketches not use WiFi.begin(), only wait for the connection to WiFi network to finish.

2
  • Could you elaborate on the separate sketch part. You mean that I can upload two sketches on the eps and run one at the time and when the one is finished run the second program? Sep 25, 2019 at 8:42
  • no. you run the configuration sketch and the esp8266 remembers the ssid and password. any other sketch uploaded after that will use this settings. the esp8266 will connect automatically even if you upload Blink
    – Juraj
    Sep 25, 2019 at 11:26
-1

This is not a big deal...

You can do it vary easily, the most simple way is that you can use EEPROM to achieve this..

Simply pass the SSID and PASSWORD of new network to ESP8266 and save it in EEPROM and also create a new variable in eeprom that store the status that new network credentials is entered or not and check it in loop and then fetch new network credentials from the EEPROM and try to connect to it...

You can also check and connect to your desired network from the loop...

Or alternatively you can use the library that can help you in doing that [WiFiManager]: https://github.com/tzapu/WiFiManager

1
  • ESP8266 doesn't have an EEPROM but stores the SSID and password in flash automatically if you don't disable this feature. that is what WiFiManager utilizes
    – Juraj
    Sep 25, 2019 at 5:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.