Hii,
I am doing a project. I connected my ESP32 WROOM32 Dev Module to an app which was build in MIT APP INVENTOR through BLE. I connected a simple potentiometer to the ESP32 WROOM32 Dev Module and sent data to the app. I am receiving the output data as (48 46 48 48) which is in ASCII decimal format. I need to receive data in characters. I will provide my Arduino code and MIT Block code. Please go through it and say what are the changes should i need to change in Arduino code and MIT Block code to get the output in characters example (1.13, 2.54, 3.30).
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
BLEDescriptor *pDescr;
BLE2902 *pBLE2902;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;
// 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 MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
Serial.begin(115200);
// Create the BLE Device
BLEDevice::init("ESP32");
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_NOTIFY
);
// Create a BLE Descriptor
pDescr = new BLEDescriptor((uint16_t)0x2901);
pDescr->setValue("A very interesting variable");
pCharacteristic->addDescriptor(pDescr);
pBLE2902 = new BLE2902();
pBLE2902->setNotifications(true);
pCharacteristic->addDescriptor(pBLE2902);
// Start the service
pService->start();
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
BLEDevice::startAdvertising();
Serial.println("Waiting a client connection to notify...");
}
void loop() {
// Read the voltage from pin 34 and convert it
float voltage = analogRead(34) * 3.3 / 4095;
// If a client is connected, notify the new voltage value
if (deviceConnected) {
char voltageStr[8];
dtostrf(voltage, 1, 2, voltageStr); // Convert float to string
pCharacteristic->setValue(voltageStr);
pCharacteristic->notify();
delay(2000); // Send updates every 2000ms
}
// Handle device disconnection
if (!deviceConnected && oldDeviceConnected) {
delay(500); // Give the BLE stack time to handle disconnection
pServer->startAdvertising(); // Restart advertising
oldDeviceConnected = deviceConnected;
}
// Handle new device connection
if (deviceConnected && !oldDeviceConnected) {
oldDeviceConnected = deviceConnected;
}
}