iOSで動くtechBASICがBleutooth Low Energyに対応しているということなので、早速実験してみました。
実験に使った環境は、iPad3 + techBASIC + SBDBT + Bleutooth Low Energyファームです。
以下、その手順を紹介します。
◆必要なもの
・Bleutooth Low Energyに対応した、iPad/iPhone
私は、iPad3を使いました。
・techBASIC iPad/iPhoneで動くBASIC
・SBDBT PIC24FJ64GB004搭載のマイコン基板
・USB接続のBluetooth4.0に対応したアダプタ
私は、”iBUFFALO BSBT4D09BK”を使いました。
・SBDBT用Bleutooth Low Energyファーム
機能の詳細は、ファームのユーザーズマニュアルを参照してください。
・SBDBTの開発環境
SBDBTのマニュアルを参照してください。
◆SBDBTの準備
SBDBT用Bleutooth Low Energyファームのソースをダウンロード、コンパイルし、実行ファイルをSBDBTに書き込みます。
詳細は、Bleutooth Low Energyファームのユーザーズマニュアルを参照してください。
書き込んだファームが正常に動くか、ユーザーマニュアルを参考にLightBlueで確認しておきます。
◆iPad側の準備
下のテスト用プログラムのソースをiPadのtechBASICに送ります。
techBASICのマニュアルに書いてあるやり方は2つあります。
・iTunesを使う
下のソースプログラムを適当なファイル名を付けてファイルに保存し、iTunesでtechBASICに転送します。
ファイルの拡張子は”bas”です。
(私の環境ではなぜかうまく行きませんでした。)
・メールを使う
PCなどからメールの本文にプログラムソースを張り付け、自分あてに送信します。
iPadでそのメールを開き、techBASICで作成した空のファイルにコピペします。
ただし、メールソフトによっては長い行に勝手に改行が入ってしまうので後で手直しが必要です。
!-------ここから---------------------------------------------------------------------------
! Define the various UUIDs
serviceUUID$ = "FFF0"
stringUUID$ = "FFF1"
ledUUID$ = "FFF2"
counterUUID$ = "00001234-0000-1000-8000-00805F9B34FB"
! This is the device we've connected to.
DIM sbdbtPeripheral AS BLEPeripheral
!----------------------------------------------------------------------------
BLE.startBLE
DIM uuid(0) AS STRING !Pass an empty array to get all available devices.
BLE.startScan(uuid)
!----------------------------------------------------------------------------
! Called when a peripheral is found. If it is a target device, we
! initiate a connection to is and stop scanning for peripherals.
!
! Parameters:
! time - The time when the peripheral was discovered.
! peripheral - The peripheral that was discovered.
! services - List of services offered by the device.
! advertisements - Advertisements (information provided by the
! device without the need to read a service/characteristic)
! rssi - Received Signal Strength Indicator
!----------------------------------------------------------------------------
SUB BLEDiscoveredPeripheral(time AS DOUBLE, peripheral AS BLEPeripheral,services() AS STRING, advertisements(,) AS STRING, rssi AS SINGLE)
PRINT "Found "; peripheral.bleName; "; UUID = "; peripheral.uuid
IF peripheral.bleName = "SBDBT BLE TEST" THEN
sbdbtPeripheral = peripheral
BLE.connect(sbdbtPeripheral)
END IF
PRINT "Stopping scan"
BLE.stopScan
END SUB
!----------------------------------------------------------------------------
! Called to report information about the connection status of the
! peripheral or to report that services have been discovered.
!
! Parameters:
! time - The time when the information was received.
! peripheral - The peripheral.
! kind - The kind of call. One of
! 1 - Connection completed
! 2 - Connection failed
! 3 - Connection lost
! 4 - Services discovered
! message - For errors, a human-readable error message.
! err - If there was an error, the Apple error number. If there
! was no error, this value is 0.
!----------------------------------------------------------------------------
SUB BLEPeripheralInfo (time AS DOUBLE, peripheral AS BLEPeripheral, kind AS INTEGER, msg AS STRING, err AS LONG)
IF kind = 1 THEN
PRINT "Connected to "; peripheral.bleName
peripheral.discoverServices(uuid)
ELSE IF kind = 2 OR kind = 3 THEN
PRINT "The connection was lost."
ELSE IF kind = 4 THEN
! Services were found. If it is the main service, begin discovery
! of its characteristics.
DIM availableServices(1) AS BLEService
availableServices = peripheral.services
FOR a = 1 TO UBOUND(availableServices, 1)
PRINT "Found service "; availableServices(a).UUID
IF availableServices(a).UUID = serviceUUID$ THEN
peripheral.discoverCharacteristics(uuid, availableServices(a))
END IF
NEXT
END IF
END SUB
!----------------------------------------------------------------------------
! Called to report information about a characteristic or included
! services for a service. If it is one we are interested in, start
! handling it.
!
! Parameters:
! time - The time when the information was received.
! peripheral - The peripheral.
! service - The service whose characteristic or included
! service was found.
! kind - The kind of call. One of
! 1 - Characteristics found
! 2 - Included services found
! message - For errors, a human-readable error message.
! err - If there was an error, the Apple error number. If there
! was no error, this value is 0.
!----------------------------------------------------------------------------
SUB BLEServiceInfo (time AS DOUBLE, peripheral AS BLEPeripheral, service AS BLEService, kind AS INTEGER, msg AS STRING, err AS LONG)
IF kind = 1 THEN
DIM characteristics(1) AS BLECharacteristic
characteristics = service.characteristics
FOR i = 1 TO UBOUND(characteristics, 1)
PRINT "Found characteristic "; i; ": "; characteristics(i).uuid
IF characteristics(i).uuid = counterUUID$ THEN
peripheral.readCharacteristic(characteristics(i))
!peripheral.setNotify(characteristics(i), 1)
END IF
IF characteristics(i).uuid = stringUUID$ THEN
DIM value(3) AS INTEGER
value = [ASC("A"),ASC("B"),ASC("C")]
peripheral.writeCharacteristic(characteristics(i), value, 1)
END IF
IF characteristics(i).uuid = ledUUID$ THEN
DIM value(1) AS INTEGER
value = [$01]
peripheral.writeCharacteristic(characteristics(i), value, 1)
END IF
NEXT
END IF
END SUB
!----------------------------------------------------------------------------
! Called to return information from a characteristic.
!
! Parameters:
! time - The time when the information was received.
! peripheral - The peripheral.
! characteristic - The characteristic whose information
! changed.
! kind - The kind of call. One of
! 1 - Called after a discoverDescriptors call.
! 2 - Called after a readCharacteristics call.
! 3 - Called to report status after a writeCharacteristics
! call.
! message - For errors, a human-readable error message.
! err - If there was an error, the Apple error number. If there
! was no error, this value is 0.
!
!----------------------------------------------------------------------------
SUB BLECharacteristicInfo (time AS DOUBLE, peripheral AS BLEPeripheral, characteristic AS BLECharacteristic, kind AS INTEGER, msg AS STRING, err AS LONG)
IF kind = 2 THEN
! Read response recieved.
DIM value(1) AS INTEGER
PRINT "Value for characteristic "; characteristic.uuid; " = ";
value = characteristic.value
SELECT CASE characteristic.uuid
CASE counterUUID$
FOR i = 1 TO UBOUND(value, 1)
PRINT RIGHT(HEX(value(i)), 2);
NEXT
PRINT
CASE stringUUID$
FOR i = 1 TO UBOUND(value, 1)
PRINT CHR(value(i));
NEXT
PRINT
CASE ELSE
!PRINT "Unexpected UUID "; characteristic.uuid
END SELECT
ELSE IF kind = 3 THEN
! Write response recieved.
SELECT CASE characteristic.uuid
CASE stringUUID$
peripheral.readCharacteristic(characteristic)
CASE ELSE
!PRINT "Unexpected UUID "; characteristic.uuid
END SELECT
END IF
END SUB
!-------ここまで---------------------------------------------------------------------------
◆実験手順
1.SBDBTにUSB Bluetoothアダプタを挿し、電源を入れます。
しばらくすると、赤LEDが消灯し、オレンジLEDのみ点灯になります。
2.iPadのBluetoothをONにし、techBASICに転送したプログラムを実行します。
うまく行けばこのように表示され、赤LEDが点灯するはずです。
◆iPad3 + techBASIC + mbed + Bleutooth Low Energyの実験
Ken Todotaniさんが、SBDBT用Bleutooth Low Energyファームをmbedに移植してくださったので試してみた。
・「iPhoneからmbedをBluetooth LE (BTLE)で制御する」
techBASICプログラムのサブルーチン”BLEDiscoveredPeripheral”内の、この行を修正する。
修正前:IF peripheral.bleName = "SBDBT BLE TEST" THEN
修正後:IF peripheral.bleName = "mbed BLE TEST" THEN
うまく行けばこのように表示されるはず。
◆補足
techBASIC App Builderを使えばtechBASICで作ったプログラムを、独立したアプリケーションにできるようです。
―以上―
参考リンク
・Creating and Using Controls
・BTstackLE