Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmecklem/573c48b62953a63c65f9b2692b0252f2 to your computer and use it in GitHub Desktop.
Save tmecklem/573c48b62953a63c65f9b2692b0252f2 to your computer and use it in GitHub Desktop.

You can mount the SD card on a laptop/desktop and create the /boot/wifi.yml file. /boot/wifi.yml looks something like:

networks:
 - ssid: Home
   psk: wheretheheartis
   priority: 100
 - ssid: Work
   psk: gettinstuffdone
   priority: 99

On boot, the Nerves application looks for the wifi config and copies it over to a place that won't be overwritten on a firmware upgrade, and then uses it to configure the wifi.

Starter code (not quite tested in the unit or battle sense)

  def start_network do
    maybe_config = Path.join("/boot", "wifi.yml")
    if File.exists?(maybe_config) do
      with {:ok, _config} <- YamlElixir.read_from_file(maybe_config),
           :ok <- File.cp(maybe_config, "/root/wifi.yml") do
        File.rm(maybe_config)
      end
    end

    config = "/root/wifi.yml"
    if File.exists?(config) do
      {:ok, wifi} = YamlElixir.read_from_file(config)
      networks =
        wifi["networks"]
        |> Enum.map(fn(network) ->
          [
            ssid: network["ssid"],
            psk: network["psk"],
            priority: Map.get(network, "priority", 1),
            key_mgmt: Map.get(network, "key_mgmt", "WPA-PSK") |> String.to_atom()
          ]
        end)

      settings = [networks: networks]
      Nerves.Network.setup("wlan0", settings)
    end
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment