init
This commit is contained in:
		
							
								
								
									
										110
									
								
								esp32-c3.ino
									
									
									
									
									
								
							
							
						
						
									
										110
									
								
								esp32-c3.ino
									
									
									
									
									
								
							| @@ -4,6 +4,7 @@ | ||||
| #include <BLE2902.h> | ||||
| #include "MPU6050.h" | ||||
| #include "Wire.h" | ||||
| #include <WiFi.h> | ||||
|  | ||||
| #define SERVICE_UUID "9b9f77c6-7e68-4109-b987-b096233d9525" | ||||
| #define CHARACTERISTIC_UUID "1ab2c9f4-19c0-48dd-8932-ed72558ec593" | ||||
| @@ -29,27 +30,45 @@ class MyServerCallbacks : public BLEServerCallbacks { | ||||
|   } | ||||
| }; | ||||
|  | ||||
| void setup_wifi() { | ||||
|   WiFi.mode(WIFI_STA); | ||||
|   WiFi.begin("Connecto Patronum", "MZ2PUD36D8N23PC2"); | ||||
|   Serial.print("Connecting to WiFi .."); | ||||
|   while (WiFi.status() != WL_CONNECTED) { | ||||
|     Serial.print('.'); | ||||
|     delay(1000); | ||||
|   } | ||||
|   Serial.println(WiFi.localIP()); | ||||
| } | ||||
|  | ||||
| void setup_ble() { | ||||
|   BLEDevice::init("ESP32"); | ||||
|   BLEServer *pServer = BLEDevice::createServer(); | ||||
|   BLEService *pService = pServer->createService(SERVICE_UUID); | ||||
|   BLECharacteristic *pCharacteristic = pService->createCharacteristic( | ||||
|     CHARACTERISTIC_UUID, | ||||
|     BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY); | ||||
|   BLEDevice::init("YourESP32"); | ||||
|   pServer = BLEDevice::createServer(); | ||||
|   pServer->setCallbacks(new MyServerCallbacks()); | ||||
|   BLEService *pService = pServer->createService(BLEUUID(SERVICE_UUID)); | ||||
|  | ||||
|   Serial.println("Created service"); | ||||
|  | ||||
|   pCharacteristic = pService->createCharacteristic( | ||||
|     BLEUUID(CHARACTERISTIC_UUID), | ||||
|     BLECharacteristic::PROPERTY_NOTIFY); | ||||
|  | ||||
|   pCharacteristic->addDescriptor(new BLE2902()); | ||||
|  | ||||
|   Serial.println("Created characteristic"); | ||||
|  | ||||
|   pService->start(); | ||||
|   // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility | ||||
|   Serial.println("Started service"); | ||||
|  | ||||
|   BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); | ||||
|   pAdvertising->addServiceUUID(SERVICE_UUID); | ||||
|   pAdvertising->setScanResponse(true); | ||||
|   pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue | ||||
|   pAdvertising->setMinPreferred(0x12); | ||||
|   pAdvertising->addServiceUUID(BLEUUID(SERVICE_UUID)); | ||||
|   pAdvertising->start(); | ||||
|   Serial.println("Waiting for a client connection to notify..."); | ||||
|  | ||||
|   uint8_t gyroData[] = { 1, 2, 3, 4, 5, 6 }; | ||||
|   pCharacteristic->setValue(gyroData, sizeof(gyroData)); | ||||
|   pCharacteristic->notify(); | ||||
|   BLEDevice::startAdvertising(); | ||||
|   Serial.println("Characteristic defined! Now you can read it in your phone!"); | ||||
|   Serial.println("Initial notification sent"); | ||||
| } | ||||
|  | ||||
| void setup() { | ||||
| @@ -69,6 +88,33 @@ void setup() { | ||||
|   Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); | ||||
|  | ||||
|   // use the code below to change accel/gyro offset values | ||||
|   BLEDevice::init("YourESP32"); | ||||
|   pServer = BLEDevice::createServer(); | ||||
|   pServer->setCallbacks(new MyServerCallbacks()); | ||||
|   BLEService *pService = pServer->createService(BLEUUID(SERVICE_UUID)); | ||||
|  | ||||
|   Serial.println("Created service"); | ||||
|  | ||||
|   pCharacteristic = pService->createCharacteristic( | ||||
|     BLEUUID(CHARACTERISTIC_UUID), | ||||
|     BLECharacteristic::PROPERTY_NOTIFY); | ||||
|  | ||||
|   pCharacteristic->addDescriptor(new BLE2902()); | ||||
|  | ||||
|   Serial.println("Created characteristic"); | ||||
|  | ||||
|   pService->start(); | ||||
|   Serial.println("Started service"); | ||||
|  | ||||
|   BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); | ||||
|   pAdvertising->addServiceUUID(BLEUUID(SERVICE_UUID)); | ||||
|   pAdvertising->start(); | ||||
|   Serial.println("Waiting for a client connection to notify..."); | ||||
|  | ||||
|   uint8_t gyroData[] = { 1, 2, 3, 4, 5, 6 }; | ||||
|   pCharacteristic->setValue(gyroData, sizeof(gyroData)); | ||||
|   pCharacteristic->notify(); | ||||
|   Serial.println("Initial notification sent"); | ||||
|   Serial.println("Updating internal sensor offsets..."); | ||||
|   // -76	-2359	1688	0	0	0 | ||||
|   Serial.print(accelgyro.getXAccelOffset()); | ||||
| @@ -105,27 +151,31 @@ void setup() { | ||||
|   setup_ble(); | ||||
| } | ||||
|  | ||||
| uint8_t idx = 0; | ||||
|  | ||||
| void loop() { | ||||
|   idx++; | ||||
|  | ||||
|   // read raw accel/gyro measurements from device | ||||
|   accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); | ||||
|  | ||||
|   // these methods (and a few others) are also available | ||||
|   //accelgyro.getAcceleration(&ax, &ay, &az); | ||||
|   //accelgyro.getRotation(&gx, &gy, &gz); | ||||
|  | ||||
|   // display tab-separated accel/gyro x/y/z values | ||||
|   Serial.print("a/g:\t"); | ||||
|   Serial.print(ax); | ||||
|   Serial.print("\t"); | ||||
|   Serial.print(ay); | ||||
|   Serial.print("\t"); | ||||
|   Serial.print(az); | ||||
|   Serial.print("\t"); | ||||
|   Serial.print(gx); | ||||
|   Serial.print("\t"); | ||||
|   Serial.print(gy); | ||||
|   Serial.print("\t"); | ||||
|   Serial.println(gz); | ||||
|   if (idx % 20 == 0) { | ||||
|  | ||||
|     // display tab-separated accel/gyro x/y/z values | ||||
|     Serial.print("a/g:\t"); | ||||
|     Serial.print(ax); | ||||
|     Serial.print("\t"); | ||||
|     Serial.print(ay); | ||||
|     Serial.print("\t"); | ||||
|     Serial.print(az); | ||||
|     Serial.print("\t"); | ||||
|     Serial.print(gx); | ||||
|     Serial.print("\t"); | ||||
|     Serial.print(gy); | ||||
|     Serial.print("\t"); | ||||
|     Serial.println(gz); | ||||
|   } | ||||
|  | ||||
|   if (deviceConnected) { | ||||
|  | ||||
| @@ -154,5 +204,5 @@ void loop() { | ||||
|   } | ||||
|  | ||||
|  | ||||
|   delay(50); | ||||
|   delay(100); | ||||
| } | ||||
|   | ||||
							
								
								
									
										69
									
								
								mpu6050/mpu6050.ino
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								mpu6050/mpu6050.ino
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,69 @@ | ||||
| // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files | ||||
| // for both classes must be in the include path of your project | ||||
| #include "I2Cdev.h" | ||||
| #include "MPU6050.h" | ||||
| #include "Wire.h" | ||||
|  | ||||
| MPU6050 accelgyro; | ||||
|  | ||||
| int16_t ax, ay, az; | ||||
| int16_t gx, gy, gz; | ||||
|  | ||||
| void setup() { | ||||
|     Wire.begin(); | ||||
|  | ||||
|     // initialize serial communication | ||||
|     // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but | ||||
|     // it's really up to you depending on your project) | ||||
|     Serial.begin(38400); | ||||
|  | ||||
|     // initialize device | ||||
|     Serial.println("Initializing I2C devices..."); | ||||
|     accelgyro.initialize(); | ||||
|  | ||||
|     // verify connection | ||||
|     Serial.println("Testing device connections..."); | ||||
|     Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); | ||||
|  | ||||
|     // use the code below to change accel/gyro offset values | ||||
|     /* | ||||
|     Serial.println("Updating internal sensor offsets..."); | ||||
|     // -76	-2359	1688	0	0	0 | ||||
|     Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76 | ||||
|     Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359 | ||||
|     Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688 | ||||
|     Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0 | ||||
|     Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0 | ||||
|     Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0 | ||||
|     Serial.print("\n"); | ||||
|     accelgyro.setXGyroOffset(220); | ||||
|     accelgyro.setYGyroOffset(76); | ||||
|     accelgyro.setZGyroOffset(-85); | ||||
|     Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76 | ||||
|     Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359 | ||||
|     Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688 | ||||
|     Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0 | ||||
|     Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0 | ||||
|     Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0 | ||||
|     Serial.print("\n"); | ||||
|     */ | ||||
|  | ||||
| } | ||||
|  | ||||
| void loop() { | ||||
|     // read raw accel/gyro measurements from device | ||||
|     accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); | ||||
|  | ||||
|     // these methods (and a few others) are also available | ||||
|     //accelgyro.getAcceleration(&ax, &ay, &az); | ||||
|     //accelgyro.getRotation(&gx, &gy, &gz); | ||||
|     // display tab-separated accel/gyro x/y/z values | ||||
|     Serial.print("a/g:\t"); | ||||
|     Serial.print(ax); Serial.print("\t"); | ||||
|     Serial.print(ay); Serial.print("\t"); | ||||
|     Serial.print(az); Serial.print("\t"); | ||||
|     Serial.print(gx); Serial.print("\t"); | ||||
|     Serial.print(gy); Serial.print("\t"); | ||||
|     Serial.println(gz); | ||||
| } | ||||
|  | ||||
							
								
								
									
										2
									
								
								mpu6050/sketch.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								mpu6050/sketch.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| default_port: /dev/ttyACM0 | ||||
| default_fqbn: esp32:esp32:XIAO_ESP32C3 | ||||
							
								
								
									
										2
									
								
								test/sketch.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								test/sketch.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| default_port: /dev/ttyACM0 | ||||
| default_fqbn: esp32:esp32:esp32s2 | ||||
							
								
								
									
										62
									
								
								test/test.ino
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								test/test.ino
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,62 @@ | ||||
| /********* | ||||
|   Rui Santos | ||||
|   Complete instructions at https://RandomNerdTutorials.com/esp32-ble-server-client/ | ||||
|   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. | ||||
|   The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||||
| *********/ | ||||
|  | ||||
| #include "BLEDevice.h" | ||||
| #include <Wire.h> | ||||
|  | ||||
| //BLE Server name (the other ESP32 name running the server sketch) | ||||
| #define bleServerName "YourESP32" | ||||
|  | ||||
| /* UUID's of the service, characteristic that we want to read*/ | ||||
| // BLE Service | ||||
| static BLEUUID bmeServiceUUID("91bad492-b950-4226-aa2b-4ede9fa42f59"); | ||||
|  | ||||
| //Temperature Celsius Characteristic | ||||
| static BLEUUID temperatureCharacteristicUUID("cba1d466-344c-4be3-ab3f-189f80dd7518"); | ||||
|  | ||||
| //Flags stating if should begin connecting and if the connection is up | ||||
| static boolean doConnect = false; | ||||
| static boolean connected = false; | ||||
|  | ||||
| //Address of the peripheral device. Address will be found during scanning... | ||||
| static BLEAddress* pServerAddress; | ||||
|  | ||||
| //Characteristicd that we want to read | ||||
| static BLERemoteCharacteristic* temperatureCharacteristic; | ||||
|  | ||||
| //Activate notify | ||||
| const uint8_t notificationOn[] = { 0x1, 0x0 }; | ||||
| const uint8_t notificationOff[] = { 0x0, 0x0 }; | ||||
|  | ||||
| //Variables to store temperature and humidity | ||||
| char* temperatureChar; | ||||
|  | ||||
| //Flags to check whether new temperature and humidity readings are available | ||||
| boolean newTemperature = false; | ||||
| boolean newHumidity = false; | ||||
|  | ||||
| //Connect to the BLE Server that has the name, Service, and Characteristics | ||||
| bool connectToServer(BLEAddress pAddress) { | ||||
|   BLEClient* pClient = BLEDevice::createClient(); | ||||
|  | ||||
|   // If the flag "doConnect" is true then we have scanned for and found the desired | ||||
|   // BLE Server with which we wish to connect.  Now we connect to it.  Once we are | ||||
|   // connected we set the connected flag to be true. | ||||
|   if (doConnect == true) { | ||||
|     if (connectToServer(*pServerAddress)) { | ||||
|       Serial.println("We are now connected to the BLE Server."); | ||||
|       //Activate the Notify property of each Characteristic | ||||
|       temperatureCharacteristic->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)notificationOn, 2, true); | ||||
|       humidityCharacteristic->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)notificationOn, 2, true); | ||||
|       connected = true; | ||||
|     } else { | ||||
|       Serial.println("We have failed to connect to the server; Restart your device to scan for nearby BLE server again."); | ||||
|     } | ||||
|     doConnect = false; | ||||
|   } | ||||
|   delay(1000);  // Delay a second between loops. | ||||
| } | ||||
		Reference in New Issue
	
	Block a user