Skip to main content

Module net

Module net 

Source
Expand description

WiFi radio and TCP/IP stack (esp-radio + embassy-net).

One of two modes is chosen at boot from Config::connection:

  • Access point (default): static IPv4 192.168.4.1/24 plus a DHCP server handing out leases to AP clients (dhcp).
  • Station: joins the configured network and gets its address via DHCP. If the association does not come up within STA_CONNECT_TIMEOUT โ€” or the DHCP lease does not arrive within STA_DHCP_TIMEOUT โ€” the device switches to AP mode instead so it stays reachable. After a successful station boot, a lost association is retried every STA_RECONNECT_DELAY (the device then stays in station mode, so reachability depends on the AP coming back).

The event loop at the end of task doubles as the controller keep-alive: dropping WifiController de-initializes the radio.

In both modes the HTTP/WebSocket server (http, ws) is started on the stack once the interface is up.

Modulesยง

dhcp ๐Ÿ”’
Minimal DHCPv4 server for access-point clients.
http
Minimal HTTP/1.1 server with fixed connection slots.
ws ๐Ÿ”’
Minimal WebSocket server (RFC 6455 subset) with JSON envelopes.

Constantsยง

AP_IP
Static IPv4 address of the access point โ€” also the DHCP server identifier handed out in option 54.
AP_PREFIX
Prefix length of the access-point subnet.
SOCKETS ๐Ÿ”’
Socket slots in the network stack โ€” four HTTP connection slots plus the DHCP server (AP mode) or DHCP client (station mode), with headroom.
STA_CONNECT_TIMEOUT ๐Ÿ”’
Maximum time to wait for a station association before falling back to access-point mode.
STA_DHCP_TIMEOUT ๐Ÿ”’
Maximum time to wait for the station DHCP lease before falling back to access-point mode.
STA_RECONNECT_DELAY ๐Ÿ”’
Pause between station re-association attempts after a runtime disconnect (the boot-time fallback does not apply here โ€” the device keeps retrying).

Staticsยง

STACK_AP ๐Ÿ”’
Socket storage for the AP-mode stack, kept separate from STACK_STA so a DHCP-timeout fallback can bring the AP up beside the idle station stack (both cells always cost ~2 KiB of .bss).
STACK_STA ๐Ÿ”’
Socket storage for the station-mode stack โ€” used when the boot path attempts a station lease (the cell is abandoned if DHCP times out).

Functionsยง

ap_ssid ๐Ÿ”’
Resolves the AP SSID: the configured one, or a MAC-derived default.
build_ap ๐Ÿ”’
Builds the access-point configuration for esp-radio.
build_sta ๐Ÿ”’
Builds the station configuration for esp-radio.
runner_task ๐Ÿ”’
Drives the network device event loop (spawned by spawn_runner).
spawn_runner ๐Ÿ”’
Spawns the embassy-net driver loop (logged, non-fatal on failure).
stack_seed ๐Ÿ”’
Deterministic seed for the network stack, derived from the factory MAC address (keeps ephemeral port selection stable across reboots).
start_ap ๐Ÿ”’
Brings up the embassy-net stack on the access-point interface with a static address and spawns the DHCP server for AP clients.
start_station ๐Ÿ”’
Brings up the embassy-net stack on the station interface with a DHCP client and waits for the lease.
task
Network task: brings up WiFi in station or AP mode and runs the event loop.
try_station ๐Ÿ”’
Races the station association against STA_CONNECT_TIMEOUT.