Reputation: 11
I am a student currently working on a project for an engineering degree. I have some good base knowledge of coding, data analysis, etc but I am starting to get in over my head. So basically I have been going over the included example in Arduino's IDE called "BLE_write" found in the ESP32 tutorials under ESP32 BLE Arduino. I will include the entire code. This is just a part of my entire project but it is a critical step in getting it to work. So essentially, this example code takes in serial bluetooth in uft-8 from an app on your phone and prints it to the serial port monitor on your PC using the ESP32 as a server. WHAT I NEED TO DO is simply take a number from 1-60 from bluetooth serial and assign it to an int variable. I will be using this int to specify an alarm time for a timer using interrupts later. But for now, I think I just need to know how to assign that value to the int variable. So for example, I type "42" on the app and tap send, then the code takes and assigns 42 to an int variable. I see that the code uses a class and some void functions so I assume I'll have to change that to return a value but so far, all of my attempt have failed. Any help or insight would be apprecieated! The link at the top to github does nothing :((
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleWrite.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
Serial.println("*********");
Serial.print("New value: ");
for (int i = 0; i < value.length(); i++)
Serial.print(value[i]);
Serial.println();
Serial.println("*********");
}
}
};
void setup() {
Serial.begin(115200);
Serial.println("1- Download and install an BLE scanner app in your phone");
Serial.println("2- Scan for BLE devices in the app");
Serial.println("3- Connect to MyESP32");
Serial.println("4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something");
Serial.println("5- See the magic =)");
BLEDevice::init("MyESP32");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Hello World");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}
Update: Ive discovered what I need to do is pass by reference within the defined class in order to store the address of the value for 'value'. Below is my attempt to to make such a pass by reference. I then need to call that variable in setup or maybe loop. So yea in summary: I need this to take in a string in the Void Function 'onWrite' within the Class 'MyCallBacks' and store that string at an address. Then access that string within main or loop. At the bottom of setup, you can see one of my attempts but I get the error:
"BLE_write:67:23: error: 'stgTimeSpan' was not declared in this scope std::string stg1 = &stgTimeSpan;"
Hope this update helps. Find Edited Code Below.
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
std::string *passString;
std::string stgTimeSpan = '\0';
passString = &value;
stgTimeSpan = *passString;
// if (value.length() > 0) {
// Serial.println("*********");
// Serial.print("New value: ");
// for (int i = 0; i < value.length(); i++)
// Serial.print(value[i]);
//
// Serial.println();
// Serial.println("*********");
// }
}
};
void setup() {
Serial.begin(115200);
Serial.println("1- Download and install an BLE scanner app in your phone");
Serial.println("2- Scan for BLE devices in the app");
Serial.println("3- Connect to MyESP32");
Serial.println("4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something");
Serial.println("5- See the magic =)");
BLEDevice::init("MyESP32");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Hello World");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
std::string stg1 = &stgTimeSpan;
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}
Upvotes: 1
Views: 2189
Reputation: 39
You can save the value in a global string variable outside of callbacks and extract string from the std::string using c_str() . now you can do any function of string on it in loop.
String yourValue;
class MyCallbacks: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { std::string value = pCharacteristic->getValue();
yourValue = value.c_str();
}
}
Upvotes: -1