Using ESP in AP and STA mode using wifi manager | ESP| WIFI| Part-3

Using ESP with Wifi manager

In the last two tutorials, we have seen usage of AP mode and STA mode. In this tutorial we will see both AP and STA being used in conjunction. We have hard coded SSID and password in STA mode earlier, now we will see a way to select among multiple SSIDs and switch between them and have a wifi network setup in AP.

We will use WifiManager to manager multiple SSIDs. Here how it will work.
1. Wifi Manager will attempt to connect with any know SSID and password combination. If it doesnt find a connection.
2. Wifi manager will setup an Access point and any device with a browser can connect to it at default 196.168.4.1 and then select SSID from scaned networks.
3. Once SSID is selected. ESP will be set to STA mode up on successful connection. This SSID will be stored for future connection attempts. 
4. Up on restart if the stored SSID cannot be connected, ESP will be set to AP mode and process repeats.

Wifi manager is useful to setup devices for the first time. Smart plug, smart washing machine etc. Most smart devices use customised version of wifi manager to provide this capability to users.

Now lets see a simple example on how to use wifi manager in your applications to manage networks.

First install library, Go to Sketch -> Include Library -> Manage Library and select any version of tzapu wifimanager.


Once installed, Go to File -> Examples -> WifiManager -> Basic


#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager


void setup() {
    // WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
    // it is a good practice to make sure your code sets wifi mode how you want it.

    // put your setup code here, to run once:
    Serial.begin(115200);
    
    //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
    WiFiManager wm;

    // reset settings - wipe stored credentials for testing
    // these are stored by the esp library
    // wm.resetSettings();

    // Automatically connect using saved credentials,
    // if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
    // if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
    // then goes into a blocking loop awaiting configuration and will return success result

    bool res;
    // res = wm.autoConnect(); // auto generated AP name from chipid
    // res = wm.autoConnect("AutoConnectAP"); // anonymous ap
    res = wm.autoConnect("AutoConnectAP","sensorism"); // password protected ap

    if(!res) {
        Serial.println("Failed to connect");
        // ESP.restart();
    } 
    else {
        //if you get here you have connected to the WiFi    
        Serial.println("connected...yeey :)");
    }

}

void loop() {
    Serial.println(WiFi.localIP());
    delay(5000);
}

Compile and upload. Use tutorial for uploading.

Now open any device and connect to Wifi with SSID "AutoConnectAP" using password "sensorism".
A pop up window will appear on device at 192.168.4.1. If it doesnt appear, please go to any browser and enter the url. http://192.168.4.1

Now ESP will attempt to scan all wifi networks and provide you the list.
Open Tools -> Serial Monitor


On the device, select the SSID and provide password to connect. Till this point ESP is on AP mode. 
Once submitted, ESP will reset and use the provided details to connect to SSID by setting up ESP in STA mode. If connection succeeds, ESP will continue else it will fall back to AP mode again for configuration.





See Serial output to know if the connection to SSID is established.



Now that you are aware of all wifi modes of ESP. Now lets learn about ESP 8266 and how to interface sensors with it.

Comments

Popular posts from this blog

Microphone Sound Sensor explained | HW-484 | Interfacing with ESP 8266

Using ESP 01 in Access Point (AP) mode to control ESP relay switch | ESP wifi tutorial | Part - 1

[Experiment] DIY Robotic 2 wheel drive car using L298N motor driver and NodeMCU | ESP 8266