Principle:78 Xiaozhi esp32 Network Provisioning
| Knowledge Sources | |
|---|---|
| Domains | Networking, Embedded_Systems |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
A network connection and provisioning pattern for embedded devices that retrieves persisted credentials from non-volatile storage, establishes connectivity asynchronously, reports connection state through event callbacks, and enters a configuration mode when no valid credentials are stored.
Description
Network provisioning is the process by which an embedded device obtains network credentials (WiFi SSID/password, cellular SIM config, Ethernet settings) and establishes a connection. The XiaoZhi firmware supports multiple network types through board base classes:
WifiBoard: Reads WiFi credentials from NVS. If no credentials are stored, enters BluFi (Bluetooth-based WiFi provisioning) mode. Usesesp_wifiAPIs for connection.Ml307Board: Cellular modem board using ML307 AT commands. Requires SIM card and network registration.EthernetBoard: Wired Ethernet connection.DualNetworkBoard: Supports both WiFi and ML307, selects based on availability.RndisBoard: USB RNDIS network interface.
All network types share a common interface:
StartNetwork(): Initiates the connection process (abstract, board-specific).SetNetworkEventCallback(callback): Registers a unified callback that receivesNetworkEventenum values (Scanning, Connecting, Connected, Disconnected, WifiConfigModeEnter/Exit, modem errors).- Event-driven flow: The callback sets
MAIN_EVENT_NETWORK_CONNECTEDon successful connection, which triggers the activation task.
The BluFi provisioning flow is used when WiFi credentials are not found in NVS:
- Device enters
kDeviceStateWifiConfiguring - BluFi broadcasts a BLE advertisement for provisioning
- User app sends SSID/password via BLE
- Device stores credentials in NVS and reboots/reconnects
Usage
Use this principle when:
- An embedded device must connect to a network using stored credentials
- First-boot provisioning is needed (no credentials stored)
- Multiple network types must be supported through a common interface
- Connection state must be reported to the UI and main event loop
Theoretical Basis
The pattern is an event-driven connection state machine:
States: Disconnected → Scanning → Connecting → Connected
↓
WifiConfigMode (if no credentials)
Events (NetworkEvent enum):
Scanning → UI: "Scanning WiFi..."
Connecting → UI: "Connecting to <SSID>..."
Connected → set MAIN_EVENT_NETWORK_CONNECTED
Disconnected → set MAIN_EVENT_NETWORK_DISCONNECTED
WifiConfigModeEnter → device state = WifiConfiguring
WifiConfigModeExit → restart network
The callback-based notification decouples network implementation from application logic:
board.SetNetworkEventCallback([this](NetworkEvent event, const std::string& data) {
switch (event) {
case NetworkEvent::Connected:
xEventGroupSetBits(event_group_, MAIN_EVENT_NETWORK_CONNECTED);
break;
case NetworkEvent::Disconnected:
xEventGroupSetBits(event_group_, MAIN_EVENT_NETWORK_DISCONNECTED);
break;
// ...
}
});
board.StartNetwork();
Practical Guide
When implementing network provisioning:
- Store credentials in NVS: use
Settings("wifi")to persist SSID/password. - Enter config mode on first boot: if
Settings.GetString("ssid")is empty, enter BluFi provisioning. - Use event callbacks, not polling: register
SetNetworkEventCallbackand signal via event bits. - Handle reconnection: on disconnect, update UI and attempt reconnection.
- Report meaningful data: the callback's
datastring carries SSID or carrier name for display.