Skip to content

Commit 9f4eb5c

Browse files
committed
feat(esp_wifi): Add example to write wifi config in nvs
This commit adds example for using wifi station and softap by flashing wifi config directly into nvs using NVS Partition generator Utility (using csv file). Closes #14554
1 parent 45f495c commit 9f4eb5c

File tree

8 files changed

+398
-0
lines changed

8 files changed

+398
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# The following five lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.16)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
# "Trim" the build. Include the minimal set of components, main, and anything it depends on.
7+
idf_build_set_property(MINIMAL_BUILD ON)
8+
project(wifi_nvs_config)

examples/wifi/wifi_nvs_config/README.md

Lines changed: 217 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
idf_component_register(SRCS "wifi_nvs_config_main.c"
2+
INCLUDE_DIRS "."
3+
PRIV_REQUIRES esp_wifi nvs_flash)
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
/* WiFi example
7+
8+
This example code is in the Public Domain (or CC0 licensed, at your option.)
9+
10+
Unless required by applicable law or agreed to in writing, this
11+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+
CONDITIONS OF ANY KIND, either express or implied.
13+
*/
14+
#include <string.h>
15+
#include "freertos/FreeRTOS.h"
16+
#include "freertos/task.h"
17+
#include "freertos/event_groups.h"
18+
#include "esp_system.h"
19+
#include "esp_wifi.h"
20+
#include "esp_event.h"
21+
#include "esp_log.h"
22+
#include "nvs_flash.h"
23+
24+
#include "lwip/err.h"
25+
#include "lwip/sys.h"
26+
27+
/* The examples use WiFi configuration that you can set via flashing nvs partition */
28+
29+
/* FreeRTOS event group to signal when we are connected*/
30+
static EventGroupHandle_t s_wifi_event_group;
31+
32+
/* The event group allows multiple bits for each event, but we only care about two events:
33+
* - we are connected to the AP with an IP
34+
* - we failed to connect after the maximum amount of retries */
35+
#define WIFI_CONNECTED_BIT BIT0
36+
#define WIFI_FAIL_BIT BIT1
37+
38+
#define MAXIMUM_RETRY_COUNT 5
39+
40+
static const char *TAG = "wifi_nvs_config";
41+
42+
static int s_retry_num = 0;
43+
44+
45+
static void event_handler(void* arg, esp_event_base_t event_base,
46+
int32_t event_id, void* event_data)
47+
{
48+
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_START) {
49+
ESP_LOGI(TAG, "SoftAP started successfully!");
50+
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) {
51+
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
52+
ESP_LOGI(TAG, "station join, AID=%d", event->aid);
53+
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED) {
54+
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
55+
ESP_LOGI(TAG, "station leave, AID=%d, reason=%d", event->aid, event->reason);
56+
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
57+
esp_wifi_connect();
58+
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
59+
if (s_retry_num < MAXIMUM_RETRY_COUNT) {
60+
esp_wifi_connect();
61+
s_retry_num++;
62+
ESP_LOGI(TAG, "retry to connect to the AP");
63+
} else {
64+
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
65+
}
66+
ESP_LOGI(TAG,"connect to the AP fail");
67+
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
68+
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
69+
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
70+
s_retry_num = 0;
71+
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
72+
}
73+
}
74+
75+
void wifi_init(void)
76+
{
77+
s_wifi_event_group = xEventGroupCreate();
78+
79+
ESP_ERROR_CHECK(esp_netif_init());
80+
81+
ESP_ERROR_CHECK(esp_event_loop_create_default());
82+
83+
esp_netif_create_default_wifi_sta();
84+
esp_netif_create_default_wifi_ap();
85+
86+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
87+
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
88+
89+
esp_event_handler_instance_t instance_any_id;
90+
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
91+
ESP_EVENT_ANY_ID,
92+
&event_handler,
93+
NULL,
94+
&instance_any_id));
95+
esp_event_handler_instance_t instance_got_ip;
96+
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
97+
IP_EVENT_STA_GOT_IP,
98+
&event_handler,
99+
NULL,
100+
&instance_got_ip));
101+
102+
ESP_ERROR_CHECK(esp_wifi_start() );
103+
104+
ESP_LOGI(TAG, "wifi_init finished.");
105+
106+
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
107+
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
108+
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
109+
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
110+
pdFALSE,
111+
pdFALSE,
112+
portMAX_DELAY);
113+
114+
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
115+
* happened. */
116+
if (bits & WIFI_CONNECTED_BIT) {
117+
ESP_LOGI(TAG, "Successfully connected to AP");
118+
} else if (bits & WIFI_FAIL_BIT) {
119+
ESP_LOGI(TAG, "Failed to connect to AP");
120+
} else {
121+
ESP_LOGE(TAG, "UNEXPECTED EVENT");
122+
}
123+
}
124+
125+
void app_main(void)
126+
{
127+
//Initialize NVS
128+
esp_err_t ret = nvs_flash_init();
129+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
130+
ESP_ERROR_CHECK(nvs_flash_erase());
131+
ret = nvs_flash_init();
132+
}
133+
ESP_ERROR_CHECK(ret);
134+
135+
wifi_init();
136+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
key,type,encoding,value
2+
nvs.net80211,namespace,,
3+
opmode,data,u8,2
4+
ap.ssid,data,blob_sz_fill(32;0x00),myssid
5+
ap.passwd,data,blob_fill(64;0x00),mypassword
6+
ap.authmode,data,u8,3
7+
ap.max.conn,data,u8,3
8+
ap.pmf_r,data,u8,0
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
key,type,encoding,value
2+
nvs.net80211,namespace,,
3+
opmode,data,u8,1
4+
sta.ssid,data,blob_sz_fill(32;0x00),myssid
5+
sta.pswd,data,blob_fill(64;0x00),mypassword
6+
bssid.set,data,u8,0
7+
sta.lis_intval,data,u16,3
8+
sta.scan_method,data,u8,1
9+
sta.sort_method,data,u8,0
10+
sta.pmf_r,data,u8,0
11+
sta.btm_e,data,u8,0
12+
sta.rrm_e,data,u8,0
13+
sta.mbo_e,data,u8,0
14+
sta.ft,data,u8,0
15+
sta.trans_d,data,u8,0
16+
sta.sae_h2e,data,u8,0
17+
sta.sae_pk_mode,data,u8,0
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Name, Type, SubType, Offset, Size, Flags
2+
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
3+
# Name, Type, SubType, Offset, Size, Flags
4+
nvs, data, nvs, 0x9000, 0x6000,
5+
phy_init, data, phy, 0xf000, 0x1000,
6+
factory, app, factory, 0x10000, 1M,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_PARTITION_TABLE_CUSTOM=y
2+
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions_example.csv"
3+
CONFIG_PARTITION_TABLE_FILENAME="partitions_example.csv"

0 commit comments

Comments
 (0)