My first Nerves Project pt. 2

My first Nerves Project pt. 2


In my previous post I outlined my first step in the world of Nerves. I got to the point where I could read the temperature of 2 sensors and print it to a screen connected to my Raspberry Pi by HDMI.

Next step - UI

Ultimately I would like to have the rpi send the sensor data to a remote Phoenix server. But a nice step-stone to the goal is having a local Phoenix server displaying the temperatures.

Luckily there's a guide for just that in the Nerves docs! So I copied my project into a new folder, added a Phoenix project and added it as a dependency to the Nerves project (poncho structure).

I then configured networking as described in the guide.

This article described a lot of the fundamental stuff of networking with Nerves and I drew some inspiration from it as well.

Connecting to my Pi for the first time

When I had burned the SD-card with the networking code I watched the monitor output the IP of the rpi. 🎉 Great success! 🎉

On my laptop I navigated to port 80 of the IP-address in my browser and saw the Phoenix start page. Sweet joy! 😍

Getting the temperatures remotely

Before I began sending the data to Phoenix I wanted to see if I could get the state of my GenServer remotely. So I added a handle_call and a public facing function get_temps/0

defmodule Extemp.Temperature do


  def handle_call(:get_temps, _from, state) do
    {:reply, state[:temperatures], state}


  def get_temps() do, :get_temps)


Using init_gadget I set a name for the node that I could use when connecting.

## mix.exs

config :nerves_init_gadget,
  node_name: :target01,
  mdns_domain: "nerves.local",
  address_method: :dhcp,
  ifname: "wlan0"

Spinning up iex with the cookie from rel/vm.args I connected to the pi.

iex --name host@ --cookie "super_long_cookie"

Then we can connect with Node.connect

Interactive Elixir (1.6.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(host@> Node.connect(:"target01@nerves.local")

Now for the goal of this little exercise:

iex(host@>"target01@nerves.local",Extemp.Temperature, :get_temps, [])
[%{sensor_0: 18.875}, %{sensor_1: 18.625}]

We can get temperatures remotely! 🙌

This is a good place to be for future development. Stay tuned.