Current section

Files

Jump to
nerves_livebook priv samples networking configure_wifi.livemd
Raw

priv/samples/networking/configure_wifi.livemd

# Configure WiFi
```elixir
Mix.install([
{:vintage_net_wifi, "~> 0.11"},
{:kino, "~> 0.12"}
])
```
## Finding access points
Nerves uses [vintage_net_wifi](https://hexdocs.pm/vintage_net_wifi/) to
configure WiFi networking. This notebook uses the `quick_` functions to
simplify common tasks. If you're trying to connect to an enterprise network,
this may not work for you.
Step 1 is to see what WiFi networks are available.
`VintageNetWiFi.quick_scan/0` returns a lot of information, so lets just show a
few columns in a table. Rerun this if you don't see your network.
```elixir
networks = VintageNetWiFi.quick_scan()
networks
|> Kino.DataTable.new(
name: "WiFi Networks",
keys: [:ssid, :signal_percent, :frequency, :flags],
sorting_enabled: false
)
```
#### WPA3 support
A WiFi network that uses WPA3 shows up with `:sae` in the flags. Support for
WPA3 networks depends on hardware and device driver support. Raspberry Pi 4 and
5 support WPA3.
## Connect
Next, lets connect to one of the WiFi networks. Select the SSID and enter the
password.
```elixir
selectable_networks =
[{"", "SSIDs:"}] ++ Enum.map(networks, fn network -> {network.ssid, network.ssid} end)
selected_network =
Kino.Input.select("Select WiFi Network", selectable_networks)
|> Kino.render()
psk_input = Kino.Input.password("Enter WiFi password")
```
Verify that the information is correct and then evaluate the following code block to set it.
```elixir
ssid = Kino.Input.read(selected_network) |> String.trim()
psk = Kino.Input.read(psk_input) |> String.trim()
if ssid != "" do
VintageNetWiFi.quick_configure(ssid, psk)
else
IO.puts("Skipping WiFi configuration.")
end
```
## Check the connection
The final step is to check whether everything worked. `VintageNet.info/0` is an
easy way of checking overall network connectivity on a device, so run it.
Hopefully, you'll see a section for `"wlan0"` and a connection status of
`:internet`. You may need to scroll down.
```elixir
VintageNet.info()
```
## Next up...
See [VintageNet](/learn/notebooks/vintage-net) to learn more about networking in Nerves.