ARduino BLE sketch send dynamic data
Budget: €8 – €30 EUR
Hi I want to send if there is motion measured with the accelometer. I use the BLE sense with integrated accelometer.
The accelometer is working . also send advert data is working. Only I am nog able to send dynamic values for this one: completeRawAdvertisingData[] = {0x02,0x01};
So need the sketch changed so I can send both 0x02,0x01 (if motion) and 0x01,0x02, (if no motion)
See code below:
#include <ArduinoBLE.h>
//**************
#include <LSM6DS3.h>
#include <Wire.h>
//Create a instance of class LSM6DS3
LSM6DS3 myIMU(I2C_MODE, 0x6A); //I2C device address 0x6A
float aX, aY, aZ, gX, gY, gZ;
const float accelerationThreshold = 1.5; // threshold of significant in G's
const int numSamples = 60;
int samplesRead = numSamples;
//*******************
BLEService myService("fff0");
BLEIntCharacteristic myCharacteristic("fff1", BLERead | BLEBroadcast);
// Advertising parameters should have a global scope. Do NOT define them in 'setup' or in 'loop'
uint8_t completeRawAdvertisingData[] = {0x02,0x01};
void setup() {
Serial.begin(9600);
// while (!Serial);
//************************
if (myIMU.begin() != 0) {
Serial.println("Device error");
} else {
Serial.println("aX,aY,aZ,gX,gY,gZ");
}
//*****************
if (!BLE.begin()) {
Serial.println("failed to initialize BLE!");
while (1);
}
myService.addCharacteristic(myCharacteristic);
BLE.addService(myService);
// Build advertising data packet
BLEAdvertisingData advData;
// If a packet has a raw data parameter, then all the other parameters of the packet will be ignored
// advData.setRawData(completeRawAdvertisingData, sizeof(completeRawAdvertisingData));
advData.setRawData(completeRawAdvertisingData, sizeof(completeRawAdvertisingData));
// Copy set parameters in the actual advertising packet
BLE.setAdvertisingData(advData);
// Build scan response data packet
BLEAdvertisingData scanData;
scanData.setLocalName("Test advertising raw data");
// Copy set parameters in the actual scan response packet
BLE.setScanResponseData(scanData);
BLE.advertise();
Serial.println("advertising ...");
}
void loop() {
BLE.poll();
// Here I want the variable completeRawAdvertisingData be filled with
//0x01,0x02 . then I kniow that there is no motion.
//****************
// wait for significant motion
while (samplesRead == numSamples) {
// read the acceleration data
aX = myIMU.readFloatAccelX();
aY = myIMU.readFloatAccelY();
aZ = myIMU.readFloatAccelZ();
// sum up the absolutes
float aSum = fabs(aX) + fabs(aY) + fabs(aZ);
// check if it's above the threshold
if (aSum >= accelerationThreshold) {
// reset the sample read count
samplesRead = 0;
break;
}
}
// check if the all the required samples have been read since
// the last time the significant motion was detected
while (samplesRead < numSamples) {
// check if both new acceleration and gyroscope data is
// available
// read the acceleration and gyroscope data
samplesRead++;
// Here I want the variable completeRawAdvertisingData be filled with
//0x02,0x01 .then I know that there is motion.
// print the data in CSV format
Serial.print(myIMU.readFloatAccelX(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatAccelY(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatAccelZ(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatGyroX(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatGyroY(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatGyroZ(), 3);
Serial.println();
if (samplesRead == numSamples) {
// add an empty line if it's the last sample
Serial.println();
}
}
//*************************
}
The accelometer is working . also send advert data is working. Only I am nog able to send dynamic values for this one: completeRawAdvertisingData[] = {0x02,0x01};
So need the sketch changed so I can send both 0x02,0x01 (if motion) and 0x01,0x02, (if no motion)
See code below:
#include <ArduinoBLE.h>
//**************
#include <LSM6DS3.h>
#include <Wire.h>
//Create a instance of class LSM6DS3
LSM6DS3 myIMU(I2C_MODE, 0x6A); //I2C device address 0x6A
float aX, aY, aZ, gX, gY, gZ;
const float accelerationThreshold = 1.5; // threshold of significant in G's
const int numSamples = 60;
int samplesRead = numSamples;
//*******************
BLEService myService("fff0");
BLEIntCharacteristic myCharacteristic("fff1", BLERead | BLEBroadcast);
// Advertising parameters should have a global scope. Do NOT define them in 'setup' or in 'loop'
uint8_t completeRawAdvertisingData[] = {0x02,0x01};
void setup() {
Serial.begin(9600);
// while (!Serial);
//************************
if (myIMU.begin() != 0) {
Serial.println("Device error");
} else {
Serial.println("aX,aY,aZ,gX,gY,gZ");
}
//*****************
if (!BLE.begin()) {
Serial.println("failed to initialize BLE!");
while (1);
}
myService.addCharacteristic(myCharacteristic);
BLE.addService(myService);
// Build advertising data packet
BLEAdvertisingData advData;
// If a packet has a raw data parameter, then all the other parameters of the packet will be ignored
// advData.setRawData(completeRawAdvertisingData, sizeof(completeRawAdvertisingData));
advData.setRawData(completeRawAdvertisingData, sizeof(completeRawAdvertisingData));
// Copy set parameters in the actual advertising packet
BLE.setAdvertisingData(advData);
// Build scan response data packet
BLEAdvertisingData scanData;
scanData.setLocalName("Test advertising raw data");
// Copy set parameters in the actual scan response packet
BLE.setScanResponseData(scanData);
BLE.advertise();
Serial.println("advertising ...");
}
void loop() {
BLE.poll();
// Here I want the variable completeRawAdvertisingData be filled with
//0x01,0x02 . then I kniow that there is no motion.
//****************
// wait for significant motion
while (samplesRead == numSamples) {
// read the acceleration data
aX = myIMU.readFloatAccelX();
aY = myIMU.readFloatAccelY();
aZ = myIMU.readFloatAccelZ();
// sum up the absolutes
float aSum = fabs(aX) + fabs(aY) + fabs(aZ);
// check if it's above the threshold
if (aSum >= accelerationThreshold) {
// reset the sample read count
samplesRead = 0;
break;
}
}
// check if the all the required samples have been read since
// the last time the significant motion was detected
while (samplesRead < numSamples) {
// check if both new acceleration and gyroscope data is
// available
// read the acceleration and gyroscope data
samplesRead++;
// Here I want the variable completeRawAdvertisingData be filled with
//0x02,0x01 .then I know that there is motion.
// print the data in CSV format
Serial.print(myIMU.readFloatAccelX(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatAccelY(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatAccelZ(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatGyroX(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatGyroY(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatGyroZ(), 3);
Serial.println();
if (samplesRead == numSamples) {
// add an empty line if it's the last sample
Serial.println();
}
}
//*************************
}