Skip to content

Commit 7f4bec5

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 1e28d76 commit 7f4bec5

1 file changed

Lines changed: 98 additions & 4 deletions

File tree

doc/src/network-programming-guide.md

Lines changed: 98 additions & 4 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()) -> 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,7 +143,96 @@ 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).
146+
### STA (or AP+STA) mode functions
147+
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 mode and started, as long as no connection has been
167+
initiated or associated, you may scan for 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+
Due to network stack size limitations on the esp32 classic the maximum number of scan results that
203+
can be returned is 14. Other ESP32 family chips can return up to 20 networks in the scan results.
204+
The default scan is quite fast, and likely may not find all the available networks. Scans are
205+
quite configurable with `active` (the default) and `passive` modes. Options should take the form of
206+
a proplist. The per-channel scan time can be changed with the `dwell` key, the channel dwell time
207+
can be set for up to 1500 ms. Passive scans are slower, as they always linger on each channel for
208+
the full dwell time. Passive mode can be used by simply adding `passive` to the configuration
209+
proplist. Keep in mind when choosing a dwell time that between each progressively scanned channel
210+
the device must return to the home channel for a short time (typically 30ms), but for scans with a
211+
dwell time of over 1000ms the home channel dwell time will increase to 60ms to help mitigate
212+
beacon-timeout events. In some network configuration beacon timeout events may still occur, but
213+
should not lead to a dropped connection, and after the scan completes the device should receive the
214+
next beacon from the access point. The default of 6 access points in the returned `NetworkList` may
215+
be changed with the `results` key. By default hidden networks are ignored, but can be included in
216+
the results by adding `show_hidden` to the configuration.
217+
218+
For example, to do a passive scan using an ESP32-C6, including hidden networks, using the longest
219+
allowed scan time and showing the maximum number of networks available use the following:
220+
221+
```erlang
222+
{ok, Results} = network:wifi_scan([passive, {results, 20}, {dwell, 1500}, show_hidden]),
223+
```
224+
225+
For convenience the default options used for `network:wifi_scan/0` may be configured along with the
226+
`sta_config()` used to start the network driver. For the corresponding configuration keys consult
227+
the [`network:scan_options()`](./apidocs/erlang/eavmlib/network.md#scan-options) type definition.
228+
You may also perform a scan without starting the network with a configuration. This will use the
229+
configuration `[{sta, [managed]}]`, which uses the default `disconnected` event callback that will
230+
always attempt to reconnect to the last network. This makes future use of `network:wifi_scan/0,1`
231+
impossible, and `network:sta_disconnect/0` useless as the driver will immediately attempt to
232+
reconnect to the last network. This is mainly intended for quick testing purposes. For most
233+
applications that will use wifi scan results it is recommended to start the driver with a
234+
configuration that uses a custom callback function for `disconnected` events, so that the driver
235+
will remain idle and allow the use of scan results to decide if a connection should be made.
142236

143237
### STA (or AP+STA) mode functions
144238

@@ -191,7 +285,7 @@ The `<ap-properties>` property list may contain the following entries:
191285

192286
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.
193287

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.
288+
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.
195289

196290
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.
197291

0 commit comments

Comments
 (0)