Recap.

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}
  end

...

  def get_temps() do
    GenServer.call(__MODULE__, :get_temps)
  end

end

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@0.0.0.0 --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@0.0.0.0)1> Node.connect(:"target01@nerves.local")
true

Now for the goal of this little exercise:

iex(host@0.0.0.0)2> :rpc.call(:"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.