0
0

More than 3 years have passed since last update.

サーマルカメラ(サーモ AI デバイス TiD) Python スイッチ編

Last updated at Posted at 2021-01-10

Raspberry Piのシャットダウンと、再起動が出来るスイッチを制御します。


  1. 紹介編
  2. センサ編
  3. センサケース編
  4. Raspberry Pi編
  5. Python編
    5.1 Form編
    5.2 オムロン 非接触温度センサ D6T-44L-06編
    5.3 Pololu 測距センサ VL53L0X編
    5.4 BOSCH 温湿度・気圧センサ BME280
    5.5 シャットダウン・再起動スイッチ編
    5.6 OpenCV編
    5.7 高速化編

再起動スイッチ配線

reboot_sw_wiring.jpg
再起動スイッチ(a接点)をGPIO3とGNDに接続します。
Raspberry Piをシャットダウンし、電源が入っている状態で再起動スイッチを押すと、 Raspberry Piが再起動します。

シャットダウンスイッチ配線

shutdown_sw_wiring.jpg
シャットダウンスイッチ(a接点)をGPIO14とGNDに接続します

pigpioの準備

最近のOSではraspi-configを用いて、「9 Advanced Options」から入り、「AB GPIO Server」を選択すれば、pigpioが利用できます。
有効にできない場合は、下記コマンドでインストールしてください。

sudo apt-get update
sudo apt-get install pigpio

pigpioをサービスで動かしておくと便利です。

  • サービスの確認
sudo nano /lib/systemd/system/pigpiod.service
  • サービスの有効化
sudo systemctl enable pigpiod
sudo systemctl start pigpiod
sudo systemctl status pigpiod

シャットダウンプログラム

shutdown.pyをRaspberry Piのフォルダにコピーします。/home/pi直下で良いと思います。
shutdown.py (ZIPで圧縮済)

#!/usr/bin/python
# coding:utf-8
"""
Raspberry Pi Shutdown script.
=============================
"""
import os
rp_mode = False
try:
    import pigpio
    rp_mode = True
except ModuleNotFoundError:
    rp_mode = False
import sys
import time


def main():
    if rp_mode == False:
        return

    gpionumber = 14
    shutdown_time = 3
    wait_time = 0.5

    args = sys.argv
    if len(args) > 1:
        shutdown_time = float(args[1])

    pi = pigpio.pi()
    pi.set_mode(gpionumber, pigpio.INPUT)

    while True:
        counter = 0

        while True:
            if pi.read(gpionumber) == 0:
                counter = counter + 1
                if counter >= (shutdown_time / wait_time):
                    os.system('sudo shutdown -h now')
                    #print('sudo shutdown -h now')
                    break
            time.sleep(wait_time)

if __name__ == '__main__':
    main()

シャットダウンプログラムのサービス化

shutdownbuttond.serviceファイルを作成します。

sudo nano /usr/lib/systemd/system/shutdownbuttond.service

shutdownbuttond.serviceファイルの内容は下記です。
python3、shutdown.pyの場所は適宜変更してください。

[Unit]
Description=Shutdown Daemon

[Service]
ExecStart=/usr/bin/python3 /home/pi/pyhome/shutdownd.py
Restart=always
Type=simple

[Install]
WantedBy=multi-user.target

サービスに登録します。

sudo systemctl enable shutdownbuttond.service
sudo systemctl daemon-reload

サービスが登録されているか確認します。

sudo systemctl status shutdownbuttond.service

Activeと言う文字があれば、正常に起動しています。

* shutdownbuttond.service - Shutdown Daemon
   Loaded: loaded (/usr/lib/systemd/system/shutdownbuttond.service; enabled; ven
   Active: active (running) since Sun 2020-11-01 13:45:21 JST; 2h 10min ago
 Main PID: 354 (python3)
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/shutdownbuttond.service
           -- 354 /usr/bin/python3 /home/pi/shutdown.py

**月 ** 13:45:21 user systemd[1]: Started Shutdown Daemon.

再起動して、シャットダウンできるか確認します。


YouTube: サーマルカメラ(サーモ AI デバイス TiD) Python編
web: サーモ AI デバイス TiD Python スイッチ編 (URLが変更されました)

0
0
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
0
0