Set ESP32 WiFi Connection Without Re-programming

by Lan_Makerfabs in Circuits > Arduino

19474 Views, 17 Favorites, 0 Comments

Set ESP32 WiFi Connection Without Re-programming

1132.jpg

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

Problem I Met

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.

How I Solve It

erw.jpg
wer.jpg

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.

//WIFI
const char *ssid = "Makerfabs";
const char *password = "20160704";<br>

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

  1. Set the working mode of ESP32 to AP mode that the phone connects it.
  2. 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.

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.

Program the Firmware of the ESP32

  • 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:
        int runtime = millis();
        int starttime = runtime;
        while ((runtime - starttime) < 3000)
        {
            if (digitalRead(WIFI_SET_PIN) == LOW)
            {
             ......
   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);
#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.

Operation

dgf.jpg
wifi112.jpg
  • 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.

Result

wifi113.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.

Finally

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.

Originally published at https://www.makerfabs.cc/article/how-to-connect-esp32-to-wifi-without-programming.html