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
@@ -76,6 +76,11 @@ 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.
io:format("Connecting to unsecured network ~s...~n", [SSID]),
152
+
network:sta_connect([{ssid, SSID}]);
153
+
undefined ->
154
+
io:format("No psk stored in nvs for network ~s with ~p security!~n", [SSID, Auth]),
155
+
connect_if_known(Results);
156
+
PSK ->
157
+
io:format("Connecting to ~s (~p)...~n", [SSID, Auth]),
158
+
network:sta_connect([{ssid, SSID}, {psk, PSK}])
159
+
end;
160
+
false ->
161
+
connect_if_known(Results)
162
+
end.
117
163
```
118
164
119
165
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 +184,107 @@ case network:wait_for_sta(Config, 15000) of
138
184
end
139
185
```
140
186
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
187
### STA (or AP+STA) mode functions
144
188
189
+
Some functions are only available if the device is configured in STA or AP+STA mode.
190
+
191
+
#### `sta_rssi`
192
+
193
+
Once connected to an access point, the signal strength in decibel-milliwatts (dBm) of the
194
+
connection to the associated access point may be obtained using
195
+
[`network:sta_rssi/0`](./apidocs/erlang/eavmlib/network.md#sta_rssi0). The value returned as
196
+
`{ok, Value}` will typically be a negative number, but in the
197
+
presence of a powerful signal this can be a positive number. A level of 0 dBm corresponds to the
198
+
power of 1 milliwatt. A 10 dBm decrease in level is equivalent to a ten-fold decrease in signal
199
+
power.
200
+
201
+
#### `wifi_scan`
202
+
203
+
```{notice}
204
+
This function is currently only supported on the ESP32 platform.
205
+
```
206
+
207
+
After the network has been configured for STA or AP+STA mode and started, you may scan for
208
+
available access points using
209
+
[`network:wifi_scan/0`](./apidocs/erlang/eavmlib/network.md#wifi_scan0) or
210
+
[`network:wifi_scan/1`](./apidocs/erlang/eavmlib/network.md#wifi_scan1). Scanning for access
211
+
points will temporarily inhibit other traffic on the access point network if it is in use, but
212
+
should not cause any active connections to be dropped. With no options, a default 'active'
213
+
(`{passive, false}`) scan, with a per-channel dwell time of 120ms will be used and will return
214
+
network details for up to 6 access points, the default may be changed using the `sta_scan_config()`
215
+
option in the `sta_config()`. The return value for the scan takes the form of a tuple consisting
216
+
of `{ok, Results}`, where `Results = {FoundAPs, NetworkList}`. `FoundAPs` may be a number larger
217
+
than the length of the NetworkList if more access points were discovered than the number of results
218
+
requested. The entries in the `NetworkList` take the form of a map with the keys `ssid` mapped to
219
+
the network name, `rssi` for the dBm signal strength of the access point, `authmode` value is the
220
+
authentication method used by the network, `bssid` (a.k.a MAC address) of the access point, the
221
+
`channel` key for the primary channel for the network, hidden networks (when `show_hidden` is
222
+
used in the `scan_options()`) will have an empty `ssid` and the `hidden` key will be set to `true`.
223
+
If an error is encountered the return will be `{error, Reason :: term()}`. If the network is
224
+
stopped while a scan is in progress, the callback or caller may receive either a successful scan
225
+
result, or `{error, canceled}`.
226
+
227
+
Blocking example with no `scan_done` callback:
228
+
```erlang
229
+
casenetwork:wifi_scan() of
230
+
{ok, {Num, Networks}} ->
231
+
io:format("network scan found ~p networks.~n", [Num]),
For convenience the default options used by `network:wifi_scan/0` may be configured along
280
+
with the `sta_config()` used to start the network driver. For the corresponding startup-time scan
281
+
configuration keys, consult `sta_scan_config()` in the `sta_config()` definition rather than the
282
+
runtime [`scan_options()`](./apidocs/erlang/eavmlib/network.md#scan-options) accepted by
283
+
`network:wifi_scan/1`. For most applications that will use wifi scan results, it is recommended to
284
+
start the driver with a configuration that uses a custom callback function for `disconnected`
285
+
events, so that the driver will remain idle and allow the use of scan results to decide if a
286
+
connection should be made.
287
+
145
288
#### `sta_status`
146
289
147
290
The function [`network:sta_status/0`](./apidocs/erlang/eavmlib/network.md#sta_status0) may be used
@@ -191,7 +334,7 @@ The `<ap-properties>` property list may contain the following entries:
191
334
192
335
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
336
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.
337
+
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
338
196
339
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