You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/src/network-programming-guide.md
+95-5Lines changed: 95 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -76,6 +76,9 @@ Callback functions can be specified by the following configuration parameters:
76
76
*`{connected, fun(() -> term())}` A callback function which will be called when the device connects to the target network.
77
77
*`{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.
78
78
*`{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.
79
82
80
83
```{warning}
81
84
IPv6 addresses are not yet supported in AtomVM.
@@ -112,8 +115,10 @@ gotIp(IpInfo) ->
112
115
io:format("Got IP: ~p~n", [IpInfo]).
113
116
114
117
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
+
...
117
122
```
118
123
119
124
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
138
143
end
139
144
```
140
145
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
-
143
146
### STA (or AP+STA) mode functions
144
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 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]),
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
+
145
235
#### `sta_status`
146
236
147
237
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:
191
281
192
282
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.
193
283
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.
195
285
196
286
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.
0 commit comments