Hello everyone,
I am trying use the ESP32 BLE server example in the arduino example code. The way the code is written right now is that all the initialization code (Line 34 to Line 51) for BLE is done in the setup() function. It is where the value is initialized for a characteristic of the service. The issue is that I want to use the function at Line 43, "pCharacteristic->setValue((uint8_t *)spell2, 5);" inside the loop function so that I could change the characteristic value in the loop function. But this function is out of scope for loop() function.
I tried putting the initialization code of BLE(Line 34 to Line 51) out of the setup function() but then I get a bunch of errors.
So my question is " How and where should I place the BLE initialization code so I could still access pCharacteristic->setValue() function inside the loop() function?".
On a general note, in a traditional C or C++ program, I would do this kind of initialization inside the main function and all the classes/variables etc. would be available for the infinite while loop to access. How does one go about achieving that kind of behaviour in arduino environment since the main function is hidden from us?
Look for comment " ////////// BLE initialization stuff - Start////////////// " below. This is the code of concern.
Any help would be greatly appreciated !!!
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
#define SPELL_LENGTH 1800
Adafruit_MPU6050 mpu;
uint8_t spell[SPELL_LENGTH] = {};
uint8_t spell2[] = {0,2,3,4,5};
void setup(void) {
Serial.begin(115200);
while (!Serial)
{
delay(10); // will pause Zero, Leonardo, etc until serial console opens
}
////////// BLE initialization stuff - Start//////////////
Serial.println("Starting BLE work!");
BLEDevice::init("ESP32");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setValue((uint8_t *)spell2, 5);
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!");
////////// BLE initialization stuff - END//////////////
// Try to initialize!
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_184_HZ);
pinMode(15, INPUT);
delay(100);
}
int counter = 0;
String data = "";
int cleared = 1;
void loop() {
printf("%d", spell2[0]);
sensors_event_t a, g, temp;
if(!digitalRead(15)){
mpu.getEvent(&a, &g, &temp);
if(spell2[0] == 0)
{
spell2[0] = 1;
}
// data = data + a.acceleration.x +" " +a.acceleration.y +" " +a.acceleration.z +"\n";
float x = a.acceleration.x;
int x_i = (x*100);
uint8_t x_8[2] = {};
spell[counter+0] = (uint8_t)x_i;
spell[counter+1] = (uint8_t)(x_i>>8);
float y = a.acceleration.y;
int y_i = (y*100);
uint8_t y_8[2] = {};
spell[counter+2] = (uint8_t)y_i;
spell[counter+3] = (uint8_t)(y_i>>8);
float z = a.acceleration.z;
int z_i = (z*100);
uint8_t z_8[2] = {};
spell[counter+4] = (uint8_t)z_i;
spell[counter+5] = (uint8_t)(z_i>>8);
delay(10);
counter = counter + 6;
if (counter>=SPELL_LENGTH){
counter = 0;
}
cleared= 0;
}
else if(digitalRead(15)){
if(spell2[0] == 1)
{
spell2[0] = 0;
}
if(!cleared){
for (int i = counter ; i<=SPELL_LENGTH; i++){
spell[i] = 0;
}
for (int i = 0 ; i<=SPELL_LENGTH; i++){
// printf("%d\n",spell[i]);
}
counter = 0;
}
cleared = 1;
}
}