ESPCODE.ino 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <BLEDevice.h>
  2. #include <BLEUtils.h>
  3. #include <BLEServer.h>
  4. // See the following for generating UUIDs:
  5. // https://www.uuidgenerator.net/
  6. #define SERVICE_UUID "d86aecf2-d87d-489f-b664-b02de82b2fc0"
  7. #define CHARACTERISTIC_UUID "d86aecf2-d87d-489f-b664-b02de82b2fc0"
  8. #include <Wire.h>
  9. long accelX, accelY, accelZ;
  10. float gForceX, gForceY, gForceZ;
  11. long gyroX, gyroY, gyroZ;
  12. float rotX, rotY, rotZ;
  13. BLECharacteristic *pCharacteristic = NULL;
  14. void setup() {
  15. Serial.begin(115200);
  16. BLEDevice::init("Long name works now");
  17. BLEServer *pServer = BLEDevice::createServer();
  18. BLEService *pService = pServer->createService(SERVICE_UUID);
  19. pCharacteristic =
  20. pService->createCharacteristic(CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
  21. pCharacteristic->setValue("Hello World says Neil");
  22. pService->start();
  23. // BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
  24. BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  25. pAdvertising->addServiceUUID(SERVICE_UUID);
  26. pAdvertising->setScanResponse(true);
  27. pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
  28. pAdvertising->setMinPreferred(0x12);
  29. BLEDevice::startAdvertising();
  30. Serial.println("Characteristic defined! Now you can read it in your phone!");
  31. Wire.begin();
  32. setupMPU();
  33. Serial.println("HELLO WORLD");
  34. }
  35. void loop(){
  36. recordAccelRegisters();
  37. recordGyroRegisters();
  38. String data = printData();
  39. Serial.println(data);
  40. pCharacteristic->setValue(data);
  41. /*
  42. if(client.available()){
  43. char text = client.read();
  44. request += text;
  45. if (text == '\n'){
  46. if (request.indexOf("GET /data") != -1){
  47. … client.print("<html><body><p>");
  48. client.print(dataArray);
  49. client.println("</p></body></html>");
  50. client.println("Connection: close");
  51. client.println(dataArray);
  52. }
  53. }
  54. }
  55. //printData();
  56. }
  57. */
  58. }
  59. void setupMPU(){
  60. Wire.beginTransmission(0b1101000); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2)
  61. Wire.write(0x6B); //Accessing the register 6B - Power Management (Sec. 4.28)
  62. Wire.write(0b00000000); //Setting SLEEP register to 0. (Required; see Note on p. 9)
  63. Wire.endTransmission();
  64. Wire.beginTransmission(0b1101000); //I2C address of the MPU
  65. Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration (Sec. 4.4)
  66. Wire.write(0x00000000); //Setting the gyro to full scale +/- 250deg./s
  67. Wire.endTransmission();
  68. Wire.beginTransmission(0b1101000); //I2C address of the MPU
  69. Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration (Sec. 4.5)
  70. Wire.write(0b00000000); //Setting the accel to +/- 2g
  71. Wire.endTransmission();
  72. }
  73. void recordAccelRegisters() {
  74. Wire.beginTransmission(0b1101000); //I2C address of the MPU
  75. Wire.write(0x3B); //Starting register for Accel Readings
  76. Wire.endTransmission();
  77. Wire.requestFrom(0b1101000,6); //Request Accel Registers (3B - 40)
  78. while(Wire.available() < 6);
  79. accelX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
  80. accelY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
  81. accelZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
  82. processAccelData();
  83. }
  84. void processAccelData(){
  85. gForceX = accelX / 16384.0;
  86. gForceY = accelY / 16384.0;
  87. gForceZ = accelZ / 16384.0;
  88. }
  89. void recordGyroRegisters() {
  90. Wire.beginTransmission(0b1101000); //I2C address of the MPU
  91. Wire.write(0x43); //Starting register for Gyro Readings
  92. Wire.endTransmission();
  93. Wire.requestFrom(0b1101000,6); //Request Gyro Registers (43 - 48)
  94. while(Wire.available() < 6);
  95. gyroX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
  96. gyroY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
  97. gyroZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
  98. processGyroData();
  99. }
  100. void processGyroData() {
  101. rotX = gyroX / 131.0;
  102. rotY = gyroY / 131.0;
  103. rotZ = gyroZ / 131.0;
  104. }
  105. String printData() {
  106. Serial.print("Gyro (deg)");
  107. Serial.print(" X=");
  108. Serial.print(rotX);
  109. Serial.print(" Y=");
  110. Serial.print(rotY);
  111. Serial.print(" Z=");
  112. Serial.print(rotZ);
  113. Serial.print(" Accel (g)");
  114. Serial.print(" X=");
  115. Serial.print(gForceX);
  116. Serial.print(" Y=");
  117. Serial.print(gForceY);
  118. Serial.print(" Z=");
  119. Serial.println(gForceZ);
  120. return String(rotX) + "," + String(rotY) + "," + String(rotZ) + "," + String(gForceX) + "," + String(gForceY) + "," + String(gForceZ) + ",";
  121. }