Raspberry Piを使いPCモニターの入力切り替えを出来るようにする。
OSは、Raspberry Pi対応のIoTプラットフォームopenHABのOS「openHABian」を使います。
PCモニターの制御にはLinux プログラムのdcutilを使います。
dcutilは、モニターのボタンを押して設定できる設定を変更できます。
基本的にスマホなどからweb経由で入力切り替えしますが、Raspberry PiとopenHABを使っているので、工夫次第でリモコンで入力切り替えなども出来るはずです。
目次
必要なもの
- Raspberry Pi 4(推奨)
- Micro HDMIケーブル
openHABとddcutilに対応したRaspberry Piが必要です。
Raspberry Pi 4(推奨)、Raspberry Pi 3、Raspberry Pi 2
PCモニターを制御するためにRaspberry PiをHDMIで接続する必要があります。
DDC/CIを使用して設定変更するのでモニターのDDC/CIを有効にします。
OS「openHABian」の準備
Raspberry Pi Imager などでOS「openHABian」を書き込みます。
openHABianのセットアップ
openHABianのセットアップには別のPCやスマホのWebブラウザからopenHABianにアクセスします。
openHABian
http://openhabian:8080/
openHABianにアドオン「Exec Binding」を追加
このアドオンは任意のシェルコマンドを実行する機能を追加します。
https://www.openhab.org/addons/bindings/exec/
モニター制御ソフト「ddcutil」のインストール
ddcutilはモニターの入力切り替え、輝度制御などが出来るモニター制御 Linux プログラム
Windows TerminalなどのSSHクライアントからopenHABianにログインする際の情報
# openhabian ログイン
ssh [email protected] -p 22
#パスワード
openhabian
SSHクライアントからopenHABianにログインしddcutilのインストール
ddcutilのインストール
sudo apt install ddcutil
Raspberry Pi 4の設定
https://www.ddcutil.com/raspberry/
- sudo nano /boot/config.txt で [Pi4] セクションの dtoverlay=vc4-fkms-v3d を dtoverlay=vc4-kms-v3d に置き換えます。
- /etc/modules に i2c_dev を追加します。
- 再起動
Raspberry Pi 3、Raspberry Pi 2は設定が違うのでddcutilから確認してください。
電源をオフにする
sudo shutdown -h now
再起動
sudo reboot
ddcutilのテスト
PCモニターの電源を入れた状態で
ddcutil capabilities
シェルコマンドをホワイトリストに登録
シェルコマンドをホワイトリストに登録しなければ、シェルコマンドが実行されません。
sudo nano /etc/openhab/misc/exec.whitelist
下記のシェルコマンドをホワイトリストに登録
/usr/bin/ddcutil %2$s
openHAB経由でddcutilを実行するには/dev/i2cに権限の付加が必要です。
https://www.ddcutil.com/i2c_permissions/
openhabユーザーをグループ i2c に追加します。
sudo usermod openhab -aG i2c
下記のコマンドはRaspberry Piの再起動後にもう一度/dev/i2cに権限の付加が必要なテスト用です。
sudo chmod 666 /dev/i2c-*
Thingsの作成
settings > Things
Command
/usr/bin/ddcutil %2$s
Model作成
Add to Model > Create Equipment from Thing
Channels はInputを選択その他は任意
openHABにボタンを作成
openHABのTOPページの右上の鉛筆の隣のアイコンをクリックしHAB Panelを選択。
HAB Panelでボタンを作成しボタンの設定をする。
アクションタイプ
アイテムに固定コマンドを送る
openHAB アイテム
作成したThingsのInput
コマンド値
setvcp 60 17