Genuino 101 の BLE 機能を試す

2016.07.29

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

車輪開発大好きおたいがです。こんにちは。( 挨拶 )

今回は Genuino 101 ( 以下 Genuino ) に搭載されている BLE 機能を試してみました。

Genuino の BLE サービスを開始させたあと、Android 端末から接続して、Genuino に書き込み信号を送ります。その信号を Genuino に検知させて LED を制御するまでの手順をまとめました。

動作環境・道具など

  • Genuino 101
  • USB ケーブル
  • 3mm LED ( 2 本足 )
  • Windows 7 Pro (64bit)
  • Arduino IDE 1.6.9
  • nRF Connect for Android and iOS

サンプルスケッチ ( プログラム ) をボードに書き込む

Arduino IDE の [ファイル] → [スケッチの例] → [CurieBLE] メニューには、BLE 関連のサンプルスケッチがまとめられています。( 同様のものが Intel Open Source Technology Center の GitHub リポジトリにまとめられています )

今回は [CurieBLE] の中から [LED] スケッチを選んで展開します。

genuino_ble_001

Android 端末が BLE を検知するとき、迷わないように名前だけ変えておきました。(L:35)

/*
   Copyright (c) 2015 Intel Corporation.  All rights reserved.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include <CurieBLE.h>

BLEPeripheral blePeripheral;  // BLE Peripheral Device (the board you're programming)
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

const int ledPin = 13; // pin to use for the LED

void setup() {
  Serial.begin(9600);

  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);

  // set advertised local name and service UUID:
  blePeripheral.setLocalName("taiga test LED");
  blePeripheral.setAdvertisedServiceUuid(ledService.uuid());

  // add service and characteristic:
  blePeripheral.addAttribute(ledService);
  blePeripheral.addAttribute(switchCharacteristic);

  // set the initial value for the characeristic:
  switchCharacteristic.setValue(0);

  // begin advertising BLE service:
  blePeripheral.begin();

  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLECentral central = blePeripheral.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        if (switchCharacteristic.value()) {   // any value other than 0
          Serial.println("LED on");
          digitalWrite(ledPin, HIGH);         // will turn the LED on
        } else {                              // a 0 value
          Serial.println(F("LED off"));
          digitalWrite(ledPin, LOW);          // will turn the LED off
        }
      }
    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

マイコンボードに書き込んで、Genuino の 13 ピン と GND に LED を挿しこんだら Genuino 側の支度は完了です。見た目では分りませんが、すでに BLE サービスが動作しています。

genuino_ble_002

genuino_ble_003

Genuino の BLE サービスを検知させる

nRF Connect という BLE デバイスの検知と通信を可能にするアプリを端末にインストールします。iOS 版 | Android 版 ( 本記事では Android 版を使用しました )

ble で検索すると色々でてきます。

genuino_ble_004

genuino_ble_005

nRF Connect を起動したら、Genuino の傍で SCANING を開始します。立ち上げた BLE が検知されるので、[CONNECT] ボタンを押下します。

genuino_ble_006

接続すると、[Unknown Characteristic] という項目が表示されるので、上矢印ボタンを押下します。

genuino_ble_007

Genuino に向けて送る信号を設定します。まずは LED を光らせるための信号 ( UINT 8 型で 1 を送る設定 ) を用意して…

genuino_ble_008

次に、点灯中の LED を消すための信号 ( UINT 8 型で 0 を送る設定 ) を用意したら完成です。

genuino_ble_009

下記画像の [on] を選択した状態で [SEND] ボタンを押下すると Genuino に挿しこまれている LED が点灯します。[off] を選択した状態で [SEND] ボタンを押すと LED が消灯します。

genuino_ble_010

オチ

ここまで試した段階で、ようやく公式のチュートリアルが存在することに気付きました… orz

Arduino/Genuino 101 CurieBLE LED
https://www.arduino.cc/en/Tutorial/Genuino101CurieBLELED

その他、参考

Genuino (Arduino) 101 で BLE 経由でLチカさせる - Qiita
http://qiita.com/hotchpotch/items/79bca9ec00e969a0c44a