Forráskód Böngészése

added ESPCODE.ino

Lucas 7 hónapja
szülő
commit
ea43fcf25f
2 módosított fájl, 144 hozzáadás és 168 törlés
  1. 0 168
      ESP32.cpp
  2. 144 0
      ESPCODE.ino

+ 0 - 168
ESP32.cpp

@@ -1,168 +0,0 @@
-#include <BLEDevice.h>
-#include <BLEServer.h>
-#include <BLEUtils.h>
-#include <BLE2902.h> 
-#include <ArduinoJson.h>
-#include <Timer.h>
-#include <MPU6050.h>
-#include <I2Cdev.h>
-
-
-MPU6050 mpu;
-Timer time;
-BLECharacteristic *pCharacteristic;
-bool deviceConnected = false;
-JsonDocument txValue;
-
-#define SERVICE_UUID = NULL //theres a UUID for each esp32 from the FCC we jsut put that data here and copy it below
-#define CHARACTERISTIC_UUID_TX = NULL
-//defines the output format of the mpu data
-#define OUTPUT_READABLE_ACCELGYRO
-
-//creates a few 16 bit sighned intergers for each axis
-int16_t ax, ay, az;
-int16_t gx, gy, gz;
-
-
-class MyServerCallBacks: public BLEServerCallbacks {
-  
-  void onConnect(BLEServer* pServer) {
-    deviceConnected = true;
-  };
-
-  void onDisconnect(BLEServer* pServer){
-    deviceConnected = false;
-  };
-
-};
-
-bool connectedTimer() {
-  if(deviceConnected) {
-      timer.start();
-      if(time.read() == 3000) {
-        if(deviceConnected) {
-          return true;
-          time.stop();
-        } else {
-          time.stop();
-          return false;
-        }
-      }
-  }
-}
-
-int accelX = mpu.getXAccelOffset();
-int accelY = mpu.getYAccelOffset();
-int accelZ = mpu.getZAccelOffset();
-
-int gyroX = mpu.getXAccelOffset();
-int gyroY = mpu.getYAccelOffset();
-int gyroZ = mpu.getZAccelOffset();
-
-//create the JSON format
-JsonDocument mpuData;
-
-mpuData["accelration_x"] = accelX;
-mpuData["accelration_y"] = accelY;
-mpuData["accelration_z"] = accelZ;
-mpuData["gyro_x"] = gyroX;
-mpuData["gyro_y"] = gyroY;
-mpuData["gyro_z"] = gyroZ;
-
-//serializeJson(mpuData, Serial);
-
-void setup() {
-  // put your setup code here, to run once:
-  Serial.begin(38400);
-
-  if(I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE){
-    Wire.begin();
-  } elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) {
-    Fastwire::setup(400, true);
-  }
-
-  Serial.println("starting MPU");
-
-  mpu.initialize();
-  Serial.println("running checks.....");
-  if(mpu.testConnection() == false){
-    Serial.println("testing failed");
-  } else {
-    Serial.println("MPU6050; 200")
-  }
-
-  Serial.println("about to Zero, Hold device in desired pos for 5 seconds to accuratlly zero");
-  Delay(3000);
-  mpu.setXAccelOffset(0); 
-  mpu.setYAccelOffset(0); 
-  mpu.setZAccelOffset(0);
-  mpu.setXGyroOffset(0);  
-  mpu.setYGyroOffset(0); 
-  mpu.setZGyroOffset(0);  
-
-  Serial.println("Device is now zeroed to current pos");
-  Serial.println("Setting current values");
-
-
-
-
-  //create bluetooth device
-  BLEDevice::init("dope paddle");
-
-  //create bluetooth server
-  BLEServer *pServer = BLEServer::createServer();
-  pServer->setCallbacks(new MyServerCallbacks());
-
-  //actually create the signal
-  BLEService *pService = pServer->createService(SERVICE_UUID);
-
-  //create a bluetooth chararicteristic
-  pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX,BLECharacteristic::PROPERTY_NOTIFY);
-
-  //send bluetooth notification out
-  pCharacteristic->addDescriptor(new BLE2902());
-
-  pService->start();
-
-  pServer->getAdvertising()->start();
-  
-  Serial.println("waiting for connection....");
-}
-
-
-
-void loop() {
-  // put your main code here, to run repeatedly:
-  //get all the current values form the MPU
-  accelX = mpu.getXAccelOffset();
-  accelY = mpu.getYAccelOffset();
-  accelZ = mpu.getZAccelOffset();
-  gyroX = mpu.getXGyroOffset();
-  gyroY = mpu.getYGyroOffset();
-  gyroZ = mpu.getZGyroOffset();
-  
-  //print all current values
-  Serial.println(accelX + accelY + accelZ + gyroX + gyroY + gyroZ);
-  
-  if(connectedTimer()) {//checks that the device has been connected for 3 seconds
-    txValue = mpuData["gyro_x"]["gyro_y"]["gyro_z"]["accelration_x"]["accelration_y"]["accelration_z"]; //txValue will transmit all the current values of mpuData
-    //convert txValue into bytes
-    //char txString[8];
-    //dtostrf(txValue, 1, 2, txString);
-
-    //sets txValue to the charecteristic of the device (the data to send)
-    pCharacteristic->setValue(txValue);//txString
-
-    //actually send data
-    pCharacteristic->notify();
-    Serial.println("IT WORKED: " + String(txValue));
-    
-  }
-
-
-
-
-
-
-}
-

+ 144 - 0
ESPCODE.ino

@@ -0,0 +1,144 @@
+#include <BLEDevice.h>
+#include <BLEUtils.h>
+#include <BLEServer.h>
+
+// See the following for generating UUIDs:
+// https://www.uuidgenerator.net/
+
+#define SERVICE_UUID        "d86aecf2-d87d-489f-b664-b02de82b2fc0"
+#define CHARACTERISTIC_UUID "d86aecf2-d87d-489f-b664-b02de82b2fc0"
+
+#include <Wire.h>
+
+long accelX, accelY, accelZ;
+float gForceX, gForceY, gForceZ;
+
+long gyroX, gyroY, gyroZ;
+float rotX, rotY, rotZ;
+
+BLECharacteristic *pCharacteristic = NULL;
+
+void setup() {
+  Serial.begin(115200);
+  BLEDevice::init("Long name works now");
+  BLEServer *pServer = BLEDevice::createServer();
+  BLEService *pService = pServer->createService(SERVICE_UUID);
+  pCharacteristic =
+    pService->createCharacteristic(CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
+
+  pCharacteristic->setValue("Hello World says Neil");
+  pService->start();
+  // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
+  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
+  pAdvertising->addServiceUUID(SERVICE_UUID);
+  pAdvertising->setScanResponse(true);
+  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
+  pAdvertising->setMinPreferred(0x12);
+  BLEDevice::startAdvertising();
+  Serial.println("Characteristic defined! Now you can read it in your phone!");
+
+  Wire.begin();
+  setupMPU();
+  Serial.println("HELLO WORLD");
+}
+
+void loop(){
+  recordAccelRegisters();
+  recordGyroRegisters();
+
+  String data = printData();
+  Serial.println(data);
+  pCharacteristic->setValue(data);
+
+  /*
+
+
+
+    if(client.available()){
+      char text = client.read();
+      request += text;
+
+      if (text == '\n'){
+        if (request.indexOf("GET /data") != -1){
+…          client.print("<html><body><p>");
+          client.print(dataArray);
+          client.println("</p></body></html>");
+          client.println("Connection: close");
+          client.println(dataArray);
+        }
+
+      }
+    }
+    //printData();
+  }
+  */
+}
+
+void setupMPU(){
+  Wire.beginTransmission(0b1101000); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2)
+  Wire.write(0x6B); //Accessing the register 6B - Power Management (Sec. 4.28)
+  Wire.write(0b00000000); //Setting SLEEP register to 0. (Required; see Note on p. 9)
+  Wire.endTransmission();
+  Wire.beginTransmission(0b1101000); //I2C address of the MPU
+  Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration (Sec. 4.4)
+  Wire.write(0x00000000); //Setting the gyro to full scale +/- 250deg./s
+  Wire.endTransmission();
+  Wire.beginTransmission(0b1101000); //I2C address of the MPU
+  Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration (Sec. 4.5)
+  Wire.write(0b00000000); //Setting the accel to +/- 2g
+  Wire.endTransmission();
+}
+
+void recordAccelRegisters() {
+  Wire.beginTransmission(0b1101000); //I2C address of the MPU
+  Wire.write(0x3B); //Starting register for Accel Readings
+  Wire.endTransmission();
+  Wire.requestFrom(0b1101000,6); //Request Accel Registers (3B - 40)
+  while(Wire.available() < 6);
+  accelX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
+  accelY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
+  accelZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
+  processAccelData();
+}
+
+void processAccelData(){
+  gForceX = accelX / 16384.0;
+  gForceY = accelY / 16384.0;
+  gForceZ = accelZ / 16384.0;
+}
+
+void recordGyroRegisters() {
+  Wire.beginTransmission(0b1101000); //I2C address of the MPU
+  Wire.write(0x43); //Starting register for Gyro Readings
+  Wire.endTransmission();
+  Wire.requestFrom(0b1101000,6); //Request Gyro Registers (43 - 48)
+  while(Wire.available() < 6);
+  gyroX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
+  gyroY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
+  gyroZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
+  processGyroData();
+}
+
+void processGyroData() {
+  rotX = gyroX / 131.0;
+  rotY = gyroY / 131.0;
+  rotZ = gyroZ / 131.0;
+}
+
+String printData() {
+  Serial.print("Gyro (deg)");
+  Serial.print(" X=");
+  Serial.print(rotX);
+  Serial.print(" Y=");
+  Serial.print(rotY);
+  Serial.print(" Z=");
+  Serial.print(rotZ);
+  Serial.print(" Accel (g)");
+  Serial.print(" X=");
+  Serial.print(gForceX);
+  Serial.print(" Y=");
+  Serial.print(gForceY);
+  Serial.print(" Z=");
+  Serial.println(gForceZ);
+  return String(rotX) + "," + String(rotY) + "," + String(rotZ) + "," + String(gForceX) + "," + String(gForceY) + "," + String(gForceZ) + ",";
+}