私的AI研究会 > WebIOPi導入

ブラウザからGPIO を操作1-WebIOPi導入

 WebIOPi を使うと「Webブラウザから Raspberry Pi をリモート操作」「」Raspberry Pi を操作するプログラムを書く」「Raspberry Pi のGPIOを操作」することができる。

WebIOPi導入準備

パッケージ更新の確認

「RaspberryPiで学ぶ電子工作」サンプルファイルのダウンロード

・ VNCビューアを起動し、Raspberry Pi 上のWEBブラウザを開く。

出版社のサポートサイト からサンプルファイル一式をダウンロードする。

・ダウンロードしたファイル「raspi1a-sample.zip」を右クリックし、「指定先にファイルを展開」を選択し、展開先に「/home/pi/bluebacks/raspi1a-sample」を入力。

・「展開」を押してファイルを解凍する。

WebIOPi導入

WebIOPi をインストール

  1. 作業用のフォルダをホームディレクトリに用意する。(インストール作業はここで行うことにする)
    $ mkdir wget
    $ cd wget
  2. WebIOPi をダウンロードする。(228ページ)
    (1)
    $ wget http://sourceforge.net/projects/webiopi/files/WebIOPi-0.7.1.tar.gz
    
    --2020-07-06 09:48:50--  http://sourceforge.net/projects/webiopi/files/WebIOPi-0.7.1.tar.gz
    sourceforge.net (sourceforge.net) をDNSに問いあわせています... 216.105.38.13
    sourceforge.net (sourceforge.net)|216.105.38.13|:80 に接続しています... 接続しました。
    HTTP による接続要求を送信しました、応答を待っています... 301 Moved Permanently
    場所: https://sourceforge.net/projects/webiopi/files/WebIOPi-0.7.1.tar.gz [続く]
    --2020-07-06 09:48:51--  https://sourceforge.net/projects/webiopi/files/WebIOPi-0.7.1.tar.gz
    sourceforge.net (sourceforge.net)|216.105.38.13|:443 に接続しています... 接続しました。
    HTTP による接続要求を送信しました、応答を待っています... 301 Moved Permanently
    場所: https://sourceforge.net/projects/webiopi/files/WebIOPi-0.7.1.tar.gz/ [続く]
    --2020-07-06 09:48:52--  https://sourceforge.net/projects/webiopi/files/WebIOPi-0.7.1.tar.gz/
    sourceforge.net (sourceforge.net)|216.105.38.13|:443 に接続しています... 接続しました。
    HTTP による接続要求を送信しました、応答を待っています... 302 Found
    場所: https://sourceforge.net/projects/webiopi/files/WebIOPi-0.7.1.tar.gz/download [続く]
      :
      : 途中省略
      :
    HTTP による接続要求を送信しました、応答を待っています... 200 OK
    長さ: 213894 (209K) [application/x-gzip]
    `WebIOPi-0.7.1.tar.gz' に保存中
    
    WebIOPi-0.7.1.tar.gz                           100%[====================================================================================================>] 208.88K  --.-KB/s 時間 0.1s     
    
    2020-07-06 09:48:54 (1.80 MB/s) - `WebIOPi-0.7.1.tar.gz' へ保存完了 [213894/213894]
    (2)
    $ tar zxf WebIOPi-0.7.1.tar.gz
    (3)
    $ cd WebIOPi-0.7.1/
    (4)
    $ wget https://raw.githubusercontent.com/neuralassembly/raspi2/master/webiopi-pi2bplus.patch
    
    --2020-07-06 09:50:05--  https://raw.githubusercontent.com/neuralassembly/raspi2/master/webiopi-pi2bplus.patch
    raw.githubusercontent.com (raw.githubusercontent.com) をDNSに問いあわせています... 151.101.108.133
    raw.githubusercontent.com (raw.githubusercontent.com)|151.101.108.133|:443 に接続しています... 接続しました。
    HTTP による接続要求を送信しました、応答を待っています... 200 OK
    長さ: 9308 (9.1K) [text/plain]
    `webiopi-pi2bplus.patch' に保存中
    
    webiopi-pi2bplus.patch                         100%[====================================================================================================>]   9.09K  --.-KB/s 時間 0.001s   
    
    2020-07-06 09:50:06 (6.59 MB/s) - `webiopi-pi2bplus.patch' へ保存完了 [9308/9308]
    (5)
    $ patch -p1 -i webiopi-pi2bplus.patch
    
    patching file htdocs/webiopi.js
    patching file python/native/cpuinfo.c
    patching file python/native/gpio.c
    patching file python/webiopi/utils/version.py
    patching file python/webiopi/protocols/http.py
    patching file python/webiopi/utils/thread.py
    (6)
    $ sudo ./setup.sh
    
    Installing WebIOPi...
    
    Updating apt package list...
    ヒット:1 http://ftp.jaist.ac.jp/raspbian buster InRelease
    ヒット:2 http://archive.raspberrypi.org/debian buster InRelease       
    パッケージリストを読み込んでいます... 完了                            
    
    Found Python 2.7.16... 
    Trying to install python-dev using apt-get
    パッケージリストを読み込んでいます... 完了
    依存関係ツリーを作成しています                
    状態情報を読み取っています... 完了
      :
      : 途中省略
      :
    Installed /usr/local/lib/python3.7/dist-packages/WebIOPi-0.7.1-py3.7-linux-armv7l.egg
    Processing dependencies for WebIOPi==0.7.1
    Finished processing dependencies for WebIOPi==0.7.1
    WebIOPi installed for Python 3.7.3
    
    Copying HTML resources...
    'htdocs' -> '/usr/share/webiopi/htdocs'
    'htdocs/app' -> '/usr/share/webiopi/htdocs/app'
    'htdocs/app/gpio-header' -> '/usr/share/webiopi/htdocs/app/gpio-header'
      :
      : 途中省略
      :
    Copying default config file...
    'python/config' -> '/etc/webiopi/config'
    Copying default passwd file...
    'python/passwd' -> '/etc/webiopi/passwd'
    Installing startup script...
    Installing webiopi command...
    Installing webiopi-passwd command...
    
    Do you want to access WebIOPi over Internet ? [y/n]
    同一ネットワーク内のみに設定。「n」を入力。(229ページ)
    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/pi/wget/WebIOPi-0.7.1/examples for Python library usage examples
    設定ファイルインストール (230ページ)
    (1)
    $ wget https://raw.githubusercontent.com/neuralassembly/raspi2/master/webiopi.service
    
    --2020-07-06 09:51:47--  https://raw.githubusercontent.com/neuralassembly/raspi2/master/webiopi.service
    raw.githubusercontent.com (raw.githubusercontent.com) をDNSに問いあわせています... 151.101.228.133
    raw.githubusercontent.com (raw.githubusercontent.com)|151.101.228.133|:443 に接続しています... 接続しました。
    HTTP による接続要求を送信しました、応答を待っています... 200 OK
    長さ: 273 [text/plain]
    `webiopi.service' に保存中
    
    webiopi.service                                100%[====================================================================================================>]     273  --.-KB/s 時間 0s       
    
    2020-07-06 09:51:48 (3.21 MB/s) - `webiopi.service' へ保存完了 [273/273]
    (2)
    $ sudo mv webiopi.service /etc/systemd/system/

WebIOPi の起動・停止

WebIOPi の動作確認

同一ネットワーク内のブラウザからWebIOPi にアクセス

  1. Raspberry Pi のIPアドレスを調べるコマンド
    $ ifconfig
  2. PCやスマートフォンのブラウザから得られたIPアドレスでアクセスする。
    http://192.168.xxx.xxx:8000
  3. ユーザー名とパスワードの入力
    ユーザー名:webiopi
    パスワード:raspberry
  4. 右のページが表示される。

WebIOPi でGPIO にアクセス

  1. 上のページの「GPIO Header」のリンクをクリック
  2. GPIO の状態表示ページとなるので、Aibotプロジェクトの回路図よりGPIO 17・18ピン番号をクリックし、LED のON/OFF を確認する。
     
    ※以上で240ページまで完了

参考資料


Last-modified: 2020-07-17 (金) 03:09:21