2017/06/15

ESP8266 - WiFi Manager

This is a brief summary of my experience working with WiFi Manager developed by tzapu for ESP8266. A big thank you to tzapu for this amazing work. The original version of the code can be found at https://github.com/tzapu/WiFiManager/blob/master/examples/AutoConnect/AutoConnect.ino

The instruction for getting WiFiManager ready for Arduino IDE can be found at https://github.com/tzapu/WiFiManager.


The code below is based on the sample code mentioned above with the parts highlighted in yellow added to allow user to manually clear the wifi credential saved so that the ESP module could be connected to other wireless AP.

The Schematic

I use the ESP-01 programming jig that I developed as the carrier board for this experiment. GPIO0 of ESP-01 is connected to the FLASH button. RST of ESP-01 is connected to the RESET button. There is a connector for external 3.3V power supply which is needed when the WiFi of ESP-01 is turned on (note, the power from the 3v3 output of CP2102 is not enough to drive ESP-01 when its WiFi is on).


The Arduino Sketch

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino

//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager

int wifi_reset = 0;        // Use GPIO 0 to reset WiFi
int val = 0;               // variable to store the read value

void setup() {
    // put your setup code here, to run once:
    Serial.begin(115200);
    
    pinMode(wifi_reset, INPUT);  // set pin to input

    //WiFiManager
    //Local intialization. Once its business is done, there is no need to keep it around
    WiFiManager wifiManager;
    //reset saved settings
    //wifiManager.resetSettings();
    
    //set custom ip for portal
    //wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));

    //fetches ssid and pass from eeprom and tries to connect
    //if it does not connect it starts an access point with the specified name
    //here  "AutoConnectAP"
    //and goes into a blocking loop awaiting configuration
    wifiManager.autoConnect("AutoConnectAP");
    //or use this for auto generated name ESP + ChipID
    //wifiManager.autoConnect();

    
    //if you get here you have connected to the WiFi
    Serial.println("connected...yeey :)");
}

void loop() {
    // put your main code here, to run repeatedly:
    val = digitalRead(wifi_reset);   // read the input pin
    if (val == LOW) {
      WiFi.disconnect();
     delay(1000);
    }
}

Procedure for connecting ESP-01 to the AP

1. Compile and load the sketch to the flash memory of ESP-01;

2. Launch the WiFi utility of your phone to view a list of available wireless AP. In the photo below, "AutoConnectAP" is the one from ESP-01.


3. Click on AutoConnectAP to connect to it.


4. Launch a browser and type in 192.168.4.1 in the URL field to connect to AutoConnectAP.

5. Click on Configure WiFi. Click on the SSID of the AP to connect and enter the password.

6. After the ESP module is successfully connected to the AP, the AutoConnectAP disappears (because the ESP module is switched from AP mode to Station mode).


7. launch the AP's management UI to check for the IP assigned to the ESP module. In the example below, the IP assigned by the AP to the ESP module is 192.168.2.8 and we could use ping to verify it.


Procedure for clearing the credential stored in ESP-01 for making a new connection

1. Press down the FLASH button for 1 ~ 2 second then release it;

2. Press down the RESET button for 1 ~ 2 second then release it;

3. The module is now ready to be connected to another AP.

Note, after the ESP module is connected to an AP, as long as the FLASH button (which is connected to GPIO0 and GPIO0 is used to reset wifi in the above sketch) is not pressed, the ESP module will keep trying to connect to the same AP after a power loss recovery or after the RESET button is pressed. 

References:

ESP8266 WiFi Connection manager with web captive portal
https://github.com/tzapu/WiFiManager

esp8266: Yet another esp WiFi config to eeprom..
https://nobugsjustfeatures.wordpress.com/2016/03/27/esp8266-yet-another-esp-wifi-config-to-eeprom/

[TMT] - Avoid Hard-Coding WiFi Credentials On Your ESP8266 Using WiFiManager
https://youtu.be/A-P20vC7zq4

how to erase previous credentials?
https://github.com/tzapu/WiFiManager/issues/142

No comments:

Post a Comment