Skip to content

Commit fa979b6

Browse files
mnishiguchifhunleth
authored andcommitted
Add support for provisioning WiFi
1 parent 15c58d7 commit fa979b6

File tree

5 files changed

+96
-2
lines changed

5 files changed

+96
-2
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ Success!
8888
Elapsed time: 3.595 s
8989
```
9090

91+
If you're using a WiFi-enabled device and want the WiFi credentials to be
92+
written to the MicroSD card, initialize the MicroSD card like this instead:
93+
94+
```sh
95+
export NERVES_WIFI_SSID='access_point'
96+
export NERVES_WIFI_PASSPHRASE='passphrase'
97+
fwup circuits_quickstart_rpi0.fw
98+
```
99+
100+
You can still change the WiFi credentials at runtime using
101+
`VintageNetWiFi.quick_configure/2`, but this helps you don't have an easy
102+
alternative way of accessing the device to configure WiFi.
103+
91104
It's quite fast. Now you have Nerves ready to run on your device. Skip ahead to
92105
the next section.
93106

@@ -98,6 +111,10 @@ follow the prompts:
98111

99112
![etcher screenshot](assets/etcher.png)
100113

114+
IMPORTANT: There's no way to configure the initial WiFi credentials with
115+
`etcher`. If you have a device that you can only access via WiFi (so no way of
116+
setting credentials), then check out the `fwup` instructions above.
117+
101118
## GRiSP 2 installation
102119

103120
GRiSP 2 support is VERY new. While it should be safe, it's probably a good idea
@@ -379,7 +396,7 @@ more information:
379396
* [SPI](https://hex.pm/packages/circuits_spi)
380397
* [UART](https://hex.pm/packages/circuits_uart)
381398

382-
At some point you'll want to create your own firmware. See the [Nerves
399+
At some point you may want to create your own firmware. See the [Nerves
383400
Installation](https://hexdocs.pm/nerves/installation.html) and [Getting
384401
Started](https://hexdocs.pm/nerves/getting-started.html) guides for details.
385402

config/config.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ config :circuits_quickstart, target: Mix.target()
1313
# Customize non-Elixir parts of the firmware. See
1414
# https://hexdocs.pm/nerves/advanced-configuration.html for details.
1515

16-
config :nerves, :firmware, rootfs_overlay: "rootfs_overlay"
16+
config :nerves, :firmware,
17+
rootfs_overlay: "rootfs_overlay",
18+
provisioning: "config/provisioning.conf"
1719

1820
# Set the SOURCE_DATE_EPOCH date for reproducible builds.
1921
# See https://reproducible-builds.org/docs/source-date-epoch/ for more information

config/provisioning.conf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Support setting the initial WiFi credentials when creating MicroSD cards.
2+
#
3+
# Note that the '$' is escaped so that environment variable replacement happens
4+
# when firmware is written to the MicroSD card rather than when the '.fw' file
5+
# is created. WiFi credentials are not stored in the .fw file. If left blank,
6+
# the device won't connect to WiFi and you'll need to configure it in this Mix
7+
# project or via an iex prompt.
8+
#
9+
# IMPORTANT: If you configure WiFi in this Mix project or via the IEx prompt,
10+
# that configuration will be persisted and these won't be used unless
11+
# $NERVES_WIFI_FORCE is set to "true" (or anything)
12+
uboot_setenv(uboot-env, "wifi_ssid", "\${NERVES_WIFI_SSID}")
13+
uboot_setenv(uboot-env, "wifi_passphrase", "\${NERVES_WIFI_PASSPHRASE}")
14+
uboot_setenv(uboot-env, "wifi_force", "\${NERVES_WIFI_FORCE}")
15+
16+
# Normally the serial number comes from a unique device ID on the board. Use
17+
# this to override the default serial number. The serial number is used to
18+
# determine the hostname so overriding the serial number can be convenient for
19+
# naming devices.
20+
uboot_setenv(uboot-env, "nerves_serial_number", "\${NERVES_SERIAL_NUMBER}")
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
defmodule CircuitsQuickstart.Application do
2+
# See https://hexdocs.pm/elixir/Application.html
3+
# for more information on OTP Applications
4+
@moduledoc false
5+
6+
use Application
7+
8+
def start(_type, _args) do
9+
if target() != :host do
10+
setup_wifi()
11+
end
12+
13+
# See https://hexdocs.pm/elixir/Supervisor.html
14+
# for other strategies and supported options
15+
opts = [strategy: :one_for_one, name: CircuitsQuickstart.Supervisor]
16+
17+
children = []
18+
19+
Supervisor.start_link(children, opts)
20+
end
21+
22+
defp setup_wifi() do
23+
kv = Nerves.Runtime.KV.get_all()
24+
25+
if true?(kv["wifi_force"]) or wlan0_unconfigured?() do
26+
ssid = kv["wifi_ssid"]
27+
passphrase = kv["wifi_passphrase"]
28+
29+
unless empty?(ssid) do
30+
_ = VintageNetWiFi.quick_configure(ssid, passphrase)
31+
:ok
32+
end
33+
end
34+
end
35+
36+
defp wlan0_unconfigured?() do
37+
"wlan0" in VintageNet.configured_interfaces() and
38+
VintageNet.get_configuration("wlan0") == %{type: VintageNetWiFi}
39+
end
40+
41+
defp true?(""), do: false
42+
defp true?(nil), do: false
43+
defp true?("false"), do: false
44+
defp true?("FALSE"), do: false
45+
defp true?(_), do: true
46+
47+
defp empty?(""), do: true
48+
defp empty?(nil), do: true
49+
defp empty?(_), do: false
50+
51+
defp target() do
52+
Application.get_env(:circuits_quickstart, :target)
53+
end
54+
end

mix.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ defmodule CircuitsQuickstart.MixProject do
3333

3434
def application do
3535
[
36+
mod: {CircuitsQuickstart.Application, []},
3637
extra_applications: [:logger, :runtime_tools, :inets, :ssl]
3738
]
3839
end

0 commit comments

Comments
 (0)