ESP32.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <BLEDevice.h>
  2. #include <BLEServer.h>
  3. #include <BLEUtils.h>
  4. #include <BLE2902.h>
  5. #include <ArduinoJson.h>
  6. #include <Timer.h>
  7. #include <MPU6050.h>
  8. #include <I2Cdev.h>
  9. MPU6050 mpu;
  10. Timer time;
  11. BLECharacteristic *pCharacteristic;
  12. bool deviceConnected = false;
  13. JsonDocument txValue;
  14. #define SERVICE_UUID = NULL //theres a UUID for each esp32 from the FCC we jsut put that data here and copy it below
  15. #define CHARACTERISTIC_UUID_TX = NULL
  16. //defines the output format of the mpu data
  17. #define OUTPUT_READABLE_ACCELGYRO
  18. //creates a few 16 bit sighned intergers for each axis
  19. int16_t ax, ay, az;
  20. int16_t gx, gy, gz;
  21. class MyServerCallBacks: public BLEServerCallbacks {
  22. void onConnect(BLEServer* pServer) {
  23. deviceConnected = true;
  24. };
  25. void onDisconnect(BLEServer* pServer){
  26. deviceConnected = false;
  27. }
  28. };
  29. bool connectedTimer() {
  30. if(deviceConnected) {
  31. timer.start();
  32. if(time.read() == 3000) {
  33. if(deviceConnected) {
  34. return true;
  35. time.stop();
  36. } else {
  37. time.stop();
  38. return false;
  39. }
  40. }
  41. }
  42. }
  43. int accelX = mpu.getXAccelOffset();
  44. int accelY = mpu.getYAccelOffset();
  45. int accelZ = mpu.getZAccelOffset();
  46. int gyroX = mpu.getXAccelOffset();
  47. int gyroY = mpu.getYAccelOffset();
  48. int gyroZ = mpu.getZAccelOffset();
  49. //create the JSON format
  50. JsonDocument mpuData;
  51. mpuData["accelration_x"] = accelX;
  52. mpuData["accelration_y"] = accelY;
  53. mpuData["accelration_z"] = accelZ;
  54. mpuData["gyro_x"] = gyroX;
  55. mpuData["gyro_y"] = gyroY;
  56. mpuData["gyro_z"] = gyroZ;
  57. //serializeJson(mpuData, Serial);
  58. void setup() {
  59. // put your setup code here, to run once:
  60. Serial.begin(38400);
  61. if(I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE){
  62. Wire.begin();
  63. } elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) {
  64. Fastwire::setup(400, true);
  65. }
  66. Serial.println("starting MPU");
  67. mpu.initialize();
  68. Serial.println("running checks.....");
  69. if(mpu.testConnection() == false){
  70. Serial.println("testing failed");
  71. } else {
  72. Serial.println("MPU6050; 200")
  73. }
  74. Serial.println("about to Zero, Hold device in desired pos for 5 seconds to accuratlly zero");
  75. Delay(3000);
  76. mpu.setXAccelOffset(0);
  77. mpu.setYAccelOffset(0);
  78. mpu.setZAccelOffset(0);
  79. mpu.setXGyroOffset(0);
  80. mpu.setYGyroOffset(0);
  81. mpu.setZGyroOffset(0);
  82. Serial.println("Device is now zeroed to current pos");
  83. Serial.println("Setting current values");
  84. //create bluetooth device
  85. BLEDevice::init("dope paddle");
  86. //create bluetooth server
  87. BLEServer *pServer = BLEServer::createServer();
  88. pServer->setCallbacks(new MyServerCallbacks());
  89. //actually create the signal
  90. BLEService *pService = pServer->createService(SERVICE_UUID);
  91. //create a bluetooth chararicteristic
  92. pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX,BLECharacteristic::PROPERTY_NOTIFY);
  93. //send bluetooth notification out
  94. pCharacteristic->addDescriptor(new BLE2902());
  95. pService->start();
  96. pServer->getAdvertising()->start();
  97. Serial.println("waiting for connection....");
  98. }
  99. void loop() {
  100. // put your main code here, to run repeatedly:
  101. //get all the current values form the MPU
  102. accelX = mpu.getXAccelOffset();
  103. accelY = mpu.getYAccelOffset();
  104. accelZ = mpu.getZAccelOffset();
  105. gyroX = mpu.getXGyroOffset();
  106. gyroY = mpu.getYGyroOffset();
  107. gyroZ = mpu.getZGyroOffset();
  108. //print all current values
  109. Serial.println(accelX + accelY + accelZ + gyroX + gyroY + gyroZ);
  110. if(connectedTimer()) {//checks that the device has been connected for 3 seconds
  111. txValue = mpuData["gyro_x"]["gyro_y"]["gyro_z"]["accelration_x"]["accelration_y"]["accelration_z"]; //txValue will transmit all the current values of mpuData
  112. //convert txValue into bytes
  113. //char txString[8];
  114. //dtostrf(txValue, 1, 2, txString);
  115. //sets txValue to the charecteristic of the device (the data to send)
  116. pCharacteristic->setValue(txValue);//txString
  117. //actually send data
  118. pCharacteristic->notify();
  119. Serial.println("IT WORKED: " + String(txValue));
  120. }
  121. }