Skip to content

Commit 4c8b954

Browse files
committed
Add network:wifi_scan/0,1 to esp32 network driver
Signed-off-by: Winford <winford@object.stream>
1 parent b786dbd commit 4c8b954

File tree

9 files changed

+985
-19
lines changed

9 files changed

+985
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ encoding/decoding options, also Elixir `(url_)encode64`/`(url_)decode64` have be
8787
- Added `nanosecond` and `native` time unit support to `erlang:system_time/1`, `erlang:monotonic_time/1`, and `calendar:system_time_to_universal_time/2`
8888
- Added `erlang:system_time/0`, `erlang:monotonic_time/0`, and `os:system_time/0,1` NIFs
8989
- Added `filename:join/1` and `filename:split/1`
90+
- Added `network:wifi_scan/0,1` to ESP32 network driver to scan available APs when in sta or sta+ap mode.
9091

9192
### Changed
9293

examples/erlang/esp32/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ pack_runnable(reformat_nvs reformat_nvs eavmlib avm_esp32)
3838
pack_runnable(uartecho uartecho eavmlib estdlib avm_esp32)
3939
pack_runnable(ledc_example ledc_example eavmlib estdlib avm_esp32)
4040
pack_runnable(epmd_disterl epmd_disterl eavmlib estdlib avm_network avm_esp32)
41+
pack_runnable(wifi_scan wifi_scan estdlib eavmlib avm_network avm_esp32)
42+
pack_runnable(wifi_scan_callback wifi_scan_callback estdlib eavmlib avm_network avm_esp32)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
%% This file is part of AtomVM.
2+
%%
3+
%% Copyright (c) 2023 <winford@object.stream>
4+
%% All rights reserved.
5+
%%
6+
%% Licensed under the Apache License, Version 2.0 (the "License");
7+
%% you may not use this file except in compliance with the License.
8+
%% You may obtain a copy of the License at
9+
%%
10+
%% http://www.apache.org/licenses/LICENSE-2.0
11+
%%
12+
%% Unless required by applicable law or agreed to in writing, software
13+
%% distributed under the License is distributed on an "AS IS" BASIS,
14+
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
%% See the License for the specific language governing permissions and
16+
%% limitations under the License.
17+
%%
18+
%%
19+
%% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
20+
%%
21+
22+
-module(wifi_scan).
23+
24+
-export([start/0]).
25+
26+
start() ->
27+
{ok, _Pid} = network:start([{sta, [managed]}]),
28+
scan_passive([show_hidden, {dwell, 1000}]),
29+
scan_active([{dwell, 500}]).
30+
31+
scan_active(Config) ->
32+
io:format(
33+
"~nStarting active scan with configuration ~p, this may take some time depending on dwell ms used.~n~n",
34+
[Config]
35+
),
36+
BeginTime = erlang:monotonic_time(millisecond),
37+
{ok, {Num, Networks}} = network:wifi_scan(Config),
38+
io:format("Active scan found ~p networks in ~pms.~n", [
39+
Num, erlang:monotonic_time(millisecond) - BeginTime
40+
]),
41+
format_networks(Networks).
42+
43+
scan_passive(Config) ->
44+
io:format(
45+
"~nStarting passive scan with configuration: ~p, this may take some time depending on dwell ms used.~n~n",
46+
[Config]
47+
),
48+
Opts = [passive | Config],
49+
BeginTime = erlang:monotonic_time(millisecond),
50+
ScanResults = network:wifi_scan(Opts),
51+
{ok, {Num, Networks}} = ScanResults,
52+
io:format("Passive scan found ~p networks in ~pms.~n", [
53+
Num, erlang:monotonic_time(millisecond) - BeginTime
54+
]),
55+
format_networks(Networks).
56+
57+
format_networks(Networks) ->
58+
lists:foreach(
59+
fun(
60+
_Network = #{
61+
ssid := SSID, rssi := DBm, authmode := Mode, bssid := BSSID, channel := Number
62+
}
63+
) ->
64+
io:format(
65+
"Network: ~p, BSSID: ~p, signal ~p dBm, Security: ~p, channel ~p~n",
66+
[SSID, binary:encode_hex(BSSID), DBm, Mode, Number]
67+
)
68+
end,
69+
Networks
70+
).
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
%% This file is part of AtomVM.
2+
%%
3+
%% Copyright (c) 2023 <winford@object.stream>
4+
%% All rights reserved.
5+
%%
6+
%% Licensed under the Apache License, Version 2.0 (the "License");
7+
%% you may not use this file except in compliance with the License.
8+
%% You may obtain a copy of the License at
9+
%%
10+
%% http://www.apache.org/licenses/LICENSE-2.0
11+
%%
12+
%% Unless required by applicable law or agreed to in writing, software
13+
%% distributed under the License is distributed on an "AS IS" BASIS,
14+
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
%% See the License for the specific language governing permissions and
16+
%% limitations under the License.
17+
%%
18+
%%
19+
%% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
20+
%%
21+
22+
-module(wifi_scan_callback).
23+
24+
-export([start/0]).
25+
26+
start() ->
27+
Config = [{sta, [managed, {scan_done, fun display_scan_results/1}]}],
28+
{ok, _Pid} = network:start(Config),
29+
io:format(
30+
"~nStarting active scan with configuration ~p, this may take some time depending on dwell ms used.~n~n",
31+
[Config]
32+
),
33+
case network:wifi_scan() of
34+
{error, Reason} ->
35+
io:format("wifi_scan failed for reason ~p", [Reason]);
36+
ok ->
37+
timer:sleep(infinity)
38+
end.
39+
40+
display_scan_results(Results) ->
41+
case Results of
42+
{error, Reason} ->
43+
io:format("wifi_scan failed for reason ~p.~n", [Reason]);
44+
{Num, Networks} ->
45+
io:format("wifi_scan callback got ~p results:~n", [Num]),
46+
lists:foreach(
47+
fun(
48+
_Network = #{
49+
ssid := SSID,
50+
rssi := DBm,
51+
authmode := Mode,
52+
bssid := BSSID,
53+
channel := Number
54+
}
55+
) ->
56+
io:format(
57+
"Network: ~s, BSSID: ~s, signal ~p dBm, Security: ~p, channel ~p~n",
58+
[SSID, binary:encode_hex(BSSID), DBm, Mode, Number]
59+
)
60+
end,
61+
Networks
62+
)
63+
end.

0 commit comments

Comments
 (0)