LoginSignup
1
2

More than 5 years have passed since last update.

プログラム生存確認のLチカ、ボタン一発でshutdownで、LCDなしでも簡単IoT化

Last updated at Posted at 2017-10-22

Overview

Raspberry Piで、自前IoTを作っていると、Linuxが故に、ちゃんとshutdownしたいが、SSHしたり、HDMI+Keyboardとかを使うのは面倒。

また、LCDがない場合でも、自分のプログラムなど特定のプログラムがちゃんと動いているかどうかを知りたい。

というわけで、作りました。

まず、コア部分の説明 : プロセスの存在確認

def isProcessExisting(command):
    found = False
    if os.path.exists(ProcessWatcher.PATH_PROC):
        pids = [pid for pid in os.listdir(ProcessWatcher.PATH_PROC) if pid.isdigit()]
        for pid in pids:
            try:
                cmd = open(os.path.join(ProcessWatcher.PATH_PROC, pid, "cmdline"), "rb").read()
                found |= (command in cmd)
                if found:
                    break
            except IOError:
                continue

    return found

このようにして、指定されたコマンドを含むプロセスが存在するかどうかを確認します。

作ったもの

特定用途向けに作り込ずに、今回は、汎用化してありますので、configファイルでいろいろなシチュエーションに対応できるかと思います。

config.json
{
    "button_shutdown":{
        "port": 18,
        "pull-up": true,
        "active": false,
        "execute": "shutdown -h now",
        "timeout": 0
    },
    "button_reboot":{
        "port": 19,
        "pull-up": true,
        "active": false,
        "execute": "shutdown -r now",
        "timeout": 0
    },
    "process_apache2":{
        "port": 23,
        "active": true,
        "command": "apache2",
        "onFound": "",
        "onLost": "service restart apache2",
        "timeout": 0
    }
}

上のようにjsonファイルで、挙動を指定します。

上の例は、
* GPIO:18にスイッチを繋いで、Switchが押されたらGNDに落とす回路の場合に、 shutdown -h now を実行します(実際には押してから離した時)
* 同様に、19は、 shutdown -r now です
* apache2 の実行ファイル名を含むprocessがいなくなった場合に、 service restart apache2 を実行します。

考えられる利用例

  • 一定電圧以下になったら、電源終了 (コンパレーターの結果をGPIに入れれば良い)
  • ボタンを押したら、mail通知 ( echo hoge | mail hoge@hoge.com )
  • 自分のプログラムが動いているかどうかをLEDで知る
1
2
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
1
2