Skip to content

Instantly share code, notes, and snippets.

@Stephanvs
Created August 21, 2019 15:00
Show Gist options
  • Save Stephanvs/d2c9135d61a83910f495090d7a6b8aa2 to your computer and use it in GitHub Desktop.
Save Stephanvs/d2c9135d61a83910f495090d7a6b8aa2 to your computer and use it in GitHub Desktop.
BLE IMU
/*
BLE IMU sketch on projecthub
-----------------------------
Arduino MKR 1010 + IMU Shield
FOR USE WITH WEB APP DEMO AT :
https://8bitkick.github.io/ArduinoBLE-IMU.html
IMPORTANT - if required update MKR 1010 fw for bluetooth support first
See https://forum.arduino.cc/index.php?topic=579306.0
*/
#include <ArduinoBLE.h>
#include <MKRIMU.h>
// BLE Service
BLEService imuService("917649A0-D98E-11E5-9EEC-0002A5D5C51B"); // Custom UUID
// BLE Characteristic
BLECharacteristic imuCharacteristic("917649A1-D98E-11E5-9EEC-0002A5D5C51B", BLERead | BLENotify, 12);
long previousMillis = 0; // last timechecked, in ms
void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected
// begin initialization
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
Serial.print("Euler Angles sample rate = ");
Serial.print(IMU.eulerAnglesSampleRate());
Serial.println(" Hz");
Serial.println();
Serial.println("Euler Angles in degrees");
Serial.println("X\tY\tZ");
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
// Setup bluetooth
BLE.setLocalName("ArduinoIMU");
BLE.setAdvertisedService(imuService);
imuService.addCharacteristic(imuCharacteristic);
BLE.addService(imuService);
// start advertising
BLE.advertise();
Serial.println("Bluetooth device active, waiting for connections...");
}
// send IMU data
void sendSensorData() {
float eulers[3];
// read orientation x, y and z eulers
IMU.readEulerAngles(eulers[0], eulers[1], eulers[2]);
// Send 3x eulers over bluetooth as 1x byte array
imuCharacteristic.setValue((byte *) &eulers, 12);
}
void loop() {
// wait for a BLE central
BLEDevice central = BLE.central();
// if a BLE central is connected to the peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's BT address:
Serial.println(central.address());
// turn on the LED to indicate the connection:
digitalWrite(LED_BUILTIN, HIGH);
// while the central is connected:
while (central.connected()) {
long currentMillis = millis();
if (currentMillis - previousMillis >= 50) {
if (IMU.eulerAnglesAvailable()) {
previousMillis = currentMillis;
sendSensorData();
}
}
}
// when the central disconnects, turn off the LED:
digitalWrite(LED_BUILTIN, LOW);
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment