7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Nervesで、他のノードのLEDをON/OFFする

7
Posted at

はじめに

Elixir合宿 in 湯布院 で作成したものです

作ったもの

2台のRaspPi ZeroWを用意して、一台目にスイッチ、二台目にLEDを接続。

一台目のスイッチを押すと、二台目のLEDが点灯する。

image.png

プログラム

通信できるようにする準備の方法は、Elixir・Nervesで複数のラズパイと通信したいを参考にしまいた。


defmodule Node01 do
def input_loop do
  {:ok, switch} = Circuits.GPIO.open(17,:input)
  loop(switch)
end

def loop(switch) do
  x = Circuits.GPIO.read(switch)
  Node.spawn(:"nerves@192.168.77.65",fn -> Node01.led(x) end)
  Process.sleep(100)
  loop(switch)
end

def remote_start() do
  System.cmd("epmd", ["-daemon"])
  Node.start(:"nerves@192.168.77.6")
  Node.set_cookie(:securecookie)
  Node.connect(:"nerves@192.168.77.65")
end

def node_02_remote_start() do
  System.cmd("epmd", ["-daemon"])
  Node.start(:"nerves@192.168.77.65")
  Node.set_cookie(:securecookie)
end

def led(x) do
  {:ok, led} = Circuits.GPIO.open(18,:output)
  Circuits.GPIO.write(led, x)
end

end

一台目の方では、
remote_startとinput_loopを実行、
node_02_remote_start
を実行します。

ハマったところ

Node.startやNode.connectの接続先の指定で"nerves@192.168.77.6"のようにIPアドレスを指定した記述にする必要がありました。
nerves01などで名前解決できるようにしておいて、
Node.start(:"nerves01")
のような記述で指定してみたんですが、これではうまくいきませんでした。
なにか方法はあると思うのですが、未解決です。

まとめ

Node.spawnを使うことで、別のノードで関数を実行する事ができる事を確認できた。
Node.spawnでLEDのON/OFFする関数を指定してLED制御ができた
プログラム中に、IPアドレスを直接記述する必要があった。記述をしなくてもできるようにするのは今後の課題

参考
https://qiita.com/kikuyuta/items/c402d594ab55cbd883e7
https://qiita.com/mnishiguchi/items/42785af280b378187c19

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?