Wi-Fi for boards: scan, join a network, read the address and signal, TCP client sockets and a soft access point, with a simulated radio on a computer.
import hw.wifi
hw.wifi connects a board to a Wi-Fi network: scan for networks, join one with a password, ask for the address and signal, open a TCP connection and start a soft access point. It is the station-side API only and every call returns a plain value, so a failure is an answer you check, never an exception.
Where it runs
What answers the calls
A computer
A deterministic fake radio in memory. You fill the air with SimAddNetwork, SimSetPassword and SimSetReply, so programs and tests run anywhere.
An ESP32-C3 through ESP-IDF
The salam_rt_wifi_* hooks implemented in idf_radio.c on top of esp_wifi, esp_netif and lwip. See the ESP32-C3 section of docs/FREESTANDING.md. This path has not been run on a device.
A board without a radio
The runtime's default hooks answer "unsupported", so Init returns false and the program still links.
Things to know:
•
No hidden threads.Scan and Connect block until they finish or time out. Connect takes its timeout in milliseconds and polls the link itself.
•
Codes next to enums.Status and SecurityOf return the State and Security enums. StateCode, SecurityCode and the state_ and security_ constants give the same values as plain numbers for comparisons and printing.
•
Read fields with functions.Ssid, Bssid, Signal, Channel and SecurityOf read a Network; SocketOk and SocketId read a Socket.
•
You own received text.TcpReceive returns a heap string; free it with mem.Free(text as void*) when you are done with it.
•
Join before sockets.TcpOpen fails until Connect has succeeded.
Joining the strongest network
Call Init, scan, pick the entry with the best Signal and connect to it. This example fills the simulated air first, so it runs on a computer.
Looks for networks and returns up to max_scan of them as a Vector of Network. The result is empty before Init. On a board the call blocks until the scan is done.
The example for this item is shown with the ones above: hw-wifi-Scan.salam
Joins a network and waits up to timeout_ms milliseconds for an address. Returns true when connected. On a failure the state is Failed and the radio is left disconnected. An open network ignores the password.
Starts a soft access point other devices can join. The name is 1 to 32 bytes, the password is empty (open) or 8 or more characters, and the channel is 1 to 13. Returns true on success.
Opens a TCP connection to a host name or address and a port from 1 to 65535. The station must be connected. A failure returns a Socket for which SocketOk is false.
The example for this item is shown with the ones above: hw-wifi-Tcp.salam