A method that sets or changes ESP32 WiFi connection without re-programming.

This tutorial shows how to set the ESP32 WiFi connection secondly without re-programing.

In the past, I used to program ESP32 for setting or changing the WiFi. Once the WiFi setting needs to be changed, I will need to re-program the ESP32. When I show my ESP32 project to my audience, the ESP32 was not working because of the WiFi changed, and I must reset the ESP32 by re-programing, troublesome!~

So I want to set or change the ESP32 WiFi connection without re-programming that I have downloaded the program once.

After my research and testing, I found two problems in WiFi Setting without re-programming: how to transmit the WiFi name and password to ESP32; and how to store the information in ESP32.

Transmit wifi setting information

ESP32 has to get the WiFi name and password for connecting to WiFi. In the past, the WiFi information can be in code and programmed to ESP32.

2311.jpg

In the new method, the WiFi information can be transmitted from the phone to ESP32.

  • Set the working mode of ESP32 to AP mode that the phone connects it.
  • Set ESP32 to establish the server that the phone can log in to input the WiFi information.

so the ESP32 can obtain the information from the server.

erw.jpg

Store setting information

To Keep the WiFi info, the WiFi setting information needs to be store without being affected by the restart of the ESP32. Non-volatile storage (NVS) library is designed to store key-value pairs in flash. The information would be store in the NVS as the key. Now, ESP32 can be connected to the WiFi with the information read from the flash when it re-start.

wer.jpg
  • Check the digital pin for setting WiFi. If the setting pin level is low in three seconds, Set the esp32 to work in AP mode:
        void ap_init()
{
//WiFi.softAP(ssid, password);
WiFi.softAP("Makerfabs_ap");
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
}
    
  • Initiate the server.
        int wifi_config_server()
{
WiFiClient client = server.available(); // listen for incoming clients
if (client) // if you get a client,
……
    
  • If the server responds, the WiFi information can be obtained from the URL of the response.
        set_wifi_from_url(get_request);
    
  • Store the information to ESP32 flash.
        void record_wifi(char *ssid, char *password)
{
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
……
    
  • Check the data and initiate the WiFi Connection.
        check_wifi(ssid, password);
WiFi.begin(ssid, password);
    
  • The program has been completed with Arduino. And it is available for the need and can be obtained from https://github.com/Makerfabs/Makerfabs_FAQ/tree/master/Arduino_ESP32/arduino_example/wifi_set_demo. The program has been packaged to a library for ease of use. The using demo is as the following:
        #include "wifi_save.h"
void setup()
{
Serial.begin(115200);
if(wifi_set_main())
{
Serial.println("Connect WIFI SUCCESS");
}
else
{
Serial.println("Connect WIFI FAULT");
}
}
void loop()
{
Serial.println("loop");
delay(3000);
}
    
  • Open the program by the Arduino ide, upload it to the ESP32 board.
  • Reset the ESP32 board and put the pin(IO21) level to low for starting the WiFi connection.
  • Use the phone to connect the WiFi “makerfabs-ap” build by the esp32.
  • Enter the address “192.168.4.1” to log in to the server in the browser. (notice: because of the technical difficulty, the chrome app is recommended to use and the other browsers maybe be not working.)
  • Enter your WiFi information on the browser page.
  • The ESP32 board connects to the WiFi after seconds.
  • If you want to change the WiFi connected, restart the ESP32 and put the IO21 level to low in three minutes. The esp32 will reset the AP mode. You can follow the above steps to change the new WiFi.
wifi112.jpg

You can view the result of the WiFi connection in the serial monitor window when the ESP32 board connected to the PC, also can check the result by the router.

wifi113.jpg

Just after downloading the program, ESP32 can be connected or changed the WiFi without re-uploading the program, which is very convenient for people who have little or no Arduino and programming knowledge to set wifi connection secondly. It is suitable to use in the ESP32 project which will be shared with friends that need to connect to different WiFi.

Tags