Try   HackMD

RaspberryPiでNode-RED (起動時実行編)

Node-REDが起動したときに、フローを走らせるにはどうしたらよいのだろう?

injectノードを使う

injectノードに、起動時にトリガーを発生させる設定があります。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

これで、Node-REDが起動して、0.1秒後にinjectノードのトリガーが発生し、このフローが実行されます。

試しに、Node-REDが起動したときに、Mattermostに「Node-REDが起動しました!」とメッセージを送信するフローを作成しました。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

ビルドすると、再起動するようですぐにMattermostにメッセージが送られました。

RaspberryPiを再起動した時にも、メッセージが送られてくることを確認しました。

定期的に実行

injectノードの設定に、「繰り返し」という設定があります。
ここを設定すると、Node-REDが起動後に指定した設定で繰り返し実行ができそうです。

繰り返しの設定項目

  • なし
    起動したときに1回だけ実行。
  • 指定した時間間隔
    指定した時間ごとに実行。
    (例:1分ごと、30秒ごと、24時間ごと)
  • 指定した時間間隔、日時
    指定曜日の決められた時間の範囲の中で、設定時間(分)ごとに実行。
    (例:月、水、金の、10:00~15:30の間に、5分毎に実行)
  • 指定した日時
    指定曜日の指定時間に実行。
    (例:月~金の、12:05に実行)

rpi gpio in ノードを使う

RaspberryPiのGPIOの入力PINの状態変化をトリガーにする、「rpi gpio in」ノードを使うこともできそうです。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

「Read initial state of pin on deploy/restart?」にチェックを入れると、Node-RED起動時に、指定のGPIOピンを読み込みトリガーが発生します。

前述の、injectノードより早く実行されるようです。
injectノードは、起動後0.1秒後に実行するように設定してあったためかもしれません。