Getting Started with Nerves and Elixir

Cade Ward
Cade Ward
Sep 1, 2018 · 2 min read
“yellow and blue data code displayed on screen” by Markus Spiske on Unsplash

As I’ve been playing with Nerves, I’ve been impressed by how simple it is to use. If you enjoy programming in Elixir and have some hardware projects in mind, give Nerves a try.

Getting Started

It is easy to get started with Nerves. You’ll want to head over to their docs to install a few one-time tools. Once that’s done, you can use mix to start a new project.

mix nerves.new project_name --init-gadget

Nerves will generate a whole structure for you and output some instructions. It is important to note that everything needs to be done under the correct target. Head over to the docs to see what targets are supported. Once you’re ready, cd into your directory and install the dependencies for your target. I’ll use a Raspberry Pi 3 for the examples.

MIX_TARGET=rpi3 mix deps.get

Plug your sd card into the computer and build a firmware, then burn it to the card.

MIX_TARGET=rpi3 mix firmware
MIX_TARGET=rpi3 mix firmware.burn

Once that is done, you can plug the sd card into the Raspberry Pi and you’re done. That was incredibly easy! It gets even better.

Over-the-Air Firmware Updates

My favorite part about Nerves so far has been how easy it is to update your device over-the-air. Build your firmware, then instead of burning it, push it up to your device:

MIX_TARGET=rpi3 firmware.push nerves.local

It just so happens there is a small package that you can install into your new nerves project that gives you all sorts of conveniences, including over-the-air updates. The package is called nerves_init_gadget. It turns out Nerves comes with a generator for the package. We used it above when we typed --init-gadget to create our new project.

If you want to add this to an existing project, check out their docs.

nerves_init_gadget assumes some defaults in order to work:

  • you have the default id_rsa key in $HOME/.ssh.
  • you’re getting internet via usb

I wanted to use wifi, so I added a network config in config/config.exs:

key_mgmt = System.get_env("NERVES_NETWORK_KEY_MGMT") || "WPA-PSK"config :nerves_network, :default,
wlan0: [
ssid: System.get_env("NERVES_NETWORK_SSID"),
psk: System.get_env("NERVES_NETWORK_PSK"),
key_mgmt: String.to_atom(key_mgmt)
]

Note: if you aren’t able to connect to the network with the defaults, you’ll need to build a new firmware and burn it manually to the sd card again. Once you’re online, you can forever push firmware updates over the air.

Once that’s all setup, you should be able to ssh into your device.

ssh nerves.local

That should put you into an iex session on the device.

Conclusion

We now have a device that can join our network automatically, make its self discoverable with an easy name, secure ssh access, over-the-air firmware updates, and more.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade