Execute GPIO functions on the production server.

This commit is contained in:
Pim Kunis 2019-08-24 16:51:43 +02:00
parent fc54fa1761
commit 66291ac548
4 changed files with 33 additions and 23 deletions

View file

@ -21,11 +21,10 @@ defmodule Firmware.Application do
# List all child processes to be supervised # List all child processes to be supervised
def children(:host) do def children(:host) do
[ {:ok, _} = Node.start(:"host@0.0.0.0")
# Children that only run on the host Node.set_cookie(:testcookie)
# Starts a worker by calling: Firmware.Worker.start_link(arg) true = Node.connect(:"esrom@nerves.local")
# {Firmware.Worker, arg}, []
]
end end
def children(_target) do def children(_target) do

View file

@ -63,7 +63,7 @@ defmodule Firmware.MixProject do
def release do def release do
[ [
overwrite: true, overwrite: true,
cookie: "#{@app}_cookie", cookie: "testcookie",
include_erts: &Nerves.Release.erts/0, include_erts: &Nerves.Release.erts/0,
steps: [&Nerves.Release.init/1, :assemble] steps: [&Nerves.Release.init/1, :assemble]
] ]

View file

@ -1,6 +1,4 @@
defmodule Morse.Worker do defmodule Morse.Worker do
alias Circuits.GPIO
@moduledoc """ @moduledoc """
Functions to control the signal lamp connected with GPIO. Functions to control the signal lamp connected with GPIO.
""" """
@ -24,32 +22,31 @@ defmodule Morse.Worker do
update_progress(0, 100) update_progress(0, 100)
{:ok, gpio} = GPIO.open(relay_pin(), :output) toggle_lamp(@off)
GPIO.write(gpio, @off)
Process.sleep(@sleep_start) Process.sleep(@sleep_start)
code code
|> Enum.with_index() |> Enum.with_index()
|> Enum.each(&signal_symbol(gpio, &1, code_length)) |> Enum.each(&signal_symbol(&1, code_length))
update_progress(100, 100) update_progress(100, 100)
end end
defp signal_symbol(gpio, {?., _index}, _length) do defp signal_symbol({?., _index}, _length) do
GPIO.write(gpio, @on) toggle_lamp(@on)
Process.sleep(@sleep_short) Process.sleep(@sleep_short)
GPIO.write(gpio, @off) toggle_lamp(@off)
Process.sleep(@sleep_delay) Process.sleep(@sleep_delay)
end end
defp signal_symbol(gpio, {?-, _index}, _length) do defp signal_symbol({?-, _index}, _length) do
GPIO.write(gpio, @on) toggle_lamp(@on)
Process.sleep(@sleep_long) Process.sleep(@sleep_long)
GPIO.write(gpio, @off) toggle_lamp(@off)
Process.sleep(@sleep_delay) Process.sleep(@sleep_delay)
end end
defp signal_symbol(_gpio, {?\s, index}, length) do defp signal_symbol({?\s, index}, length) do
Process.sleep(@sleep_pause) Process.sleep(@sleep_pause)
update_progress(index, length) update_progress(index, length)
end end
@ -58,12 +55,27 @@ defmodule Morse.Worker do
Morse.Server.update_progress(index / length * 100) Morse.Server.update_progress(index / length * 100)
end end
defp relay_pin() do
Application.fetch_env!(:morse, :relay_pin)
end
defp secret_code do defp secret_code do
Application.fetch_env!(:morse, :morse_message) Application.fetch_env!(:morse, :morse_message)
|> String.to_charlist() |> String.to_charlist()
end end
case Mix.Nerves.Utils.mix_target() do
:host ->
def toggle_lamp(state) do
:rpc.call(:"esrom@nerves.local", Morse.Worker, :toggle_lamp, [state])
end
_ ->
alias Circuits.GPIO
def toggle_lamp(state) do
{:ok, gpio} = GPIO.open(relay_pin(), :output)
GPIO.write(gpio, state)
end
defp relay_pin() do
Application.fetch_env!(:morse, :relay_pin)
end
end
end end

View file

@ -32,7 +32,6 @@ defmodule UiWeb.UserSocket do
def id(_socket), do: nil def id(_socket), do: nil
def broadcast_progress(progress) do def broadcast_progress(progress) do
IO.puts("in user socket!")
UiWeb.Endpoint.broadcast("morse:progress", "update", %{value: progress}) UiWeb.Endpoint.broadcast("morse:progress", "update", %{value: progress})
end end
end end