Hello,
I am trying to multiple data from 33ble to App Inventor via BLE. I searched for references, only found samples for single data transfer like only accel_x. I want to transfer all accel, gyro and mag data at high frequency. Would anyone please let me know how to modify my code and App Inventor?
#include <Arduino_LSM9DS1.h>
#include <ArduinoBLE.h>
BLEService gyroService("180F");
BLEFloatCharacteristic accelxLevelChar("2A11", BLERead | BLEWrite);
//BLEFloatCharacteristic accelyLevelChar("2A12", BLERead | BLEWrite);
//BLEFloatCharacteristic accelzLevelChar("2A13", BLERead | BLEWrite);
//BLEFloatCharacteristic gyroxLevelChar("2A14", BLERead | BLEWrite);
//BLEFloatCharacteristic gyroyLevelChar("2A15", BLERead | BLEWrite);
//BLEFloatCharacteristic gyrozLevelChar("2A16", BLERead | BLEWrite);
//BLEFloatCharacteristic magxLevelChar("2A17", BLERead | BLEWrite);
//BLEFloatCharacteristic magyLevelChar("2A18", BLERead | BLEWrite);
//BLEFloatCharacteristic magzLevelChar("2A19", BLERead | BLEWrite);
void setup() {
Serial.begin(9600);
if (!IMU.begin()) { //LSM9DS1 check
Serial.println("LSM9DS1 error!");
while (1);
}
if (!BLE.begin()) {
Serial.println("BLE error!");
while (1);
}
// set advertised local name and service UUID:
BLE.setAdvertisedService(gyroService); // add the service UUID
gyroService.addCharacteristic(accelxLevelChar);
//gyroService.addCharacteristic(accelyLevelChar);
//gyroService.addCharacteristic(accelzLevelChar);
//gyroService.addCharacteristic(gyroxLevelChar);
//gyroService.addCharacteristic(gyroyLevelChar);
//gyroService.addCharacteristic(gyrozLevelChar);
//gyroService.addCharacteristic(magxLevelChar);
//gyroService.addCharacteristic(magyLevelChar);
//gyroService.addCharacteristic(magzLevelChar);
BLE.addService(gyroService);
// start advertising
BLE.setLocalName("gyro");
// start advertising
BLE.advertise();
}
float accel_x, accel_y, accel_z;
//float gyro_x, gyro_y, gyro_z;
//float mag_x, mag_y, mag_z;
//long previousMillis = 0; // last time the gyro level was checked, in ms
void updategyroLevel() {
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(accel_x, accel_y, accel_z);
accelxLevelChar.writeValue(accel_x);
// accelyLevelChar.writeValue(accel_y);
// accelzLevelChar.writeValue(accel_z);
}
// if (IMU.gyroscopeAvailable()) {
// IMU.readGyroscope(gyro_x, gyro_y, gyro_z);
// gyroxLevelChar.writeValue(gyro_x);
// gyroyLevelChar.writeValue(gyro_y);
// gyrozLevelChar.writeValue(gyro_z);
// }
// if (IMU.magneticFieldAvailable()) {
// IMU.readMagneticField(mag_x, mag_y, mag_z);
// magxLevelChar.writeValue(mag_x);
// magyLevelChar.writeValue(mag_y);
// magzLevelChar.writeValue(mag_z);
// }
}
void loop() {
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
while (central.connected()) {
updategyroLevel();
delay(100);
}
}
}
Sung