raspberry pi and webiopi

23
Raspberry Pi WebIOPi を使ってみよう ガリレオ 小山博史

Upload: hiro345

Post on 07-Jul-2015

812 views

Category:

Internet


5 download

DESCRIPTION

Raspberry Pi と WebIOPi について、IoT勉強会 feat. ワンチップもくもく会 2014 年 11 月 22 日(土) at GeekLab で発表をしたときの資料です。

TRANSCRIPT

Page 1: Raspberry Pi and WebIOPi

Raspberry Pi と WebIOPi を使ってみよう

ガリレオ 小山博史

Page 2: Raspberry Pi and WebIOPi

小山博史 Twitter: @hiro345

日経LinuxでRaspberry Pi記事連載中

Page 3: Raspberry Pi and WebIOPi

宣伝  日経Linux 2014年8月号 !

IoTフレームワークのWebIOPiを取り上げています。これを使うとRaspberry Pi で簡単に Internet of Things のプロトタイプが作れるようになります。

Page 4: Raspberry Pi and WebIOPi

Raspberry Pi とは

• 教育用に開発されたボードPC

• デスクトップPCとして使える

• DebianベースのRaspbian OSがよく使われている

• 電子部品やセンサーが簡単に接続可能

• model B、B+ はLANコネクタ装備で ネットワークが利用可能

• 豊富なUSB機器が利用可能

Page 5: Raspberry Pi and WebIOPi

注目されている IoT

• Internet of Things(モノのインターネット)

• 色々なモノがインターネット接続できるようになってきたことが注目されている。

• 個人的には、リアルワールドのモノがインターネットに接続してデータ化されると、バーチャルワールドとリアルワールドの境界がより近くなることだと認識しています。

Page 6: Raspberry Pi and WebIOPi

Raspberry Pi と IoT

• 従来のパソコンと比較して小型

• ネットワーク接続が簡単

• Linuxが使えるので豊富なライブラリ、プログラミング言語を利用可能となり、開発が簡単

• プロトタイピングに適している

• ネットワーク機能を持たないデバイスのハブになる

Page 7: Raspberry Pi and WebIOPi

WebIOPi

• webiopi - Internet of Things framework - Google Project Hosting https://code.google.com/p/webiopi/

• Pythonベースなので、サーバサイドでカスタマイズしたり機能拡張をするにはPythonを使うのが基本

• クライアントサイドプログラミング用にREST APIあり

• bash、JavaScript、Ruby、Java、Pythonなど大抵のプログラミング言語から利用可能

• JavaとPythonはクライアント用ライブラリあり

Page 8: Raspberry Pi and WebIOPi

HTTPでそのまま利用

Page 9: Raspberry Pi and WebIOPi

JavaScriptからの利用

Page 10: Raspberry Pi and WebIOPi

外部サービスのAPI利用

Page 11: Raspberry Pi and WebIOPi

WebIOPiのヘッダー画面 GPIOの状態表示と制御が可能

Page 12: Raspberry Pi and WebIOPi

ボタンのカスタマイズ 簡単にできる

Page 13: Raspberry Pi and WebIOPi

WebIOPiの ディレクトリ構成・htdocs/配下にJavaScriptなどがあることがわかる・サーバプログラムはpython/配下・examples、tutorialsを確認すると作り方がわかる・ドキュメントルートはカスタマイズ指定が可能

WebIOPi-0.7.0/ ├── doc/ │   ├── CHANGELOG │   ├── INSTALL │   ├── LICENSE │   ├── README │   └── updatedoc* ├── examples/ │   ├── clients/ │   ├── magpi-cambot/ │   └── scripts/ ├── htdocs/ │   ├── app/ │   ├── index.html │   ├── jquery.js │   ├── webiopi.css │   └── webiopi.js ├── java/ │   └── client/ ├── midori/ │   └── config ├── play.sh* ├── python/ │   ├── config │   ├── native/ │   ├── passwd │   ├── setup.py │   ├── webiopi/ │   ├── webiopi-passwd.py* │   ├── webiopi.init.sh* │   └── webiopi.sh* ├── setup.sh* └── tutorials/ ├── 1.basis/ ├── 2.macros/ ├── 3.devices/ ├── 4.serial-streaming/ └── 5.serial-command/

Page 14: Raspberry Pi and WebIOPi

Raspberry Pi のネットワーク設定

• 開発中はインターネット接続

• モバイルをしている最中は単体で稼働

• PCやスマフォから接続させたい時がある

• PCやスマフォからの接続は制限したい時がある

• 用途に応じてネットワーク設定は調整が必要

Page 15: Raspberry Pi and WebIOPi

USBシリアル変換ケーブル• USBシリアル通信でつなぐ

• UARTピンのTX、RX、GNDと、USBシリアル変換コネクタを接続

• 3.3V用のコネクタを利用する事

• いろいろなUSBシリアル変換器 | hiro345http://www.sssg.org/blogs/hiro345/archives/16623.html

• FTDI USBシリアル変換アダプター(5V/3.3V切り替え機能付き) – スイッチサイエンス

• FTDI USB・シリアル変換ケーブル(3.3V)(秋月電子通商)

• CH340 USBシリアルモジュール « ちっちゃいものくらぶ

• USB-TTL変換 – aitendo@shopping

• FT231X USBシリアル変換モジュール(秋月電子通商)

Page 16: Raspberry Pi and WebIOPi

リンクローカルネットワークの設定

・/etc/network/interfaces に記述!・169.254.12.24 の固定IPアドレス指定!・WindowsやOS Xでは、IPが取得できな! いときに169.254.0.0のIPが自動設定! されるので、これで直接通信可能!・クロスケーブルかHUB経由で接続が必要!

auto lo!!iface lo inet loopback!iface eth0 inet dhcp!!auto eth0:1!iface eth0:1 inet static!address 169.254.12.24!netmask 255.255.0.0!

Page 17: Raspberry Pi and WebIOPi

x11vnc リモートデスクトップ・リモートからGUIで操作したいときに利用!・VNCサーバの一種。他にはTightVNCなどもある!・接続先は、vnc://169.254.12.24:5900!・クライアントアプリ! ・RealVNC:Linux、OS X、Windows! ・TightVNC:Windows、Java! ・OS 標準!  ・OS X:ファインダーのサーバへ接続!  ・Ubuntu:Remmina

インストールは下記。起動時にパスワード設定あり!$ sudo apt-get install x11vnc!!!設定は $HOME/.config/autostart/x11vnc.desktop!----![Desktop Entry]!Encoding=UTF-8!Type=Application!Name=X11VNC!Exec=/usr/bin/x11vnc -forever -usepw -display :0!StartupNotify=false!Terminal=false!Hidden=false!----

Page 18: Raspberry Pi and WebIOPi

無線LAN アクセスポイント化・hostapdで無線LAN接続提供・isc-dhcp-server でIPアドレス付与・本体は/etc/network/interfacesで 固定値を設定・起動時にwlan0が認識されない ことがあるので、スクリプトで 調整

インストール!$ sudo apt-get install hostapd!$ sudo apt-get install isc-dhcp-server!!ネットワーク設定変更!allow-hotplug wlan0!iface wlan0 inet static! address 192.168.20.1! netmask 255.255.255.0!

$ cat wlan0_check.sh !#!/bin/sh!LANG=C ifconfig wlan0|grep inet!if [ $? -eq 1 ]; then! sudo /sbin/ifconfig wlan0 192.168.20.1! sudo /usr/sbin/service isc-dhcp-server restart!fi!!!$ crontab -l!*/2 * * * * sh /home/pi/wlan0_check.sh > /dev/null!

Page 19: Raspberry Pi and WebIOPi

インストールしておくと便利なもの

• WiringPi

• RPi-Cam Web Interface

Page 20: Raspberry Pi and WebIOPi

• Raspberry Pi specific functions • Timing functions • Program priority, timing and threads • Serial library • SPI library • I2C library • Shift library • Software PWM library • Software tone library

WiringPi

■ インストール!$ sudo apt-get install libi2c-dev!$ git clone git://git.drogon.net/wiringPi!$ cd wiringPi/!$ sudo ./build

Page 21: Raspberry Pi and WebIOPi

RPi-Cam-Web-Interface

• Raspberry Pi カメラをWebインタフェースで利用可能

•インストールも簡単

•撮影した動画はH.264、自動でmp4へ変換(処理が重い)

•手動で変換するにはMP4Boxコマンド

• Webブラウザで撮影した動画、静止画をダウンロード可能

■ インストール!$ cd /home/pi/RPi_Cam_Web_Interface!$ git clone https://github.com/silvanmelchior/RPi_Cam_Web_Interface.git!$ git pull origin master!$ ./RPi_Cam_Web_Interface_Installer.sh install!$ ./RPi_Cam_Web_Interface_Installer.sh start

Page 22: Raspberry Pi and WebIOPi

参考:USBカメラ

• USBカメラは値段が安い

• 使用するアプリのインストールが少し大変

• 監視カメラを作っている人も多い

• ロボットへ搭載して利用している人も多い

Page 23: Raspberry Pi and WebIOPi

まとめ

• Raspberry Pi を使うと IoT のモノを開発しやすい

• 実際に利用しようとすると工夫が必要

• WebIOPiを使うとREST APIの提供が簡単

• Raspberry Pi カメラも簡単にWebブラウザから使えるようになっている

• 高速開発をしたい場合はPython。性能を上げるにはWiringPiが便利