How to Give Each ESP32 Prototype Its Own Wi-Fi Setup Password
The usual ESP32 prototype has a setup access point with a password like 12345678, compiled into the firmware. Every board shares it, so anyone who learns it from one board (or from your repository) can join all of them.
The fix is small: each board makes its own random password on first boot, stores it, and tells you once so you can write it on a label. This guide uses ESP-IDF and its SoftAP mode.
Don't derive the password from the MAC address
A password computed from the MAC is not a secret. The access point broadcasts its BSSID, which is its MAC address, to everyone in range.
Quick detour: hashing the MAC is tempting, because the result looks random and needs no storage. But if the recipe leaks, anyone can run it on the address your board is announcing. Mixing in a firmware-wide secret only moves the problem: dump one board and you have the secret for the lot.
The MAC is still handy for the network name, which is public anyway.
Give the password its own partition
Keep the password away from the settings you wipe. Prototype code often erases the default nvs partition during a factory reset or after an NVS initialisation error. If the password lives there, it changes and your label is now lying.
Add a second NVS partition, here called fctry, in partitions.csv:
# Name, Type, SubType, Offset, Size
nvs, data, nvs, , 0x6000,
phy_init, data, phy, , 0x1000,
factory, app, factory, , 1M,
fctry, data, nvs, , 0x6000,
Then select it, either in idf.py menuconfig under Partition Table, or in sdkconfig.defaults:
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
This layout has no OTA slots. If your project already has a custom table, just add the fctry line where there is room.
Generate it once, with real randomness
The code below does four things, in order:
- reads the stored password, if there is one
- otherwise generates 12 random characters
- commits them to flash before printing them
- starts a WPA2 access point using them
Here is the whole of main.c:
#include <stdio.h>
#include <string.h>
#include "bootloader_random.h"
#include "esp_event.h"
#include "esp_mac.h"
#include "esp_netif.h"
#include "esp_random.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#define PASS_LEN 12
static void load_or_create_password(char out[PASS_LEN + 1])
{
/* No 0/O or 1/I, because someone has to type this. */
static const char alphabet[] = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
_Static_assert(sizeof(alphabet) - 1 == 32, "need exactly 32 symbols");
/* No erase-on-error here: failing loudly beats a silent new password. */
ESP_ERROR_CHECK(nvs_flash_init_partition("fctry"));
nvs_handle_t nvs;
ESP_ERROR_CHECK(nvs_open_from_partition("fctry", "setup",
NVS_READWRITE, &nvs));
size_t len = PASS_LEN + 1;
esp_err_t err = nvs_get_str(nvs, "ap_pass", out, &len);
if (err == ESP_ERR_NVS_NOT_FOUND) {
uint8_t raw[PASS_LEN];
bootloader_random_enable(); /* must run before Wi-Fi starts */
esp_fill_random(raw, sizeof raw);
bootloader_random_disable();
for (int i = 0; i < PASS_LEN; i++) {
out[i] = alphabet[raw[i] & 31];
}
out[PASS_LEN] = '\0';
ESP_ERROR_CHECK(nvs_set_str(nvs, "ap_pass", out));
ESP_ERROR_CHECK(nvs_commit(nvs));
printf("NEW SETUP PASSWORD (label this board): %s\n", out);
} else {
ESP_ERROR_CHECK(err);
}
nvs_close(nvs);
}
void app_main(void)
{
char pass[PASS_LEN + 1];
load_or_create_password(pass);
ESP_ERROR_CHECK(nvs_flash_init()); /* used by the Wi-Fi stack */
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_ap();
wifi_init_config_t init = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&init));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
uint8_t mac[6];
ESP_ERROR_CHECK(esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP));
wifi_config_t cfg = {
.ap = {
.authmode = WIFI_AUTH_WPA2_PSK,
.max_connection = 1,
.channel = 1,
},
};
snprintf((char *)cfg.ap.ssid, sizeof cfg.ap.ssid,
"proto-%02X%02X%02X", mac[3], mac[4], mac[5]);
memcpy(cfg.ap.password, pass, sizeof pass);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &cfg));
ESP_ERROR_CHECK(esp_wifi_start());
}
In your component's CMakeLists.txt, declare what it uses:
idf_component_register(SRCS "main.c"
PRIV_REQUIRES bootloader_support esp_event esp_hw_support
esp_netif esp_wifi nvs_flash)
Why the odd dance around the random number generator?
esp_fill_random() is only truly random when it has an entropy source. According to Espressif's random number documentation, that means Wi-Fi or Bluetooth is running, or the bootloader's source is enabled. Otherwise, treat the output as pseudo-random.
Hang on, does the same function return plausible bytes either way? It does. Nothing errors and the output looks fine, so this is easy to miss.
So the code switches the source on, takes its bytes and switches it off. It has to be off again before Wi-Fi, Bluetooth or the ADC are used. If you move this into a larger application, keep it early.
Twelve characters gives you 60 bits
Length matters because WPA2 passwords can be guessed offline. Someone who captures a connection handshake can test candidates on their own hardware, with no rate limit from your board. WPA2 accepts eight characters, but eight random digits is under 27 bits.
The alphabet has exactly 32 symbols, which is rather neat. 256 divides evenly by 32, so taking five bits of each random byte introduces no bias. With 33 symbols and a lazy %, some characters would turn up more often than others.
Check that it behaves
Test with two boards, since one board cannot show you that passwords differ. Run idf.py flash monitor on each, then confirm:
- each prints a different password on its first boot
- after a reset, nothing new is printed and the old password still works
- a wrong password, including the other board's, is rejected
- erasing the default
nvspartition leaves the password unchanged
Missed the printout, or want a fresh password? Erase only the fctry partition, with your own serial port, and reboot:
parttool.py --port /dev/ttyUSB0 erase_partition --partition-name=fctry
A full idf.py erase-flash removes it too, so relabel the board afterwards.
What this does not protect against
Anyone with the board in their hands can probably read the password. Without encryption, NVS contents can be read or altered with physical access to the flash. For a prototype on your bench that may be fine. For something you hand to strangers, look at NVS encryption and its key protection before relying on it.
There are other limits:
- The serial line discloses the password too, so scrub first-boot logs before pasting them into an issue.
- The Wi-Fi password controls who joins the network. It does not authenticate requests to your setup page from a device that has already joined.
- This code leaves the access point running. A real setup flow should shut it down once configuration succeeds.
- If you use Espressif's provisioning component, pass this value as the SoftAP service key. Its proof-of-possession setting is a separate thing.
And put the label somewhere sensible. A sticker facing the window can be read through it, however unique its 60-bit password is.