Reconfigure WIFI when ESP32 restarts

PoojaVernekar
Posts: 3
Joined: Thu Jul 08, 2021 6:19 am

Reconfigure WIFI when ESP32 restarts

Postby PoojaVernekar » Tue Nov 30, 2021 4:21 am

I am using ESP32-> WIFI-> Smart_Config example code.
Smart config is working fine able to Config ESP32 dev kit with WIFI through ESPTouch Application via mobile.

Issue is when ESP32 restarts, need to again configure WIFI to ESP32 dev kit.

My requirement is once ESP32 is configured with WIFI it should be fixed, either asking every time when restarts. And if it is not smart configured then connect to default WIFI configure available in menuconfig.

Please suggest what changes can make to code. Have attached WIFI part of code.

static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
/* This is a workaround as ESP platform WiFi libs don't currently
auto-reassociate. */
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
}
else if (event_base == SC_EVENT && event_id == SC_EVENT_SCAN_DONE) {
ESP_LOGI(TAG, "Scan done");
} else if (event_base == SC_EVENT && event_id == SC_EVENT_FOUND_CHANNEL) {
ESP_LOGI(TAG, "Found channel");
} else if (event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD) {
ESP_LOGI(TAG, "Got SSID and password");

smartconfig_event_got_ssid_pswd_t *evt = (smartconfig_event_got_ssid_pswd_t *)event_data;
wifi_config_t wifi_config;
uint8_t ssid[33] = { 0 };
uint8_t password[65] = { 0 };

bzero(&wifi_config, sizeof(wifi_config_t));
memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid));
memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password));
wifi_config.sta.bssid_set = evt->bssid_set;
if (wifi_config.sta.bssid_set == true) {
memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid));
}

memcpy(ssid, evt->ssid, sizeof(evt->ssid));
memcpy(password, evt->password, sizeof(evt->password));
ESP_LOGI(TAG, "SSID:%s", ssid);
ESP_LOGI(TAG, "PASSWORD:%s", password);

ESP_ERROR_CHECK( esp_wifi_disconnect() );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_connect() );
} else if (event_base == SC_EVENT && event_id == SC_EVENT_SEND_ACK_DONE) {
xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT);
}
}

static void initialise_wifi(void)
{
#ifdef CONFIG_IDF_TARGET_ESP8266 || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0))
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
#else
ESP_ERROR_CHECK( esp_netif_init() );
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_event_loop_create_default() );
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
assert(sta_netif);
#endif

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
wifi_config_t wifi_config = {
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PASSWORD,
},
};
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);

#ifdef CONFIG_IDF_TARGET_ESP8266 || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0))
#else
ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) );
ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
ESP_ERROR_CHECK( esp_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) );
#endif

ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
esp_err_t ret = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA,
GATEWAY_ID);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "failed to set hostname:%d", ret);
}
}

static void smartconfig_task(void * parm)
{
EventBits_t uxBits;
ESP_ERROR_CHECK( esp_smartconfig_set_type(SC_TYPE_ESPTOUCH) );
smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_smartconfig_start(&cfg) );
while (1) {
uxBits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT | ESPTOUCH_DONE_BIT, true, false, portMAX_DELAY);
if(uxBits & CONNECTED_BIT) {
ESP_LOGI(TAG, "WiFi Connected to ap");
printf("SMART CONFIG STARTED");
}
if(uxBits & ESPTOUCH_DONE_BIT) {
ESP_LOGI(TAG, "smartconfig over");
esp_smartconfig_stop();
printf("SMART CONFIG STOPPED");
vTaskDelete(NULL);
}
}
}

karunt
Posts: 76
Joined: Sat Apr 03, 2021 7:58 am

Re: Reconfigure WIFI when ESP32 restarts

Postby karunt » Wed Dec 01, 2021 9:52 am

I haven't familiarized myself with the smart provisioning code, but there is a BLE wifi provisioning code (separate from smart provisioning) which uses a wifi provisioning manager. Example found in examples/provisioning/wifi_prov_mgr, which seems to achieve what you're looking for. Once a chip has been successfully provisioned (wifi credentials are saved in nvs flash), if you try running the provisioning code again, the chip will tell you it's already been provisioned and will terminate the code. Worth a shot to see if it works for you.

User avatar
mbratch
Posts: 298
Joined: Fri Jun 11, 2021 1:51 pm

Re: Reconfigure WIFI when ESP32 restarts

Postby mbratch » Fri Dec 03, 2021 2:43 am

When smartconfig completes successfully, the ESP-IDF Wifi API saves the credential information in NVS. Whenever you boot, just initialize the wifi and, before running the smartconfig, check for wifi events in your event handler to see if you get a connection. If you do, then it connected with the prior credentials. If it fails to connect within a period of time (say, a few seconds), you can redo the smartconfig process. I have mine written so that if I press and hold a particular button for a couple of seconds it will erase the prior credentials and force the smartconfig to run again.

Who is online

Users browsing this forum: Google [Bot], leschge, Lvalue, mikecarlos and 128 guests