Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced connection management for esp32 network driver #1181

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

UncleGrumpy
Copy link
Collaborator

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

@@ -74,7 +76,8 @@ gotIp(IpInfo) ->
io:format("Got IP: ~p~n", [IpInfo]).

disconnected() ->
io:format("Disconnected from AP.~n").
io:format("Disconnected from AP, attempting to reconnect~n"),
network:connect().
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need a delay here eg 500-1000ms or something? (ideally exponential backoff - but static delay should be fine..)

Copy link
Collaborator Author

@UncleGrumpy UncleGrumpy Jun 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. This just signals the driver to start attempting a connection, if that attempt succeeds the connected callback will be triggered, otherwise a disconnected callback will be triggered - in which case the attempt will be made again… this would function exactly as the driver always has… by defining a custom callback the user could add any extra delay, or scan for networks instead.

Copy link
Collaborator Author

@UncleGrumpy UncleGrumpy Jun 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only difference is this was connection was called internally, so stop would never be possible. Now by defining a callback the user can control when a reconnection (or now a new connection) will happen.

@UncleGrumpy UncleGrumpy force-pushed the wifi_roaming branch 2 times, most recently from 246d3b5 to ae6e359 Compare August 19, 2024 19:21
@UncleGrumpy UncleGrumpy changed the base branch from release-0.6 to main November 20, 2024 06:51
@UncleGrumpy UncleGrumpy changed the title Support roaming for esp32 network driver Enhanced connection management for esp32 network driver Nov 20, 2024
@UncleGrumpy UncleGrumpy force-pushed the wifi_roaming branch 2 times, most recently from d95fdf5 to 53a6dc7 Compare December 22, 2024 03:50
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 <[email protected]>
Adds descriptions of new configuration options and connect/0,1
and disconnect/0 functions.

Signed-off-by: Winford <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants