iPhone から Edison を操作する
@clickbrick
Copyright © Classmethod, Inc.
自己紹介
田中 圭介(たなか けいすけ)
クラスメソッド株式会社
iOS エンジニア
サーバーサイドは苦手
SNS 嫌い
Copyright © Classmethod, Inc.
Intel Edison
ウェアラブルコンピュータ開発用のシングルボードコンピュータ
デュアルコアの Intel Atom 500MHz
メモリ 1 GB
Linux 搭載
通信用の Bluetooth と Wi-Fi を備える
Copyright © Classmethod, Inc.
iPhone と Edison
要は iOS と Linux
Copyright © Classmethod, Inc.
iOS Linux
今回はこいつらを接続します
Copyright © Classmethod, Inc.
やること
Edison
LED を光らせる
Buzzer を鳴らす
iPhone
それぞれの ON / OFF を制御する
Copyright © Classmethod, Inc.
DEMO
Copyright © Classmethod, Inc.
仕組み
Copyright © Classmethod, Inc.
* Web サーバー(Python)
* 実行プログラム(Arduino)
* Linux
0.5 秒おきにファイルパス監視
ファイルがあれば特定の処理実行
リクエストによってファイル生成/削除
* iOS アプリ
スイッチ ON / OFF によって
リクエストを送信
Web サーバー
Copyright © Classmethod, Inc.
#!/usr/bin/python
import CGIHTTPServer
CGIHTTPServer.test()
* サーバー起動プログラム(server.py)
#!/usr/bin/env python
f = open('/home/root/python/output/led', 'w')
f.write('')
f.close()
* LED 点灯 API(led_on.py)
実行プログラム
Copyright © Classmethod, Inc.
void checkLed() {
char filePath[] = "/home/root/python/output/led";
FILE *file;
if ((file = fopen(filePath, "r")) == NULL) {
Serial.println("LED file not exists.");
digitalWrite(ledPin, LOW);
} else {
Serial.println("LED file exists!!");
digitalWrite(ledPin, HIGH);
}
}
iOS アプリ
Copyright © Classmethod, Inc.
static NSString *const kBaseURL = @"http://tanason.local:8000/cgi-bin/";
- (void)sendRequestWithResource:(NSString *)resource
{
NSString *url = [NSString stringWithFormat:@"%@%@", kBaseURL, resource];
NSMutableURLRequest * request =
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
NSData * data,
NSError * error) {
if (error) {
NSLog(@"error = %@", error);
} else {
NSLog(@"Success!!");
}
}];
}
まとめ
iPhone から Edison の操作は HTTP 通信で行う
Web サーバーは Python が簡単
ファイルの存在有無で任意の処理を実行させる
ファイル生成 / 削除時はパーミッションに注意
Copyright © Classmethod, Inc.
最後に
Copyright © Classmethod, Inc.
別に iPhone じゃなくてもいいです
Copyright © Classmethod, Inc.
Hardware meetup

Hardware meetup