I am trying to send data from multiple sensors to my phone using BLE. The problem is that my phone only can find the first characteristic. Below is my code in arduino IDE 1.8.19, I use the ESP32-S3-DevKitC-1, can anybody see what I do wrong? Thanks in advance!
Code: Select all
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <driver/adc.h>
bool deviceConnected = false;
bool deviceDisconnected = true;
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" //define service UUID
#define CHARACTERISTIC_UUID_TX "59f51a40-8852-4abe-a50f-2d45e6bd51ac" //define characteristics UUID for sending
#define CHARACTERISTIC_UUID_TX1 "59f51a41-8852-4abe-a50f-2d45e6bd51ac"
//Create a class to know wether you are connected or not
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
Serial.println("Connected");
deviceConnected = true;
deviceDisconnected= false;
};
void onDisconnect(BLEServer* pServer){
Serial.println("Disconnected");
deviceConnected = false;
deviceDisconnected= true;
pServer->getAdvertising()->start();
};
};
BLECharacteristic *pCharacteristic;
BLECharacteristic *pCharacteristic1;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// Create the BLE device
BLEDevice::init("Arduino-ESP32S3");
//Create the BLE server
BLEServer *pServer= BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
//Create the BLE service
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic=pService->createCharacteristic(CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY);
pCharacteristic1=pService->createCharacteristic(CHARACTERISTIC_UUID_TX1,
BLECharacteristic::PROPERTY_NOTIFY);
pCharacteristic->addDescriptor(new BLE2902());
pCharacteristic1->addDescriptor(new BLE2902());
pService->start();
pServer->getAdvertising()->start();
Serial.println("Waiting for client connection to notify...");
}
void loop() {;
while (deviceConnected){
//Keep measuring and sending data while being connected to a phone each ... seconds(to be determined)
float VoltageN;
float VoltageN1;
int raw_dataN;
raw_dataN=adc1_get_raw(ADC1_CHANNEL_3);
VoltageN=raw_dataN*3.30/4095.00;
VoltageN1=raw_dataN;
char txString[8];
dtostrf(VoltageN, 1, 2,txString);
pCharacteristic->setValue(VoltageN);
pCharacteristic1->setValue(VoltageN1);
pCharacteristic->notify();
pCharacteristic1->notify();
Serial.println("Voltage" + String(txString));
delay(500);
}
if (deviceDisconnected){
delay(500);
}}