Rdzleo dbdd304905 代码初始化:
本项目为触摸版项目代码复制而来,基于此版本进行按键功能的适配!
2026-03-23 11:14:56 +08:00
..
2026-03-23 11:14:56 +08:00
2026-03-23 11:14:56 +08:00
2026-03-23 11:14:56 +08:00
2026-03-23 11:14:56 +08:00
2026-03-23 11:14:56 +08:00
2026-03-23 11:14:56 +08:00
2026-03-23 11:14:56 +08:00
2026-03-23 11:14:56 +08:00
2026-03-23 11:14:56 +08:00
2026-03-23 11:14:56 +08:00

ESP-Brookesia WiFi Service

Overview

brookesia_service_wifi is a WiFi connection management service for the ESP-Brookesia ecosystem, providing:

  • State Machine Management: Unified management of WiFi lifecycle states including initialization, startup, and connection through a state machine
  • Auto Reconnection: Supports automatic connection to historical APs and automatic reconnection attempts after disconnection
  • WiFi Scanning: Supports periodic scanning of surrounding APs and automatic discovery of connectable APs
  • Connection Management: Manages target AP and connected AP lists, supporting multiple AP history records
  • Event Notifications: Provides rich event notification mechanisms for real-time feedback on WiFi state changes
  • Persistent Storage: Optionally works with brookesia_service_nvs service to persistently save connection configurations and other parameters

Table of Contents

Features

State Machine Management

The WiFi service uses a state machine to uniformly manage WiFi lifecycle states, ensuring safety and consistency of state transitions. The state machine contains 4 core states:

State Description
Idle WiFi not initialized, initial system state
Inited WiFi initialized but not started, parameters can be configured
Started WiFi started, scanning or waiting for connection
Connected WiFi successfully connected to AP, normal communication available

State Transitions

State transitions are achieved by triggering corresponding actions:

  • Forward Flow: IdleInited (Init) → Started (Start) → Connected (Connect)
  • Disconnect: ConnectedStarted (Disconnect)
  • Stop Process: Started / ConnectedInited (Stop)
  • Deinitialize: InitedIdle (Deinit)

The state transition diagram is as follows:

---
config:
  look: neo
---
stateDiagram-v2
  direction TB

  %% State Styles
  classDef Stable fill:#DEFFF8,stroke:#46EDC8,color:#378E7A,font-weight:bold;
  classDef Transient fill:#FFEFDB,stroke:#FBB35A,color:#8F632D,font-weight:bold;

  %% Initial State
  [*] --> Idle

  %% =====================
  %% Idle
  %% =====================
  Idle --> Initing: Init

  %% =====================
  %% Initing
  %% =====================
  state Initing {
    do_init() --> poll_until_inited()
  }

  Initing --> Inited: Success
  Initing --> Idle: Failure
  Initing --> Deiniting: Timeout

  %% =====================
  %% Inited
  %% =====================
  Inited --> Starting: Start
  Inited --> Deiniting: Deinit

  %% =====================
  %% Deiniting
  %% =====================
  state Deiniting {
    do_deinit() --> poll_until_deinited()
  }

  Deiniting --> Idle: Success / Failure / Timeout

  %% =====================
  %% Starting
  %% =====================
  state Starting {
    do_start() --> poll_until_started()
  }

  Starting --> Started: Success
  Starting --> Inited: Failure
  Starting --> Stopping: Timeout

  %% =====================
  %% Started
  %% =====================
  Started --> Connecting: Connect
  Started --> Stopping: Stop

  %% =====================
  %% Connecting
  %% =====================
  state Connecting {
    do_connect() --> poll_until_connected()
  }

  Connecting --> Connected: Success
  Connecting --> Started: Failure
  Connecting --> Disconnecting: Timeout

  %% =====================
  %% Connected
  %% =====================
  Connected --> Disconnecting: Disconnect
  Connected --> Stopping: Stop

  %% =====================
  %% Disconnecting
  %% =====================
  state Disconnecting {
    do_disconnect() --> poll_until_disconnected()
  }

  Disconnecting --> Started: Success / Failure / Timeout

  %% =====================
  %% Stopping
  %% =====================
  state Stopping {
    do_stop() --> poll_until_stopped()
  }

  Stopping --> Inited: Success / Failure / Timeout

  class Idle,Inited,Started,Connected Stable
  class Initing,Starting,Connecting,Disconnecting,Stopping,Deiniting Transient

Auto Reconnection Mechanism

  • Auto Connect on Startup: Automatically attempts to connect to historical connectable APs after WiFi starts
  • Auto Reconnect after Disconnection: Automatically attempts to connect to historical connectable APs after detecting unexpected disconnection
  • Auto Connect on Scan Discovery: Automatically triggers connection when target AP or historical connectable AP is discovered during scanning

WiFi Scanning

  • Periodic Scanning: Supports configuring scan interval and timeout
  • Scan Result Notifications: Real-time notification of scanned AP information through events
  • AP Information: Includes SSID, signal strength level, encryption status, and other information

SoftAP Functionality

  • Parameter Configuration: Supports setting SoftAP SSID, password, maximum connections, and channel
  • Optimal Channel Selection: If channel is not set, automatically scans nearby APs and selects the best channel
  • Provisioning Function: Supports starting SoftAP provisioning functionality

Development Environment Requirements

Before using this library, please ensure the following SDK development environment is installed:

Note

For SDK installation instructions, please refer to ESP-IDF Programming Guide - Installation

Adding to Project

brookesia_service_wifi has been uploaded to the Espressif Component Registry. You can add it to your project in the following ways:

  1. Using Command Line

    Run the following command in your project directory:

    idf.py add-dependency "espressif/brookesia_service_wifi"
    
  2. Modify Configuration File

    Create or modify the idf_component.yml file in your project directory:

    dependencies:
      espressif/brookesia_service_wifi: "*"
    

For detailed instructions, please refer to Espressif Documentation - IDF Component Manager.