Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_Sensor.h>
  3. #include <Adafruit_BNO055.h>
  4. #include <utility/imumaths.h>
  5.  
  6. #include <BLEDevice.h>
  7. #include <BLEServer.h>
  8. #include <BLEUtils.h>
  9. #include <BLE2902.h>
  10.  
  11. #define IMUdelay 10 // IMU refresh rate in milisec. smaller the delay, more accurate the measurements.
  12.  
  13. Adafruit_BNO055 bno = Adafruit_BNO055();
  14.  
  15. BLECharacteristic *pCharacteristic;
  16. bool deviceConnected = false;
  17. uint8_t txValue = 0;
  18.  
  19. // See the following for generating UUIDs:
  20. // https://www.uuidgenerator.net/
  21.  
  22. #define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
  23. #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
  24. #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
  25.  
  26.  
  27. class MyServerCallbacks: public BLEServerCallbacks {
  28. void onConnect(BLEServer* pServer) {
  29. deviceConnected = true;
  30. };
  31.  
  32. void onDisconnect(BLEServer* pServer) {
  33. deviceConnected = false;
  34. }
  35. };
  36.  
  37. class MyCallbacks: public BLECharacteristicCallbacks {
  38. void onWrite(BLECharacteristic *pCharacteristic) {
  39. std::string rxValue = pCharacteristic->getValue();
  40.  
  41. if (rxValue.length() > 0) {
  42. Serial.println("*********");
  43. Serial.print("Received Value: ");
  44. for (int i = 0; i < rxValue.length(); i++)
  45. Serial.print(rxValue[i]);
  46.  
  47. Serial.println();
  48. Serial.println("*********");
  49. }
  50. }
  51. };
  52.  
  53.  
  54. void setup() {
  55. Serial.begin(115200);
  56.  
  57. if (!bno.begin())
  58. {
  59. Serial.printf("No BNO055 detected ..."); // Connection failed
  60. while (1);
  61. }
  62. delay(1000);
  63.  
  64.  
  65. bno.setExtCrystalUse(true);
  66. // Create the BLE Device
  67. BLEDevice::init("UART Service");
  68.  
  69. // Create the BLE Server
  70. BLEServer *pServer = BLEDevice::createServer();
  71. pServer->setCallbacks(new MyServerCallbacks());
  72.  
  73. // Create the BLE Service
  74. BLEService *pService = pServer->createService(SERVICE_UUID);
  75.  
  76. // Create a BLE Characteristic
  77. pCharacteristic = pService->createCharacteristic(
  78. CHARACTERISTIC_UUID_TX,
  79. BLECharacteristic::PROPERTY_READ
  80. );
  81.  
  82. pCharacteristic->addDescriptor(new BLE2902());
  83.  
  84. BLECharacteristic *pCharacteristic = pService->createCharacteristic(
  85. CHARACTERISTIC_UUID_RX,
  86. BLECharacteristic::PROPERTY_WRITE
  87. );
  88.  
  89. pCharacteristic->setCallbacks(new MyCallbacks());
  90.  
  91. // Start the service
  92. pService->start();
  93.  
  94. // Start advertising
  95. pServer->getAdvertising()->start();
  96. Serial.println("Waiting a client connection to notify...");
  97. }
  98.  
  99. void loop() {
  100.  
  101. // Possible vector values can be:
  102. // - VECTOR_ACCELEROMETER - m/s^2
  103. // - VECTOR_MAGNETOMETER - uT
  104. // - VECTOR_GYROSCOPE - rad/s
  105. // - VECTOR_EULER - degrees
  106. // - VECTOR_LINEARACCEL - m/s^2
  107. // - VECTOR_GRAVITY - m/s^2
  108. //https://www.allaboutcircuits.com/projects/bosch-absolute-orientation-sensor-bno055/
  109. imu::Vector<3> euler = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
  110. imu::Vector<3> accelerometer = bno.getVector(Adafruit_BNO055::VECTOR_ACCELEROMETER);
  111. imu::Vector<3> gyroscope = bno.getVector(Adafruit_BNO055::VECTOR_GYROSCOPE);
  112.  
  113. uint8_t system, gyro, accel, mag = 0;
  114. bno.getCalibration(&system, &gyro, &accel, &mag); // get calibration
  115.  
  116. if (deviceConnected) {
  117. String datastring = "ESP32: ";
  118. datastring += euler.y();
  119. //Serial.printf("*** Sent Value: %d ***\n", txValue);
  120. Serial.printf("*** Sent Value: %d ***\n", euler.y());
  121.  
  122. //pCharacteristic->setValue(datastring.c_str());
  123. pCharacteristic->setValue(datastring.c_str());
  124. pCharacteristic->notify();
  125. txValue++;
  126. }
  127. delay(1000);
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement