Skip to content

Commit 8d00424

Browse files
committed
Document new wifi functions
Adds documentation for `network:wifi_scan/0,1` and updates details for `network:sta_rssi/0`. Signed-off-by: Winford <winford@object.stream>
1 parent e54e66f commit 8d00424

File tree

1 file changed

+95
-5
lines changed

1 file changed

+95
-5
lines changed

doc/src/network-programming-guide.md

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ Callback functions can be specified by the following configuration parameters:
7676
* `{connected, fun(() -> term())}` A callback function which will be called when the device connects to the target network.
7777
* `{disconnected, fun(() -> term())}` A callback function which will be called when the device disconnects from the target network. If no callback function is provided the default behavior is to attempt to reconnect immediately. By providing a callback function the application can decide whether to reconnect, or connect to a new access point.
7878
* `{got_ip, fun((ip_info()) -> term())}` A callback function which will be called when the device obtains an IP address. In this case, the IPv4 IP address, net mask, and gateway are provided as a parameter to the callback function.
79+
* `{scan_done, fun((scan_results() | {error, Reason :: term()}) -> term())}` A callback function which will be called once a network scan is
80+
completed, this allows for event-driven connection management, and prevents blocking the caller
81+
when requesting a scan of available wifi networks.
7982

8083
```{warning}
8184
IPv6 addresses are not yet supported in AtomVM.
@@ -112,8 +115,10 @@ gotIp(IpInfo) ->
112115
io:format("Got IP: ~p~n", [IpInfo]).
113116

114117
disconnected() ->
115-
io:format("Disconnected from AP, attempting to reconnect~n"),
116-
network:sta_connect().
118+
io:format("Disconnected from AP, starting scan~n"),
119+
{ok, {Num, NetworkList}} = network:wifi_scan(),
120+
% your reconnect logic here
121+
...
117122
```
118123

119124
In a typical application, the network should be configured and an IP address should be acquired first, before starting clients or services that have a dependency on the network.
@@ -138,10 +143,95 @@ case network:wait_for_sta(Config, 15000) of
138143
end
139144
```
140145

141-
To obtain the signal strength (in decibels) of the connection to the associated access point use [`network:sta_rssi/0`](./apidocs/erlang/eavmlib/network.md#sta_rssi0).
142-
143146
### STA (or AP+STA) mode functions
144147

148+
Some functions are only available if the device is configured in STA or AP+STA mode.
149+
150+
#### `sta_rssi`
151+
152+
Once connected to an access point, the signal strength in decibel-milliwatts (dBm) of the
153+
connection to the associated access point may be obtained using
154+
[`network:sta_rssi/0`](./apidocs/erlang/eavmlib/network.md#sta_rssi0). The value returned as
155+
`{ok, Value}` will typically be a negative number, but in the
156+
presence of a powerful signal this can be a positive number. A level of 0 dBm corresponds to the
157+
power of 1 milliwatt. A 10 dBm decrease in level is equivalent to a ten-fold decrease in signal
158+
power.
159+
160+
#### `wifi_scan`
161+
162+
```{notice}
163+
This function is currently only supported on the ESP32 platform.
164+
```
165+
166+
After the network has been configured for STA or AP+STA mode and started, you may scan for
167+
available access points using
168+
[`network:wifi_scan/0`](./apidocs/erlang/eavmlib/network.md#wifi_scan0) or
169+
[`network:wifi_scan/1`](./apidocs/erlang/eavmlib/network.md#wifi_scan1). Scanning for access
170+
points will temporarily inhibit other traffic on the access point network if it is in use, but
171+
should not cause any active connections to be dropped. With no options, a default 'active' scan,
172+
with a per-channel dwell time of 120ms will be used and will return network details for up to 6
173+
access points. The return value for the scan takes the form of a tuple consisting of
174+
`{ok, Results}`, where `Results = {FoundAPs, NetworkList}`. `FoundAPs` may be a number larger than
175+
the length of the NetworkList if more access points were discovered than the number of results
176+
requested. The entries in the `NetworkList` take the form of a map with the keys `ssid` mapped to
177+
the network name, `rssi` for the dBm signal strength of the access point, `authmode` value is the
178+
authentication method used by the network, `bssid` (a.k.a MAC address) of the access point, and the
179+
`channel` key for the primary channel for the network.
180+
181+
Example return results:
182+
```erlang
183+
{ok, {Num, Networks}} = network:wifi_scan(),
184+
io:format("network scan found ~p networks.~n", [Num]),
185+
lists:foreach(
186+
fun(
187+
_Network = #{ssid := SSID, rssi := DBm, authmode := Mode, bssid := BSSID, channel := Number}
188+
) ->
189+
io:format(
190+
"Network: ~p, BSSID: ~p, signal ~p dBm, Security: ~p, channel ~p~n",
191+
[SSID, BSSID, DBm, Mode, Number]
192+
)
193+
end,
194+
Networks
195+
).
196+
```
197+
198+
To avoid blocking the caller for extended lengths of time, especially on 5 Ghz capable devices,
199+
a callback function may be configured in the network config. See
200+
[Station mode callbacks](#station-mode-callbacks).
201+
202+
To minimize the risk of out-of-memory errors, this driver limits the maximum number of returned
203+
networks depending on the target and memory configuration:
204+
ESP32-C2 supports up to 10, ESP32-S2/ESP32-C61/ESP32-C5 up to 14, most other targets up to 20,
205+
and ESP32-P4 or PSRAM-enabled builds up to 64.
206+
207+
The default scan is quite fast, and likely may not find all the available networks. Scans are
208+
quite configurable with `active` (the default) and `passive` modes. Options should take the form of
209+
a proplist. The per-channel scan time can be changed with the `dwell` key, the channel dwell time
210+
can be set for up to 1500 ms. Passive scans are slower, as they always linger on each channel for
211+
the full dwell time. Passive mode can be used by simply adding `passive` to the configuration
212+
proplist. Keep in mind when choosing a dwell time that between each progressively scanned channel
213+
the device must return to the home channel for a short time (typically 30ms), but for scans with a
214+
dwell time of over 1000ms the home channel dwell time will increase to 60ms to help mitigate
215+
beacon-timeout events. In some network configuration beacon timeout events may still occur, but
216+
should not lead to a dropped connection, and after the scan completes the device should receive the
217+
next beacon from the access point. The default of 6 access points in the returned `NetworkList` may
218+
be changed with the `results` key. By default hidden networks are ignored, but can be included in
219+
the results by adding `show_hidden` to the configuration.
220+
221+
For example, to do a passive scan using an ESP32-C6, including hidden networks, using the longest
222+
allowed scan time and showing the maximum number of networks available use the following:
223+
224+
```erlang
225+
{ok, Results} = network:wifi_scan([passive, {results, 20}, {dwell, 1500}, show_hidden]),
226+
```
227+
228+
For convenience the default options used for `network:wifi_scan/0` may be configured along with the
229+
`sta_config()` used to start the network driver. For the corresponding configuration keys consult
230+
the [`network:scan_options()`](./apidocs/erlang/eavmlib/network.md#scan-options) type definition.
231+
For most applications that will use wifi scan results, it is recommended to start the driver with a
232+
configuration that uses a custom callback function for `disconnected` events, so that the driver
233+
will remain idle and allow the use of scan results to decide if a connection should be made.
234+
145235
#### `sta_status`
146236

147237
The function [`network:sta_status/0`](./apidocs/erlang/eavmlib/network.md#sta_status0) may be used
@@ -191,7 +281,7 @@ The `<ap-properties>` property list may contain the following entries:
191281

192282
If the SSID is omitted in configuration, the SSID name `atomvm-<hexmac>` will be created, where `<hexmac>` is the hexadecimal representation of the factory-assigned MAC address of the device. This name should be sufficiently unique to disambiguate it from other reachable ESP32 devices, but it may also be difficult to read or remember.
193283

194-
If the password is omitted, then an _open network_ will be created, and a warning will be printed to the console. Otherwise, the AP network will be started using WPA+WPA2 authentication.
284+
If the password is omitted, then an __open network__ will be created, and a warning will be printed to the console. Otherwise, the AP network will be started using WPA+WPA2 authentication.
195285

196286
If the channel is omitted the default chanel for esp32 is `1`. This setting is only used while a device is operation is AP mode only. If `ap_channel` is configured, it will be temporarily changed to match the associated access point if AP + STA mode is used and the station is associated with an access point. This is a hardware limitation due to the modem radio only being able to operate on a single channel (frequency) at a time.
197287

0 commit comments

Comments
 (0)