REBL600FRで自宅iBeacon
RunningElectronicsさんからFRISKケースサイズのBL600基板、REBL600FRを購入して自宅iBeaconを作ってみました。
2014/10/3追記:自宅iBeaconを運用してみると、どうも電池の持ちが悪く(2ヶ月程度で電池が消耗)、iBeaconのパラメーターをチューニングしました。
デフォルト動作
購入品には、シリアル接続用のL字ピンヘッダや電池(CR2032)が付属しています。FRISKケースに入れてみるとこんな感じ(ピンヘッダをつけると、ケースの突起を切り取る工作が必要)。
デフォルトでiBeaconアプリが書き込まれており、IDもAppleのものなので、モジュールの電源をONにして(mode SWはRUN mode)、iPhoneにLocate for iBeacon Appをインストールすると以下のようにiBeaconの信号を検出します。
Beacon IDの書き換え
自宅iBeacon用にBeacon IDを書き換えてみます。まず、REBL600FRとPCをシリアル接続。スイッチサイエンスさんのFTDI USBシリアル変換アダプターを使用するとピン配置があわせてあるため簡単に接続ができます。接続の際は、ボードの電源SWをOFFにしてモード切替SWをDEV modeにしてUSBアダプターから給電します。
次に、BL600製造元のLaird社Webから、UwTerminalをDLします(執筆時点はVer.6.80)。適当なディレクトリに展開してUwTerminal.exeを起動。COMポートを選択し、Baudrateを9600に変更します。HandshakingはデフォルトのCTS/RTSでもよいのですが(ただし、この場合は、CTS/DTR信号もクロス接続が必要)、Noneを選んでいます。
OKボタンを押すと、Terminal画面に切り替わります。AT+EnterでOOが帰ってくれば接続成功です。AT I 3でファームウェアのバージョン、AT+DIRで書き込まれているソフトファイルが出力されます(デフォルトでは、$autorun$がiBeaconアプリとして書き込まれています)。
次に、Lairdの”Download the smartBASIC iBeacon Application file”をダウンロード。$autorun$.iBeacon.Firmware_v1_3_57_19_or_Older.sbファイルのID定義部分を書き換えます。自宅用のID生成はOSXのuuidgenコマンドを使っています。
47行目に新しく生成したUUIDを設定、49, 51行目のMajor/Minor valueは0を設定しています。
2014/10/3追記:電池の持ちが悪い原因として、デフォルトの設定では、送信パワー(53行目のBEACON_TXPOWER_DBM)が4dBm、Beaconの送信間隔(70行目のADV_INTERVAL_MS)が200msになっていることが考えられます。そこで、送信パワーを-8dBm、送信間隔を600msにしてみました。送信間隔を1000msまで広げると、Locate iBacon Appで監視した際に状態がunknownになる頻度が高く、在室中でもExitRegionを検出しそうだったため、600msに留めています。この設定でどの程度電池寿命が延びるかを監視する予定です。
'//****************************************************************************** '// Laird Technologies (c) 2013 '// '// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ '// +++++ ++ '// +++++ When UwTerminal downloads the app it will store it as a filename ++ '// +++++ which consists of all characters up to the first . and excluding it ++ '// +++++ ++ '// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ '// '// $autorun$.iBeacon.sb '// '// '//version of this app in Device Info Service #define APP_VERSION "2.1.0" '// '//************************************************************************************** '//**** Uncomment the first #define line for firmware older than 1.3.57.19 and older **** '//************************************************************************************** #define HANDLERCHARVAL HndlrCharVal(ByVal charHndl) //for <= v1.3.57.19 '//#define HANDLERCHARVAL HndlrCharVal(ByVal charHndl, ByVal offset, ByVal len) '// '// ################################################################################### '// # Version history in reverse chronological order (Customer change history) # '// ################################################################################### '// # # '// # ----------------------------------------------------------------------------- # '// # ::Changelog:: 05/12/13 -> 2.1.0 # '// # ----------------------------------------------------------------------------- # '// # # '// ################################################################################### '// '// '// ################################################################################### '// # # '// # See file $autorun$.iBeacon.sblib for usage instructions # '// # # '// ################################################################################### '// '// '//****************************************************************************** '//****************************************************************************** '// Definitions that customers use to customise the source '//****************************************************************************** '// New Uuid for beacon 03BA6107-9F0A-4461-B1D8-504DA65632DD #define BEACON_UUID "\03\ba\61\07\9f\0a\44\61\b1\d8\50\4d\a6\56\32\dd" '// Default Major value for beacon (16 bit number) #define BEACON_MAJOR 0x0000 '// Default Minor value for beacon (16 bit number) #define BEACON_MINOR 0x0000 '// Default TxPower, one of (4,0,-4,-8,-12,-16,-20,-40,-50) #define BEACON_TXPOWER_DBM -8 '//RSSI at 1m from BL600 when tx power = -8 '//Set this by advertising at -8 dBm and measuring the RSSI 1m from the BL600 #define CALIBRTD_VALUE_DBM -60 '// Default value for iBeacon format '// --> 0 : Full AD '// --> 1 : UUID + Major + Minor '// --> 2 : UUID '// --> Default : 0 #define BEACON_FORMAT 0 '// Default time to remain connectible : 1-255 #define REMAIN_CONN_TME_SEC 20 '// min time to remain connectible when updated over the air : 3-255 #define REMAIN_CONN_TME_SEC_MIN 3 '//Default advertising interval -- higher the number, lower the current consumption #define ADV_INTERVAL_MS 600 //should be in range 100..5000 '//Advertise time out (0==forever) #define ADV_TIMEOUT_MINUTES 0
UwTerminal画面を右クリックするとメニューが表示されますので、XCompile + Loadを選択。ファイル選択ダイアログが出ますので、先ほど保存したファイルを指定します。
SmartBASICのソースがバイトコードにコンパイルされ、モジュールに転送されます。これでIDの変更完了。
iBeacon検知アプリの作成
Locate iBでもiBeacon圏内に入るとiOSの「通知センター」に表示をプッシュすることができますが、”Enter Region”とかで味気がありません。せっかくの自宅iBeaconなので、もう少し萌えるメッセージを出したいということで、以下のような簡単なアプリを作ってみました。コードは、Takahiro Octopress Blogさんから拝借しました。iOSは7.1.1, Xcode 5.1.1を使用しています。
// // ViewController.h // Bea子 // // Created by Todontai on 2014/05/11. // #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface ViewController : UIViewController<CLLocationManagerDelegate> @end
// // ViewController.m // Bea子 // // Created by Todotani on 2014/05/11. // #import "ViewController.h" @interface ViewController () @property (strong, nonatomic) CLLocationManager *locationManager; @property (strong, nonatomic) NSUUID *proximityUUID; @property (strong, nonatomic) CLBeaconRegion *beaconRegion; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; if ([CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) { self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; self.proximityUUID = [[NSUUID alloc] initWithUUIDString:@"03BA6107-9F0A-4461-B1D8-504DA65632DD"]; self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID: self.proximityUUID identifier:@"com.todotani.ibeacon"]; [self.locationManager startMonitoringForRegion: self.beaconRegion]; } else { //iBeaconが利用できないOS, Deviceの場合 NSLog(@"お使いの端末ではiBeaconを利用できません。"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"確認" message:@"お使いの端末ではiBeaconを利用できません。" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alert show]; } } // 指定した領域に入った場合 - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { [self sendLocalNotificationForMessage:@"おかえりなさい、ご主人様"]; } // 指定した領域から出た場合 - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { [self sendLocalNotificationForMessage:@"いってらっしゃい、ご主人様"]; } #pragma mark - Private methods - (void)sendLocalNotificationForMessage:(NSString *)message { UILocalNotification *localNotification = [UILocalNotification new]; localNotification.alertBody = message; localNotification.fireDate = [NSDate date]; localNotification.soundName = UILocalNotificationDefaultSoundName; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; } @end
実行すると、iBeacon圏内に入ったとき(家に帰ったとき)と圏外に出たとき(家から出たとき)にやさしいメッセージを投げてくれます・・・
Twitterで、 localNotification.soundName をDefaultでなく、お気に入りのものに変更するとさらに萌えるとコメントをいただきましたが、そこまではできていません。あと、通知センターへのメッセージでなく、一時的にロック画面を「行ってらっしゃい用・お帰りなさい用」に書き換えることができるとよいなぁと思ったのですが、私のテクではAPIが分からず断念しています。
« SBDBT/SBBLEとiPhoneでBLE通信を行う | トップページ | 各種mbedのベンチマークテスト »
「Bluetooth」カテゴリの記事
- 自宅iBeaconの改良(2014.09.17)
- REBL600FRで自宅iBeacon(2014.05.11)
- SBDBT/SBBLEとiPhoneでBLE通信を行う(2014.03.26)
- Bluetooth LE (BTLE)のAdvertisement packet format(2013.02.12)
- iPhoneからmbedをBluetooth LE (BTLE)で制御する(2013.02.11)
この記事へのコメントは終了しました。
コメント