chakokuのブログ(rev4)

テック・コミック・DTM・・・ごくまれにチャリ

動作を確認するため、RPiにWebIOPiをインストールする

背景:WebIOPiについて異常動作の相談を受けたので自分のRPiに入れて動作を確認する
異常動作:daemon起動にするとWebIOPiのloop関数が正常に動作しない
結論:既知の不具合のようで、workaroundでの回避手段が必要
お詫び:編集でミスしたようで、記事の途中に宣伝が入っています。どうやって消したらいいのか分からずご了承ください

詳細:

  1. packageをDLする
  2. Patchをあてる
  3. setup.shでインストール
sudo ./setup.sh
Do you want to access WebIOPi over Internet ? [y/n]
n

WebIOPi successfully installed
* To start WebIOPi foreground   : sudo webiopi [-h] [-c config] [-l log] [-s script] [-d] [port]
* To start WebIOPi background   : sudo /etc/init.d/webiopi start
* To start WebIOPi at boot      : sudo update-rc.d webiopi defaults
* Run ./weaved-setup.bin to install the Weaved IoT Kit and access your device over Internet
* Look in /home/sumi/lang/py/webiopi/WebIOPi-0.7.1/examples for Python library usage examples
  1. Service登録用の設定ファイルをDLして、所定のフォルダに配置(service 登録)
/etc/systemd/system/webiopi.service
  1. service コマンドを用いてwebiopiを起動
$ sudo service webiopi start
$ ps aux | grep webiopi
root        2036  1.8  0.2 170568 17060 ?        Ssl  17:09   0:00 /usr/bin/python3 -m webiopi -l /var/log/webiopi -c /etc/webiopi/config

オリジナルのHTML/Scriptを導入するには以下の手順
Tutorial : Framework basis

  1. 自動起動設定
 sudo systemctl enable webiopi
# service webiopi
Usage: /etc/init.d/webiopi {start|stop|status|restart|force-reload}
root@raspberrypi:~# service webiopi status
● webiopi.service - WebIOPI
     Loaded: loaded (/etc/systemd/system/webiopi.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2024-08-18 17:38:56 JST; 3min 24s ago
   Main PID: 516 (python3)
      Tasks: 3 (limit: 8755)
        CPU: 965ms
     CGroup: /system.slice/webiopi.service
             └─516 /usr/bin/python3 -m webiopi -l /var/log/webiopi -c /etc/webiopi/config

確かに、コマンドラインからWebIOPiを起動するとloopは動作するが、daemon起動させるとloopが正しく動かないようだ。この件はすでにQAがあって、それによると、起動スクリプトにまずい所がありそうで setupの関数内でsleepを入れて起動を遅らせろという回避策が紹介されていた。
[Scripts] in Config not working if Webiopi started with systemctl · Issue #46 · thortex/rpi3-webiopi · GitHub


■追記
ログを有効にする方法

設定ファイルの編集: WebIOPiの設定ファイル(通常は/etc/webiopi/config)を開きます。以下のように、ログファイルのパスを指定します。
[LOG]
file = /var/log/webiopi.log

サービスファイルの確認: WebIOPiのサービスファイル(通常は/etc/systemd/system/webiopi.service)を確認し、ExecStart行にログファイルのパスを追加します。
[Service]
ExecStart=/usr/bin/python3 -m webiopi -l /var/log/webiopi.log -c /etc/webiopi/config

■追記
WebIOPiのファイル類が置かれている場所

$ pwd
/usr/local/lib/python3.9/dist-packages/WebIOPi-0.7.1-py3.9-linux-aarch64.egg/webiopi
$ ls
__init__.py  __pycache__  decorators  protocols  utils
__main__.py  clients      devices     server

■参考にした記事
Raspberry Piに「WebIOPi」を導入する - nyabot’s diary
[Scripts] in Config not working if Webiopi started with systemctl · Issue #46 · thortex/rpi3-webiopi · GitHub
Raspberry Piでプログラムを自動起動する5種類の方法を比較・解説 #RaspberryPi - Qiita
JavaScriptでcookieを削除する方法 | UX MILK
GitHub - thortex/rpi3-webiopi: WebIOPi for Raspberry Pi 1, 2, 3, and zero