Last active
June 15, 2022 11:02
-
-
Save k4zek4ge/d879b5a6dac509b542c710a31343ee55 to your computer and use it in GitHub Desktop.
[Arduino BLE multiple characteristics] #esp32 https://github.com/tigoe/BluetoothLE-Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This example creates a BLE peripheral with a service that contains a | |
couple of characteristics to test BLE connection. | |
The yellow LED shows the BLE module is initialized. | |
The green LED shows RSSI of zero. The more it blinks the worse the connection. | |
The circuit: | |
- Arduino Nano 33 BLE Sense board. | |
You can use a generic BLE central app, like LightBlue (iOS and Android) or | |
nRF Connect (Android), to interact with the services and characteristics | |
created in this sketch. | |
This example code is in the public domain. | |
*/ | |
#include <ArduinoBLE.h> | |
//#include <Arduino_LSM9DS1.h> | |
//---------------------------------------------------------------------------------------------------------------------- | |
// BLE UUIDs | |
//---------------------------------------------------------------------------------------------------------------------- | |
#define BLE_UUID_TEST_SERVICE "9A48ECBA-2E92-082F-C079-9E75AAE428B1" | |
#define BLE_UUID_ACCELERATION "2713" | |
#define BLE_UUID_COUNTER "1A3AC130-31EE-758A-BC50-54A61958EF81" | |
#define BLE_UUID_STRING "1A3AC131-31EF-758B-BC51-54A61958EF82" | |
#define BLE_UUID_BUFFER "1A3AC132-31ED-758C-BC52-54A61958EF82" | |
#define BLE_UUID_RESET_COUNTER "FE4E19FF-B132-0099-5E94-3FFB2CF07940" | |
//---------------------------------------------------------------------------------------------------------------------- | |
// BLE | |
//---------------------------------------------------------------------------------------------------------------------- | |
BLEService testService( BLE_UUID_TEST_SERVICE ); | |
BLEFloatCharacteristic accelerationCharacteristic( BLE_UUID_ACCELERATION, BLERead | BLENotify ); | |
BLEUnsignedLongCharacteristic counterCharacteristic( BLE_UUID_COUNTER, BLERead | BLENotify ); | |
BLEBoolCharacteristic resetCounterCharacteristic( BLE_UUID_RESET_COUNTER, BLEWriteWithoutResponse ); | |
BLECharacteristic stringCharacteristic( BLE_UUID_STRING, BLERead | BLENotify,"TEST1" ); | |
BLECharacteristic buffCharacteristic( BLE_UUID_BUFFER, BLERead | BLENotify,20,(1==1) ); | |
const int BLE_LED_PIN = LED_BUILTIN; | |
const int RSSI_LED_PIN = LED_PWR; | |
char buf[20]; | |
void setup() | |
{ | |
Serial.begin( 9600 ); | |
// while ( !Serial ); | |
pinMode( BLE_LED_PIN, OUTPUT ); | |
pinMode( RSSI_LED_PIN, OUTPUT ); | |
Serial.print( "Accelerometer sample rate = " ); | |
Serial.print( 10 ); | |
Serial.println( " Hz" ); | |
if( setupBleMode() ) | |
{ | |
digitalWrite( BLE_LED_PIN, HIGH ); | |
} | |
} // setup | |
void loop() | |
{ | |
static unsigned long counter = 0; | |
static long previousMillis = 0; | |
uint8_t x; | |
String aS ="TEst1String"; | |
float accelerationX =0, accelerationY, accelerationZ; | |
// listen for BLE peripherals to connect: | |
BLEDevice central = BLE.central(); | |
if ( central ) | |
{ | |
Serial.print( "Connected to central: " ); | |
Serial.println( central.address() ); | |
while ( central.connected() ) | |
{ | |
if( resetCounterCharacteristic.written() ) | |
{ | |
counter = 0; | |
Serial.println( "Reset" ); | |
//Serial.println(resetCounterCharacteristic.value() ); | |
stringCharacteristic.writeValue("test2222"); | |
aS.toCharArray(buf,20); | |
buffCharacteristic.writeValue( buf,20 ); | |
} | |
long interval = 5; | |
unsigned long currentMillis = millis(); | |
if( currentMillis - previousMillis > interval ) | |
{ | |
previousMillis = currentMillis; | |
if(x++==0){ | |
Serial.print( "Central RSSI: " ); | |
Serial.println( central.rssi() ); | |
} | |
if( central.rssi() != 0 ) | |
{ | |
digitalWrite( RSSI_LED_PIN, LOW ); | |
//if ( IMU.accelerationAvailable() ) | |
{ | |
accelerationX +=0.1; | |
//IMU.readAcceleration( accelerationX, accelerationY, accelerationZ ); | |
accelerationCharacteristic.writeValue( accelerationX ); | |
} | |
counterCharacteristic.writeValue( counter++ ); | |
counter+=0x1000; | |
} | |
else | |
{ | |
digitalWrite( RSSI_LED_PIN, HIGH ); | |
} | |
//counter++; | |
} // intervall | |
} // while connected | |
Serial.print( F( "Disconnected from central: " ) ); | |
Serial.println( central.address() ); | |
} // if central | |
} // loop | |
bool setupBleMode() | |
{ | |
//String aS ="1String"; | |
uint8_t i=0; | |
if ( !BLE.begin() ) | |
{ | |
return false; | |
} | |
// set advertised local name and service UUID: | |
BLE.setDeviceName( "Arduino Nano 33 BLE" ); | |
BLE.setLocalName( "Arduino Nano 33 BLE" ); | |
BLE.setAdvertisedService( testService ); | |
// BLE add characteristics | |
testService.addCharacteristic( accelerationCharacteristic ); | |
testService.addCharacteristic( counterCharacteristic ); | |
testService.addCharacteristic( resetCounterCharacteristic ); | |
testService.addCharacteristic( stringCharacteristic ); | |
testService.addCharacteristic( buffCharacteristic ); | |
// add service | |
BLE.addService( testService ); | |
for(i=0;i<20;i++) buf[i]=i+1; | |
// set the initial value for the characeristic: | |
accelerationCharacteristic.writeValue( 0.0 ); | |
counterCharacteristic.writeValue( 0 ); | |
buf[10]=0; | |
buffCharacteristic.writeValue( buf,20 ); | |
//buffCharacteristic. | |
// start advertising | |
BLE.advertise(); | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Accelerometer service | |
This example creates a BLE peripheral with service that contains three | |
characteristics, each an analog input value to the microcontroller. It | |
was designed with the ADXL337 accelerometer in mind, but any three analog | |
inputs will work as well. | |
The circuit: | |
- Arduino MKR WiFi 1010 (firmware version 1.2.0 or later) | |
or Arduino Uno WiFi Rev2 board | |
- ADXL337 accelerometer attached to A0, A1, and A2 | |
Based on Sandeep Mistry's ArduinoBLE examples | |
created 14 Nov. 2018 | |
by Tom Igoe | |
*/ | |
#include <ArduinoBLE.h> | |
// create service: | |
BLEService accelService("082b91ae-e83c-11e8-9f32-f2801f1b9fd1"); | |
// create characteristics and allow remote device to read and get notifications: | |
BLEIntCharacteristic accelX("082b9438-e83c-11e8-9f32-f2801f1b9fd1", BLERead | BLENotify); | |
BLEIntCharacteristic accelY("082b9622-e83c-11e8-9f32-f2801f1b9fd1", BLERead | BLENotify); | |
BLEIntCharacteristic accelZ("082b976c-e83c-11e8-9f32-f2801f1b9fd1", BLERead | BLENotify); | |
void setup() { | |
Serial.begin(9600); | |
pinMode(LED_BUILTIN, OUTPUT); | |
// begin initialization | |
while (!BLE.begin()) { | |
Serial.println("Waiting for BLE to start"); | |
delay(1); | |
} | |
// set the local name that the peripheral advertises: | |
BLE.setLocalName("MKR1010Accelerometer"); | |
// set the UUID for the service: | |
BLE.setAdvertisedService(accelService); | |
// add the characteristics to the service | |
accelService.addCharacteristic(accelX); | |
accelService.addCharacteristic(accelY); | |
accelService.addCharacteristic(accelZ); | |
// add the service | |
BLE.addService(accelService); | |
// start advertising the service: | |
BLE.advertise(); | |
} | |
void loop() { | |
BLEDevice central = BLE.central(); | |
// if a central is connected to the peripheral: | |
if (central) { | |
// print the central's BT address: | |
Serial.print("Connected to central: "); | |
Serial.println(central.address()); | |
// turn on LED to indicate connection: | |
digitalWrite(LED_BUILTIN, HIGH); | |
// while the central remains connected: | |
while (central.connected()) { | |
// read sensors: | |
int xReading = analogRead(A0); | |
delay(1); | |
int yReading = analogRead(A1); | |
delay(1); | |
int zReading = analogRead(A2); | |
// write sensor values to service characteristics: | |
accelX.writeValue(xReading); | |
accelY.writeValue(yReading); | |
accelZ.writeValue(zReading); | |
} | |
} else { | |
// turn off the LED | |
digitalWrite(LED_BUILTIN, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment