Add support for esp32 network driver scanning for access points#1165
Add support for esp32 network driver scanning for access points#1165UncleGrumpy wants to merge 4 commits intoatomvm:mainfrom
Conversation
58cf1f8 to
cdadc33
Compare
|
I struggled here: I started out empty example and tried: This crashes with: Then I read docs, about having to start the network, and tried: which crashed with: delaying things and scanning after connection is done made it further to a crash: It also seems that connecting to a network not available, disallows scanning with: Would be great seeing an example that could be used in CI eg:
I'll work on getting wokwi CI going so this can be covered by CI. |
|
I did not add an example yet, my plan was to write a demonstration of “roaming” for atomvm_examples. It is a little difficult to use until #1181 is merged, that will allow starting WiFi without immediately starting a connection. Scans will always fail when a connection is in progress, so your application will need to start a scan after obtaining an IP address, or before any connection is started (which is not possible until after #1181). |
|
I wish your second example had a stacktrace! You can see the scan result did come back with no networks found… this can happen with default scans (the dwell time is very short by default - and can easily miss networks, in my experience). I think it’s just a bad match in your test that is crashing there. |
ohh the embarrassment lol - it's the IO.inspect call that crashes, will look into it - used to throwing anything at IO.inspect.. Makes sense with the other stuff, suppose some kind of WifiManager GenServer will materialize, while this PR is on driver primitives level.. I'll test #1181 and help land that first.. |
|
My plan precisely! The PRs I have already submitted will provide all the necessary low level functionality, but I would like to create a higher level network_manager module that can simplify configuration and orchestration. |
|
As far as “dwell” time for the scan, I have found between 300-500ms will find all of the available networks, with the default (120ms) I do notice networks being frequently missed. |
cdadc33 to
b70a385
Compare
That does sound like a bug, from my limmited understanding of Elixir you should be able to give it just about anything, much like |
b70a385 to
aa3a56d
Compare
You did point me to a problem here, I started testing more boards and realized that even with maximum dwell time no network are being found. I cherry-picked these commits from a different local branch and must have missed something. |
480b056 to
50a78c7
Compare
|
When I split up the working branch I was testing new network features on I mistakenly submitted this PR first, but in order to use the scan function PR #1181 needs to be merged first, and this will need to be rebased. |
I did find a bug that would cause the results to be empty if only a single network was found, during most of my testing I had multiple networks, so this didn't get caught. @petermm, thanks for testing and helping me correct this! |
50a78c7 to
d0a4ea5
Compare
For more fine grained connection management in applications the driver can now be started, without perfoming an inital connection, by the use of the key `managed` in the STA configuration. Adds network:sta_connect/0,1 to allow connecting to an access point after the driver has been started in STA or STA+AP mode. If the function is used without parameters a connection to the last configured access point will be started. Adds network:sta_disconnect/0 to disconnect a station from an access point. The station mode disconnected callback now maintains the default behavior of reconnecting to the last access point if the connection is lost, but if the user defines a custom callback the automatic re-connection will not happen, allowing for users to take advantage of scan results or some other means to determine when and which access point to associate with. The combination of the use of a disconnected callback and `managed` mode allow for the use of `network:wifi_scan/0,1` (PR atomvm#1165), since the wifi must not be connected to a station when perfoming a scan and the current implementation always starts a connection immediatly and always reconnects when disconnected. Signed-off-by: Winford <winford@object.stream>
d0a4ea5 to
d72b955
Compare
For more fine grained connection management in applications the driver can now be started, without perfoming an inital connection, by the use of the key `managed` in the STA configuration. Adds network:sta_connect/0,1 to allow connecting to an access point after the driver has been started in STA or STA+AP mode. If the function is used without parameters a connection to the last configured access point will be started. Adds network:sta_disconnect/0 to disconnect a station from an access point. The station mode disconnected callback now maintains the default behavior of reconnecting to the last access point if the connection is lost, but if the user defines a custom callback the automatic re-connection will not happen, allowing for users to take advantage of scan results or some other means to determine when and which access point to associate with. The combination of the use of a disconnected callback and `managed` mode allow for the use of `network:wifi_scan/0,1` (PR atomvm#1165), since the wifi must not be connected to a station when perfoming a scan and the current implementation always starts a connection immediatly and always reconnects when disconnected. Signed-off-by: Winford <winford@object.stream>
d72b955 to
b3b0558
Compare
b3b0558 to
29c9c9f
Compare
For more fine grained connection management in applications the driver can now be started, without perfoming an inital connection, by the use of the key `managed` in the STA configuration. Adds network:sta_connect/0,1 to allow connecting to an access point after the driver has been started in STA or STA+AP mode. If the function is used without parameters a connection to the last configured access point will be started. Adds network:sta_disconnect/0 to disconnect a station from an access point. The station mode disconnected callback now maintains the default behavior of reconnecting to the last access point if the connection is lost, but if the user defines a custom callback the automatic re-connection will not happen, allowing for users to take advantage of scan results or some other means to determine when and which access point to associate with. The combination of the use of a disconnected callback and `managed` mode allow for the use of `network:wifi_scan/0,1` (PR atomvm#1165), since the wifi must not be connected to a station when perfoming a scan and the current implementation always starts a connection immediatly and always reconnects when disconnected. Signed-off-by: Winford <winford@object.stream>
29c9c9f to
52d491f
Compare
For more fine grained connection management in applications the driver can now be started, without perfoming an inital connection by the use of the key `managed` in the STA configuration. Adds network:sta_connect/0,1 to allow connecting to an access point after the driver has been started in STA or STA+AP mode. When used without parameters a connection to the last configured access point will be started. Adds network:sta_disconnect/0 to disconnect a station from an access point. The station mode disconnected default callback behavior is unchanged unless a custom callback is provided, in which case the automatic re-connection will not happen, allowing for users to take advantage of scan results or some other means to determine when and which access point to associate with. The combination of the use of a disconnected callback and `managed` mode allow for the use of `network:wifi_scan/0,1` (PR atomvm#1165), since the wifi must not be connected to a station when perfoming a scan and old implementation always started a connection immediatly and always persisted in attempts to reconnect when disconnected. Signed-off-by: Winford <winford@object.stream>
For more fine grained connection management in applications the driver can now be started, without perfoming an inital connection by the use of the key `managed` in the STA configuration. Adds network:sta_connect/0,1 to allow connecting to an access point after the driver has been started in STA or STA+AP mode. When used without parameters a connection to the last configured access point will be started. Adds network:sta_disconnect/0 to disconnect a station from an access point. The station mode disconnected default callback behavior is unchanged unless a custom callback is provided, in which case the automatic re-connection will not happen, allowing for users to take advantage of scan results or some other means to determine when and which access point to associate with. The combination of the use of a disconnected callback and `managed` mode allow for the use of `network:wifi_scan/0,1` (PR atomvm#1165), since the wifi must not be connected to a station when perfoming a scan and old implementation always started a connection immediatly and always persisted in attempts to reconnect when disconnected. Signed-off-by: Winford <winford@object.stream>
For more fine grained connection management in applications the driver can now be started, without perfoming an inital connection by the use of the key `managed` in the STA configuration. Adds network:sta_connect/0,1 to allow connecting to an access point after the driver has been started in STA or STA+AP mode. When used without parameters a connection to the last configured access point will be started. Adds network:sta_disconnect/0 to disconnect a station from an access point. The station mode disconnected default callback behavior is unchanged unless a custom callback is provided, in which case the automatic re-connection will not happen, allowing for users to take advantage of scan results or some other means to determine when and which access point to associate with. The combination of the use of a disconnected callback and `managed` mode allow for the use of `network:wifi_scan/0,1` (PR atomvm#1165), since the wifi must not be connected to a station when perfoming a scan and old implementation always started a connection immediatly and always persisted in attempts to reconnect when disconnected. Signed-off-by: Winford <winford@object.stream>
For more fine grained connection management in applications the driver can now be started, without perfoming an inital connection by the use of the key `managed` in the STA configuration. Adds network:sta_connect/0,1 to allow connecting to an access point after the driver has been started in STA or STA+AP mode. When used without parameters a connection to the last configured access point will be started. Adds network:sta_disconnect/0 to disconnect a station from an access point. The station mode disconnected default callback behavior is unchanged unless a custom callback is provided, in which case the automatic re-connection will not happen, allowing for users to take advantage of scan results or some other means to determine when and which access point to associate with. The combination of the use of a disconnected callback and `managed` mode allow for the use of `network:wifi_scan/0,1` (PR atomvm#1165), since the wifi must not be connected to a station when perfoming a scan and old implementation always started a connection immediatly and always persisted in attempts to reconnect when disconnected. Signed-off-by: Winford <winford@object.stream>
For more fine grained connection management in applications the driver can now be started, without perfoming an inital connection by the use of the key `managed` in the STA configuration. Adds network:sta_connect/0,1 to allow connecting to an access point after the driver has been started in STA or STA+AP mode. When used without parameters a connection to the last configured access point will be started. Adds network:sta_disconnect/0 to disconnect a station from an access point. The station mode disconnected default callback behavior is unchanged unless a custom callback is provided, in which case the automatic re-connection will not happen, allowing for users to take advantage of scan results or some other means to determine when and which access point to associate with. The combination of the use of a disconnected callback and `managed` mode allow for the use of `network:wifi_scan/0,1` (PR atomvm#1165), since the wifi must not be connected to a station when perfoming a scan and old implementation always started a connection immediatly and always persisted in attempts to reconnect when disconnected. Signed-off-by: Winford <winford@object.stream>
For more fine grained connection management in applications the driver can now be started, without perfoming an inital connection by the use of the key `managed` in the STA configuration. Adds network:sta_connect/0,1 to allow connecting to an access point after the driver has been started in STA or STA+AP mode. When used without parameters a connection to the last configured access point will be started. Adds network:sta_disconnect/0 to disconnect a station from an access point. The station mode disconnected default callback behavior is unchanged unless a custom callback is provided, in which case the automatic re-connection will not happen, allowing for users to take advantage of scan results or some other means to determine when and which access point to associate with. The combination of the use of a disconnected callback and `managed` mode allow for the use of `network:wifi_scan/0,1` (PR atomvm#1165), since the wifi must not be connected to a station when perfoming a scan and old implementation always started a connection immediatly and always persisted in attempts to reconnect when disconnected. Signed-off-by: Winford <winford@object.stream>
For more fine grained connection management in applications the driver can now be started, without perfoming an inital connection by the use of the key `managed` in the STA configuration. Adds network:sta_connect/0,1 to allow connecting to an access point after the driver has been started in STA or STA+AP mode. When used without parameters a connection to the last configured access point will be started. Adds network:sta_disconnect/0 to disconnect a station from an access point. The station mode disconnected default callback behavior is unchanged unless a custom callback is provided, in which case the automatic re-connection will not happen, allowing for users to take advantage of scan results or some other means to determine when and which access point to associate with. The combination of the use of a disconnected callback and `managed` mode allow for the use of `network:wifi_scan/0,1` (PR atomvm#1165), since the wifi must not be connected to a station when perfoming a scan and old implementation always started a connection immediatly and always persisted in attempts to reconnect when disconnected. Signed-off-by: Winford <winford@object.stream>
For more fine grained connection management in applications the driver can now be started, without perfoming an inital connection by the use of the key `managed` in the STA configuration. Adds network:sta_connect/0,1 to allow connecting to an access point after the driver has been started in STA or STA+AP mode. When used without parameters a connection to the last configured access point will be started. Adds network:sta_disconnect/0 to disconnect a station from an access point. The station mode disconnected default callback behavior is unchanged unless a custom callback is provided, in which case the automatic re-connection will not happen, allowing for users to take advantage of scan results or some other means to determine when and which access point to associate with. The combination of the use of a disconnected callback and `managed` mode allow for the use of `network:wifi_scan/0,1` (PR atomvm#1165), since the wifi must not be connected to a station when perfoming a scan and old implementation always started a connection immediatly and always persisted in attempts to reconnect when disconnected. Signed-off-by: Winford <winford@object.stream>
For more fine grained connection management in applications the driver can now be started, without perfoming an inital connection by the use of the key `managed` in the STA configuration. Adds network:sta_connect/0,1 to allow connecting to an access point after the driver has been started in STA or STA+AP mode. When used without parameters a connection to the last configured access point will be started. Adds network:sta_disconnect/0 to disconnect a station from an access point. The station mode disconnected default callback behavior is unchanged unless a custom callback is provided, in which case the automatic re-connection will not happen, allowing for users to take advantage of scan results or some other means to determine when and which access point to associate with. The combination of the use of a disconnected callback and `managed` mode allow for the use of `network:wifi_scan/0,1` (PR atomvm#1165), since the wifi must not be connected to a station when perfoming a scan and old implementation always started a connection immediatly and always persisted in attempts to reconnect when disconnected. Signed-off-by: Winford <winford@object.stream>
Improve connection management for esp32 network driver This PR adds the ability to stop a connection to an access point, and to start a connection to a new, or the last configured access point. The driver can also enable STA mode without performing an initial connection by supplying the `managed` atom in the STA configuration and omitting the `ssid` and `psk` settings. Combined with the network scan feature in #1165 this gives the esp32 platform the ability to act as a roaming device, and connect to any known wifi networks found after performing a wifi scan. The `disconnected` event callback can be used to override the default automatic re-connect action of the driver, and a network scan or other means may now be used to determine if, when, and which access point to connect to. These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
9949f66 to
5555dee
Compare
|
closed by accident. |
cdadd04 to
32969e5
Compare
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
|
Looking good! - did a full sim test, and we fail on all esp32s2, no crash just error - so will investigate that later on real hw - probably just low mem, which code then seemed to handle, eg. it didn't crash (think solution is to lower results or something, from distant memory) let's add a check and network:stop() to simtest test_wifi_scan.erl -module(test_wifi_scan).
-export([start/0]).
start() ->
{ok, _Pid} = network:start([{sta, [managed]}]),
{ok, {Num, Networks}} = network:wifi_scan(),
io:format("network:wifi_scan found ~p networks.~n", [Num]),
lists:foreach(
fun(
_Network =
#{
ssid := SSID,
rssi := DBm,
authmode := Mode,
bssid := BSSID,
channel := Number
}
) ->
io:format(
"Network: ~p, BSSID: ~p, signal ~p dBm, Security: ~p, channel "
"~p~n",
[SSID, binary:encode_hex(BSSID), DBm, Mode, Number]
)
end,
Networks
),
true = lists:any(fun(#{ssid := SSID}) -> SSID =:= <<"Wokwi-GUEST">> end, Networks),
ok = network:stop(),
ok.
LLM review: feel free to pick and choose! think most importantly is the ssid data type charlist vs binary? PR #1165 Review — Add
|
Corrects the type name db() to the more correct `dbm()`, and adds a brief edoc explanation for the value. Signed-off-by: Winford <winford@object.stream>
ESP-IDF 5.1 has been end-of-life since December 2025 and will not receive future security patches. Remove it from the workflow test matrix. Signed-off-by: Winford <winford@object.stream>
Signed-off-by: Winford <winford@object.stream>
Adds documentation for `network:wifi_scan/0,1` and updates details for `network:sta_rssi/0`. Signed-off-by: Winford <winford@object.stream>
Add
network:wifi_scan/0,1to esp32 network driver, giving the ability for devices configured for "station mode" (or with "station + access point mode") to scan for available access points.note: these changes depend on PR #1181
Closes #2024
These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).
SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later