logo elektroda
logo elektroda
X
logo elektroda

How do I set the ESP8266 to AP mode for WiFi network selection on first startup?

Rigardo5033 2481 14
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18029793
    Rigardo5033
    Level 6  
    Hello, I have a problem. I have created my own weather station with a temperature sensor and a particulate matter sensor, the data is sent to a ThingSpeak server and to a web server where the data is displayed. But I am looking for a way to make the module work in AP mode when I first start it up and to be able to select the WiFi network. Unfortunately the tutorials and examples from the internet are very complicated, I need the simplest one that exists. I used this guide:


    .

    Everything here works but I am having a hard time linking it to my application. Maybe someone can explain it to me "in a spatial manner".

    Greetings
  • ADVERTISEMENT
  • #2 18029811
    khoam
    Level 42  
    Rigardo5033 wrote:
    Everything here works but I am having a hard time connecting it to my application.
    .
    What does the code for this application look like, or at least the bit where this network selection is supposed to happen?

    Rigardo5033 wrote:
    But I'm looking for a way to make the module work in AP mode the first time it is started and there is the possibility to select a WiFi network.
    .
    If one is to perform a scan of the available WiFi networks then the module cannot operate in AP mode, but in STA mode.
  • #3 18029826
    Rigardo5033
    Level 6  
    This is how I would like the network scan to be performed. I would like the selection to be at the beginning and then my page to be done. I know it's a mess out there, I'm a beginner. Please advise

    Code: C / C++
    Log in, to see the code
    .
  • Helpful post
    #4 18029861
    khoam
    Level 42  
    The easiest way is to use the code in the WiFiScan example. The scan obviously needs to be performed before WiFi.config() in setup().
    Link: https://github.com/esp8266/Arduino/blob/maste...es/ESP8266WiFi/examples/WiFiScan/WiFiScan.ino
    It is best to put the scan and the selection of the appropriate network in a separate function that will be called once in setup().

    Added after 6 [minutes]: .

    Detailed documentation of the WiFi.scanNetworks() function is here:
    https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/scan-class.html
  • ADVERTISEMENT
  • #5 18029965
    Rigardo5033
    Level 6  
    I will try to play with this feature tomorrow. Thanks for the advice tomorrow I will share what I manage to create

    Added after 10 [hours] 36 [minutes]:

    I have reviewed this scan function, and it is great only I have one question. I search for a network and it finds me a network xxx I want to connect to it and after connecting it starts the web server. And how to do a simple network selection and password.
  • Helpful post
    #6 18030457
    khoam
    Level 42  
    WiFi.scanNetworks() returns the number of WiFi networks detected.

    WiFi.SSID(networkItem) returns the SSID (as a text string), where networkItem is the index of the network (from 0 to N-1); N is the number of networks detected by WiFi.scanNetworks()

    Passwords for individual networks are something you will have to store and select yourself depending on the SSID.

    You have everything nicely described in the documentation about which I provided a link in post #4 and additionally here:
    https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/scan-examples.html
  • Helpful post
    #7 18030459
    kaczakat
    Level 34  
    Here is a tutorial on how to use the WIFI MANAGER library for ESP Link .
    Alternatively there is the FSBrowser NG project Link . The HTML local page is saved in regular HTML files. You need to have a file system upload plugin added to the ESP, this makes sense for flash larger than 1MB. Example you compile for FS (SPIFFS) at least 128kB (no larger than the contents of the DATA directory), upload firmware, upload web page files separately:
    How do I set the ESP8266 to AP mode for WiFi network selection on first startup? .
    Within the example is a set of sample files, if you save the example AS, there is a DATA folder added in its directory, inside are sample HTML files - WIFI configuration, adding a password, updating firmware from a browser, etc. bits and pieces:
    How do I set the ESP8266 to AP mode for WiFi network selection on first startup? .
    The first time you start up, you connect to the module set to AP+ST mode, go to 192.168.4.1/admin, then from the network settings menu and select the network parameters as if you were using a smartphone:
    How do I set the ESP8266 to AP mode for WiFi network selection on first startup? .
    Edit: 192.168.4.1 of course.
    Helpful post? Buy me a coffee.
  • Helpful post
    #8 18030484
    khoam
    Level 42  
    If you will want to use the WiFiManager library then I strongly recommend the description and examples that can be found on the author's page of this project:
    https://github.com/tzapu/WiFiManager

    What specific model of ESP8266 do you have?

    Added after 6 [minutes]:

    kaczakat wrote:
    Possible is the FSBrowser NG project
    .
    This project is unfortunately no longer being developed and has some bugs.
  • ADVERTISEMENT
  • #9 18030664
    Rigardo5033
    Level 6  
    Esp8266 nodemcu v3 . I'll be back from work trying to get something working.

    Added after 6 [hours] 28 [minutes]: .

    I succeeded with the help of WiFi manager. Only I have 2 problems still :) Well, in the program code in setup() I use the following settings:
    ..
    Code: C / C++
    Log in, to see the code
    .

    And unfortunately after every unplugging it loses the given network. How do I solve this? The second thing I would like to do is to impose a fixed ip on the device after connecting to the network, e.g. : 192.168.1.10 , I seem to have this defined at the beginning but it takes a different ip:

    Code: C / C++
    Log in, to see the code
    .

    Please advise and thank you already for any help.
  • ADVERTISEMENT
  • #11 18031445
    Rigardo5033
    Level 6  
    But if it is not there then automatically when I leave the "known network" then my device will be the AP? Do I understand this correctly?
  • #12 18031458
    kaczakat
    Level 34  
    In the example it is commented out, this is used to reset the settings. If, for example, you change the wifi password and the program goes dumb, or for some other reason you want to connect to another network you can assign this function to a button - e.g. you press the button after IO13 for 5s, as a reaction you call the function wifiManager.resetSettings(); and reset the WIFI settings - it starts up again as an AP and no network is entered.
    As you've added it to setup it does a cleanup of your WIFI settings after each reset, as expected.
    And you can let go of those manual settings for IP, Gate, etc., from the library description it appears that this is configured and stored in EEPROM (emulated).
    Helpful post? Buy me a coffee.
  • #13 18031467
    Rigardo5033
    Level 6  
    Ok. But how can I no longer connect the button? Any idea how to use this function?
  • #14 18031516
    kaczakat
    Level 34  
    If it doesn't find the network or you change the password it starts up in AP mode and you can log in from your smartphone to change the SSID and password. That's probably enough for you, but there are many other options, I haven't tested them, there are quite a few descriptions in the examples and on the library page.
    Helpful post? Buy me a coffee.
  • #15 18031544
    khoam
    Level 42  
    Rigardo5033 wrote:
    And unfortunately every time it is unplugged it loses the given network.
    .
    This is described in this example, using the setBreakAfterConfig() function:
    https://github.com/tzapu/WiFiManager/blob/mas...AutoConnectWithReset/AutoConnectWithReset.ino

    Rigardo5033 wrote:
    The second thing I would like to do is to impose a fixed ip on the device after connecting to the network, e.g. : 192.168.1.10
    .
    This is described in this example, using the setSTAStaticIPConfig() function:
    https://github.com/tzapu/WiFiManager/blob/mas...nnectWithStaticIP/AutoConnectWithStaticIP.ino

    Actually you should not use resetSettings() - it is not needed for anything in this case.

Topic summary

The discussion revolves around configuring the ESP8266 to operate in Access Point (AP) mode for WiFi network selection during initial startup. The user seeks a straightforward method to implement this functionality for their weather station project, which utilizes sensors and uploads data to ThingSpeak. Responses suggest using the WiFiScan example code to perform network scanning before configuring WiFi settings. The WiFiManager library is recommended for simplifying network selection and management, allowing the device to revert to AP mode if it cannot connect to a known network. Issues regarding static IP assignment and the use of reset settings are also addressed, with advice on how to manage these configurations effectively.
Summary generated by the language model.
ADVERTISEMENT