LoginSignup
8
9

More than 5 years have passed since last update.

iPhoneの加速度センサー値をBLE経由でEdisonに送ってカラーLEDを光らせてみる

Last updated at Posted at 2016-10-22

いわゆるLチカです.
シンプルだけど、動かすのに何が必要なのかをまとめてみる
Xcode8 (swift3) で少し書き方が変わっているためそのあたりも含め。

LEDとの接続はIntel Edison board for ArduinoのGPIOを利用

構成

X軸 Acceralation -> Red
Y軸 Acceralation -> Green
Z軸 Acceralation -> Blue

という具合に、3軸を色にマッピングしてみて、振って遊べるようにする

カラーLEDは手元にあったベストセンサーモジュールキットのものを使用、EdisonからGPIOを利用して光らせる。
EdisonのGPIO番号とMRAAの番号の関連づけは、以下のサイトを参考に。
http://iotdk.intel.com/docs/master/mraa/edison.html

接続は以下のような構成
edison-iphone-color-led.png
IMG_5473.JPG

(最終的な動画では、10kの抵抗を入れてプルダウンしてます..直接つなぐと明るすぎ)

Edison 側のプログラム

カラーLEDをランダムに光らせる

まずは、Edison側は PWM を3つ使って、接続したカラーLEDをランダムに光らせる。
BLE経由でiPhoneから受け取ったものを利用するのは次。

Edison側のプログラムはXDKを使って開発。カラーLEDを光らせる。
新規プロジェクトを作成して、main.jsを以下のようにした.

main.js
var mraa = require('mraa'); //require mraa

var r = new mraa.Pwm(5);
var g = new mraa.Pwm(3);
var b = new mraa.Pwm(6);

r.enable(true);
r.period_us(2000);
r.write(0.5);

g.enable(true);
g.period_us(2000);
g.write(0.5);

b.enable(true);
b.period_us(2000);
b.write(0.5);

periodicActivity();

var count = 0;
function periodicActivity()
{
  setTimeout(periodicActivity,100);

  // 乱数で適当に色を生成して設定
  var rcolor = (count*Math.random()*10)%255 / 255;
  var gcolor = (count*Math.random()*10)%255 / 255;
  var bcolor = (count*Math.random()*10)%255 / 255;

  r.write(rcolor);
  g.write(gcolor);
  b.write(bcolor);

  count ++;
}

BLEで待ち受けてカラーLEDを光らせる

こちらのサイトが非常に参考になる。
http://edison-lab.jp/blog/2015/07/lets-make-edison-iphone-ble.html

edison 側のセットアップ

bluez5-devをインストール

opkg update
opkg install bluez5-dev

bluetoothを有効にする
Bluetoothのソフトがブロックされているため、ブロックを解除する

rfkill list
rfkill unblock bluetooth
rfkill list

コードをセットアップする

git clone https://github.com/motokazu/led-edison-ble.git
cd led-edison-ble
npm i
node main.js 
stateChange : poweredOn
start advertising ...

続いて、iOS側のセットアップを行う

iOS側は、加速度センサー値を取得して BLE経由でデータを送る。今回は簡易的にテキストをカンマ区切りで送ることにした。

timestamp,acceleration.x,acceleration.y,acceleration.z

コードはこちら
https://github.com/motokazu/device-ios-ble
Xcodeでビルドして実機にインストールする

  • iOSで加速度センサー値を取得するには、CoreMotionを使う
  • iOSでBLEを使うには、CoreBluetooth.frameworkを使う

完成バージョンの動画

途中で出会った問題

開発途中に、iOSからBLE経由で writeValueしてデータを送っていると、すぐに送信が停止してEdisonとの接続がDisconnectとなってしまう問題が発生。

writeValue : 4743982319333547688,-0.382124,-0.409589,-14.219012
writeValue : 4743982319333963817,-0.388445,-0.524466,-14.224679
Disconnect!
error: Optional(Error Domain=CBErrorDomain Code=7 "The specified device has disconnected from us." UserInfo={NSLocalizedDescription=The specified device has disconnected from us.})

突然 接続が切れる...。

参考ページを見ていたら、"bluetoothdが背後で起動していると blenoに干渉する" と書いてあるのに.. この手順を飛ばしていた。以下を実行すると接続が切れなくなった。

killall bluetoothd
hciconfig hci0 up
8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9