BLEServiceledService("180A");// BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristicswitchCharacteristic("2A57",BLERead|BLEWrite);
// set LED pin to output mode
pinMode(LEDR,OUTPUT);pinMode(LEDG,OUTPUT);pinMode(LEDB,OUTPUT);pinMode(LED_BUILTIN,OUTPUT);digitalWrite(LED_BUILTIN,LOW);// when the central disconnects, turn off the LED
digitalWrite(LEDR,HIGH);// will turn the LED off
digitalWrite(LEDG,HIGH);// will turn the LED off
digitalWrite(LEDB,HIGH);// will turn the LED off
BLE.setLocalName()では、利用しているボードを識別するのに、名前を"Nano 33 BLE Sense"に変更する必要があります。
1
2
3
// set advertised local name and service UUID:
BLE.setLocalName("Nano 33 BLE Sense");BLE.setAdvertisedService(ledService);
// 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()){switch(switchCharacteristic.value()){// any value other than 0
case01:Serial.println("Red LED on");digitalWrite(LEDR,LOW);// will turn the LED on
digitalWrite(LEDG,HIGH);// will turn the LED off
digitalWrite(LEDB,HIGH);// will turn the LED off
break;case02:Serial.println("Green LED on");digitalWrite(LEDR,HIGH);// will turn the LED off
digitalWrite(LEDG,LOW);// will turn the LED on
digitalWrite(LEDB,HIGH);// will turn the LED off
break;case03:Serial.println("Blue LED on");digitalWrite(LEDR,HIGH);// will turn the LED off
digitalWrite(LEDG,HIGH);// will turn the LED off
digitalWrite(LEDB,LOW);// will turn the LED on
break;default:Serial.println(F("LEDs off"));digitalWrite(LEDR,HIGH);// will turn the LED off
digitalWrite(LEDG,HIGH);// will turn the LED off
digitalWrite(LEDB,HIGH);// will turn the LED off
break;}}}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));Serial.println(central.address());digitalWrite(LED_BUILTIN,LOW);// when the central disconnects, turn off the LED
digitalWrite(LEDR,HIGH);// will turn the LED off
digitalWrite(LEDG,HIGH);// will turn the LED off
digitalWrite(LEDB,HIGH);// will turn the LED off
}}
#include<ArduinoBLE.h>BLEServiceledService("180A");// BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristicswitchCharacteristic("2A57",BLERead|BLEWrite);voidsetup(){Serial.begin(9600);while(!Serial);// set LED's pin to output mode
pinMode(LEDR,OUTPUT);pinMode(LEDG,OUTPUT);pinMode(LEDB,OUTPUT);pinMode(LED_BUILTIN,OUTPUT);digitalWrite(LED_BUILTIN,LOW);// when the central disconnects, turn off the LED
digitalWrite(LEDR,HIGH);// will turn the LED off
digitalWrite(LEDG,HIGH);// will turn the LED off
digitalWrite(LEDB,HIGH);// will turn the LED off
// begin initialization
if(!BLE.begin()){Serial.println("starting Bluetooth® Low Energy failed!");while(1);}// set advertised local name and service UUID:
BLE.setLocalName("Nano 33 BLE Sense");BLE.setAdvertisedService(ledService);// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);// add service
BLE.addService(ledService);// set the initial value for the characteristic:
switchCharacteristic.writeValue(0);// start advertising
BLE.advertise();Serial.println("BLE LED Peripheral");}voidloop(){// listen for Bluetooth® Low Energy peripherals to connect:
BLEDevicecentral=BLE.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());digitalWrite(LED_BUILTIN,HIGH);// turn on the LED to indicate the connection
// 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()){switch(switchCharacteristic.value()){// any value other than 0
case01:Serial.println("Red LED on");digitalWrite(LEDR,LOW);// will turn the LED on
digitalWrite(LEDG,HIGH);// will turn the LED off
digitalWrite(LEDB,HIGH);// will turn the LED off
break;case02:Serial.println("Green LED on");digitalWrite(LEDR,HIGH);// will turn the LED off
digitalWrite(LEDG,LOW);// will turn the LED on
digitalWrite(LEDB,HIGH);// will turn the LED off
break;case03:Serial.println("Blue LED on");digitalWrite(LEDR,HIGH);// will turn the LED off
digitalWrite(LEDG,HIGH);// will turn the LED off
digitalWrite(LEDB,LOW);// will turn the LED on
break;default:Serial.println(F("LEDs off"));digitalWrite(LEDR,HIGH);// will turn the LED off
digitalWrite(LEDG,HIGH);// will turn the LED off
digitalWrite(LEDB,HIGH);// will turn the LED off
break;}}}// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));Serial.println(central.address());digitalWrite(LED_BUILTIN,LOW);// when the central disconnects, turn off the LED
digitalWrite(LEDR,HIGH);// will turn the LED off
digitalWrite(LEDG,HIGH);// will turn the LED off
digitalWrite(LEDB,HIGH);// will turn the LED off
}}
テスト
コーディングが完了したら、スケッチをボードにアップロードします。アップロードが成功したら、シリアルモニタを開きます。シリアルモニタに、“BLE LED Perihperal"という文字が以下のように現れます。
シリアルモニタ出力
利用可能なBluetooth®デバイスのリストに、Nano 33 BLE Senseボードが表示されます。サービスとキャラクタリスティックにアクセスするのに、LightBlueアプリケーションの利用を勧めます。iPhone用リンクか、Android用リンクを参照してください。