Hi.
I have a Xiao ESP32S3 Sense. I am trying to learn some basic BLE functionality, but I am struggling and not really finding any answers to my questions. The end implementation for this project is to use two of these modules to send a message to the other one and have it perform something (like lighting an LED) in response to the message from the other one. A way to send messages between two floors in a building.
For now, I am using one module and my phone and trying to get this to work. I have used the server example sucessfully, and been able to use the nRF connect app to write to the characteristic. The issues lies in doing anything with this characteristic. No doubt I am doing something wrong, perhaps I should do this another way?
For now, I modified the example to have "pCharacteristic" as a global variable, and the loop to include logic to turn an LED on and off based on the characteristic. This is essentially psudocode, I am aware there is an illegal conversion.
#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"
BLECharacteristic* pCharacteristic = NULL;
void setup() {
pinMode(D2, OUTPUT);
Serial.begin(115200);
Serial.println("Starting BLE work!");
BLEDevice::init("Long name works now");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setValue("0");
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!");
}
void loop() {
int value = pCharacteristic->getValue();
if (value == 1) { //if characteristic is 1, set lLED to on
digitalWrite(D2, HIGH);
} else {
digitalWrite(D2, LOW);
}
delay(2000);
}
Leads to the following error:
Compilation error: cannot convert 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'} to 'int' in initialization
I also tried simply printing the value with Serial.println, but even when I found a way to get it to compile using c_str(), the processor crashes because it accesses an illegal memory location.
I don't think BLE is supposed to be this complicated, and I am likely doing something in a too convolted way. Lets just start by thinking of an LED that I want to turn on and off by setting a characteristic with the nRF connect app, how would that be done?