WordPressで制御する
これからのIoT
画像変更できます!!
Internet of Things
モノのインターネット
なんでもネット接続
Philips Hue
小型コンピュータが普及
SoC Weight Price
周波数 帯域
無線通信技術
ヘッダ長 方向 接続
通信手順
REST API
WPのデータをJSON化
{"id":1,

"date":"2017-05-23T06:25:50",

"date_gmt":"2017-05-23T06:25:50",

"guid":{"rendered":"http://demo.wp-api.org/?p=1"},

"modified":"2017-05-23T06:25:50",

"modified_gmt":"2017-05-23T06:25:50",

"slug":"hello-world",

"status":"publish",

"type":"post",

"link":"https://demo.wp-api.org/2017/05/23/hello-world/",

"title":{"rendered":"Hello world!"},

"content":{"rendered":"<p>Welcome to...
エンドポイントの数々
カスタム可能
エンドポイント設置
add_action( 'rest_api_init', function () {
register_rest_route( 'anything/v1', '/get',
array(
'methods' => 'GET',
'callback' => 'get_anything_awesome',
) );
} );
function get_anything_awesome() {
$data = array('result' => 'great data');
return new WP_REST_Response( $data, 200 );
}
API Response
http://example.net/wp-json/
anything/v1/get
{"result":"great data"}
LED Matrix
Panel の制作
Hardware
Raspberry Pi




LED Matrix Panel
RGB Matrix HAT
横長パネルを縦つなぎ
Software




Raspbian Lite
OS 設定
OS 書き込み
Etcher
Apple Pi Baker
初期設定
パッケージインストール
$ sudo apt install git 
libgraphicsmagick++-dev 
libwebp-dev
RPi RGB LED Matrix
ライブラリのmake
$ git clone https://github.com/
hzeller/rpi-rgb-led-matrix
$ cd rpi-rgb-led-matrix
$ make -C examples-api-use
$ cd utils
$ make led-image-viewer
Demo
$ cd example-api-use
$ sudo ./demo 

--led-gpio-mapping=adafruit-hat 
--led-no-hardware-pulse 
--led-chain=4 -L 

-D 0
わぷー表示
わぷー画像を収集
横に並べる
Scroll Demo
$ sudo ./demo 

--led-gpio-mapping=adafruit-hat 
--led-no-hardware-pulse 
--led-chain=4 -L 

-D 1 ../../images/wapuu.ppm
API 設置
WP API で表示変更
Custom End Point
/wp-json/wapuu/v1/set/nnn
/wp-json/wapuu/v1/get
エンドポイント設置
add_action( 'rest_api_init', function () {
register_rest_route( 'wapuu/v1', '/set/(?P<id>d+)', array(
'methods' => 'POST',
'callback' => 'set_wapuu_image',
'args' => array(
'id' => array(
'validate_callback' => function($param, $request, $key) {
return is_numeric( $param );
}
),
),
) );
register_rest_route( 'wapuu/v1', '/get', array(
'methods' => 'GET',
'callback' => 'get_wapuu_image',
) );
} );
画像番号を保存・取得
function set_wapuu_image( $data ) {
$id = $data['id'];
update_post_meta( 60, 'wapuu-image', $id );
$data = array( 'id' => $id );
return new WP_REST_Response( $data, 200 );
}
function get_wapuu_image() {
$id = get_post_meta( 60, 'wapuu-image');
$data = array( 'id' => $id );
return new WP_REST_Response( $data, 200 );
}
API Response
http://example.net/wp-json/
wapuu/v1/get
{"id":"001"}
Python code
on RasPi
APIより画像番号取得
api="http://example.net/wp-json/wapuu/v1/get"
def img_number():
try:
html = urllib.urlopen( api ).read()
except:
return None
data = json.loads(html)
return data['id']
番号で画像を探す
image_dir = "~/wapuu/"
viewer = "~/rpi-rgb-led-matrix/utils/led-image-viewer"
opt = " --led-daemon --led-gpio-mapping=adafruit-hat" +
" --led-no-hardware-pulse --led-chain=4 -L"
prev = None
while True:
time.sleep(2)
number = img_number()
if number is None: continue
files = glob.glob(image_dir+number+"-*.png")
if len(files) < 1: continue
if files[0] == prev: continue
if prev is not None: kill_img()
commands.getstatusoutput("sudo "+viewer+opt+files[0])
prev = files[0]
切替時は画像を消す
pattern = r"^ *(d+) .*/led-image-viewer"
def kill_img ():
id = []
ps_list = commands.getoutput('ps awx')
for process in ps_list.split("n"):
match = re.match(pattern, process)
if not match: continue
id.append(match.group(1))
commands.getoutput("sudo kill "+" ".join(id))
選択ページ
<form method="post"
action="/wp-json/wapuu/v1/set/001">
<div>[caption id="attachment_40"
align="alignleft" width="150"]
<img src="/wp-content/uploads/
2017/09/001-original-150x150.png"
alt="001" width="150" height="150"
class="size-thumbnail wp-image-40" />
Original (WordCamp 2011 Fukuoka)<br>
<button type="submit" value="001">

Submit</button>
[/caption]</div></form>
室温ロガーの制作
Hardware
Raspberry Pi Zero




Enviro pHAT
Software




ライブラリインストール
$ curl https://get.pimoroni.com/
envirophat | bash
続きは宿泊で
イベント参加予定










http://www.yuriko.net/
@lilyfanjp
https://www.slideshare.net/
lilyfan/wordpress-iot
https://github.com/lilyfanjp/
wordfes2017-sample

WordPressで制御するこれからのIoT