Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:78 Xiaozhi esp32 Network Provisioning

From Leeroopedia
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. Uses esp_wifi APIs 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 receives NetworkEvent enum values (Scanning, Connecting, Connected, Disconnected, WifiConfigModeEnter/Exit, modem errors).
  • Event-driven flow: The callback sets MAIN_EVENT_NETWORK_CONNECTED on successful connection, which triggers the activation task.

The BluFi provisioning flow is used when WiFi credentials are not found in NVS:

  1. Device enters kDeviceStateWifiConfiguring
  2. BluFi broadcasts a BLE advertisement for provisioning
  3. User app sends SSID/password via BLE
  4. 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 SetNetworkEventCallback and signal via event bits.
  • Handle reconnection: on disconnect, update UI and attempt reconnection.
  • Report meaningful data: the callback's data string carries SSID or carrier name for display.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment