First, What Is It
- Wi-Fi (802.11): lets devices join a LAN over radio; Ai-WB2 has a built-in 2.4 GHz Wi-Fi 4 radio.
- STA / AP: connecting to a router is STA (station); hosting a hotspot is AP (access point); the dev board can be either.
- Connecting is not plug-and-play: it goes through scan → authentication → association → DHCP (get IP); failure at any step means no connection.
Breaking Down the Principle
1. STA Connection Flow (Power-on to Online)
The key events appear in every example: CODE_WIFI_ON_INIT_DONE → CODE_WIFI_ON_CONNECTED → CODE_WIFI_ON_GOT_IP → CODE_WIFI_ON_DISCONNECT.
2. Soft-AP Mode
The role reverses when the board hosts a hotspot: the firmware starts an AP and waits for clients (CODE_WIFI_ON_AP_STARTED, CODE_WIFI_ON_AP_STA_ADD). Typical uses: provisioning, LAN control panel.
3. SDK's Event-Driven Implementation
SDK Wi-Fi uses an event callback model: instead of polling, register wifi_event_handler and get notified on state changes:
async_register_event_filter(EV_WIFI, wifi_event_handler, NULL);
wifi_task_create(); /* start the Wi-Fi firmware */
fhost_init(); /* start the Wi-Fi management framework */How the SDK Implements It
- Related pages: Connect Wi-Fi, Router Mode (Soft-AP), Wi-Fi Intro
CLI connect (from the wifi_ble coexistence example):
wifi_sta_connect <ssid> <passwd>; /* connect to a router */
wifi_sta_ps_on; /* enable power save (recommended for coexistence) */In code, the event callback starts TCP/UDP/HTTP/MQTT tasks after "connected/got IP" — the standard pattern of SDK examples.
Common Exam & Interview Questions
Does connecting to Wi-Fi mean you can go online?
No. Connected only means the link layer is associated; you still need an IP address from DHCP before using IP/TCP/UDP/HTTP protocols.
Difference between CODE_WIFI_ON_CONNECTED and CODE_WIFI_ON_GOT_IP?
The former means authenticated/associated with the router (no IP yet); the latter means DHCP assigned an IP — only then can network communication start.
Why does the SDK use event callbacks instead of polling?
Events notify the app only when state changes, letting the CPU sleep when idle and respond faster; polling keeps the CPU busy and can miss transient states.
Can STA and Soft-AP run simultaneously?
In some scenarios yes (e.g., AP+STA for relaying), but the shared radio is time-multiplexed, hurting throughput/stability; normal examples pick one.
Where does a wrong Wi-Fi password fail?
Usually at authentication/association: the device cannot connect or keeps disconnecting, staying before CONNECTED (or quickly receiving DISCONNECT) — check the SSID and password.
Have questions?
For any other questions, visit the unified Q&A and discussion board: Ai-Thinker Discussions

