1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

デスクトップPCの電源スイッチをラズパイで操作し起動させる

Posted at

概要

デスクトップPCに付属している電源ボタンの回路を、ラズパイで電気的に横取り(?)し操作します。
ラズパイに外部からsshするなり、Webフロントエンドを適当に用意するなりすれば、リモートでデスクトップPCの電源を入れることができます。
回路構成図とPythonの実装サンプルを記載します。

  • WakeOnLan非対応でも可能
  • 電源ボタン長押しも再現できるため、PCの強制再起動までもリモートで実行可能

構成

2024-04-30_23.32.37.png

ラズパイGPIO図 Raspberry Pi OS - Raspberry Pi Documentation より
フォトカプラ図 PS817データシートより

スクリプト

下記スクリプトを~/make-pulse.pyに設置

#!/usr/bin/env python3
# デスクトップPCのスイッチをラズパイで横取りすることでリモート起動させる
#
# Usage
# ./make-pulse.py <PIN> <DURATION_SECONDS>
#
# e.g.
# ./make-pulse.py 23 0.3
#

import RPi.GPIO as GPIO
import sys
import time

args = sys.argv

pin = int(args[1])
duration_seconds = float(args[2])

GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT, initial=GPIO.LOW)

# スイッチ押下
# (GPIO<PIN>の出力をオンにして、<DURATION_SECONDS>後、オフにする)
GPIO.output(pin, GPIO.HIGH)
time.sleep(duration_seconds)
GPIO.output(pin, GPIO.LOW)

GPIO.cleanup()

実行権限を付与

chmod +x ~/make-pulse.py

PCが起動するかテスト

./make-pulse.py 23 0.3

おまけ

iPhoneから起動させる

iOSの「ショートカット」アプリ経由で実行できるととても便利です。ラズパイへSSHできる設定がされているとして、下記のように設定します。

  • アプリを起動
  • 右上「+」を押下し新規ショートカット
  • アクションを追加
  • 「SSH経由でスクリプトを実行」を選択
    • スクリプト、ホスト、ユーザ、認証(パスワード・SSHキー)を入力

スクリプトは下記のように、先程のスクリプトの起動コマンドを入力します

~/make-pulse.py

ウィジェットなどで設定しておくと、リモコン感覚で起動できました。

参考

1
4
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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?