hi
I create a project with ESP32-pico-mini
I want to use it as BLE server that when it receive a request from client, send a string as an answer.
At first I use BLE Arduino example and send a byte every 0.5 second. I use cellphone as a client and i use LightBlue and nRF connect app. in both of them I should press a key to receive data( in LightBlue I should press Read Again and in nRF I should press down key)
and none of them can not receive data automatically.
And also I have another problem.
when I change CHARACTERISTIC_UUID or its properties, they don't change any more and they remain what ever they were.
/*
Video: https://www.youtube.com/watch?v=oCMOYS71NIU
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
Ported to Arduino ESP32 by Evandro Copercini
Create a BLE server that, once we receive a connection, will send periodic notifications.
The service advertises itself as: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
Has a characteristic of: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E - used for receiving data with "WRITE"
Has a characteristic of: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E - used to send data with "NOTIFY"
The design of creating the BLE server is:
1. Create a BLE Server
2. Create a BLE Service
3. Create a BLE Characteristic on the Service
4. Create a BLE Descriptor on the characteristic
5. Start the service.
6. Start advertising.
In this example rxValue is the data received (only accessible inside that function).
And txValue is the data to be sent, in this example just a byte incremented every second.
*/
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <BLE2901.h>
#include "OP750.h"
BLEServer *pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;
BLE2901 *descriptor_2901 = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint8_t txValue = '0';
uint8_t DataAvailable = 0;
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "74b9dd20-0f16-460f-a5ed-40e5a3cde6a0" // UART service UUID
#define CHARACTERISTIC_UUID "bf8796f1-64f7-70b5-1e41-09bb46d79105"
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer *pServer) {
deviceConnected = true;
digitalWrite(BT_LED, HIGH);
Serial.println("BLE Connected");
};
void onDisconnect(BLEServer *pServer) {
deviceConnected = false;
digitalWrite(BT_LED, LOW);
Serial.println("BLE DisConnected");
}
};
void setup() {
setCpuFrequencyMhz(80);
Configurations();
digitalWrite(METER_SELECT, HIGH);
digitalWrite(SELF_SHUTDOWN, HIGH);
digitalWrite(GREEN_LED, HIGH);
BLEDevice::init("ESP64");
// 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_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
// Create a BLE Descriptor
// Creates BLE Descriptor 0x2902: Client Characteristic Configuration Descriptor (CCCD)
pCharacteristic->addDescriptor(new BLE2902());
// Adds also the Characteristic User Description - 0x2901 descriptor
descriptor_2901 = new BLE2901();
descriptor_2901->setDescription("My own description for this characteristic.");
descriptor_2901->setAccessPermissions(ESP_GATT_PERM_READ); // enforce read only - default is Read|Write
pCharacteristic->addDescriptor(descriptor_2901);
// 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() {
if (deviceConnected){
pCharacteristic->setValue(&txValue,1);
pCharacteristic->notify(true);
Serial.write(txValue);
txValue++;
delay(500);
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
// do stuff here on connecting
oldDeviceConnected = deviceConnected;
}
}
//------------------------------------------------------------
void Configurations(void)
{
// config ports
ConfigPorts();
Serial.begin(115200, SERIAL_8N1);
// config serial port
Serial1.begin(300, SERIAL_7E1,RXPIN,TXPIN);
}
//------------------------------------------------------------
void ConfigPorts(void)
{
pinMode(POWER_CHECK, INPUT);
pinMode(KEY_STATUS,INPUT);
pinMode(BATTERY_VOLTAGE, INPUT);
pinMode(OP_LED, OUTPUT);
pinMode(BT_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
pinMode(METER_SELECT,OUTPUT);
pinMode(SELF_SHUTDOWN,OUTPUT);
pinMode(BOOT,OUTPUT);
}