Finalized BLE communication

This commit is contained in:
Tempest 2024-11-15 11:46:47 +07:00
parent 2a30a98144
commit 245eb45ff1
2 changed files with 63 additions and 39 deletions

BIN
src/lightingFirmware/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -1,7 +1,7 @@
// Include Section // Include Section
#include "esp_dmx.h" #include "esp_dmx.h"
#include "UUID.h"
#define INTERRUPT_PIN 0 #define INTERRUPT_PIN 0
struct Button { struct Button {
@ -23,7 +23,9 @@ struct Panel {
#include <BLE2902.h> #include <BLE2902.h>
BLEServer* pServer = NULL; BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL; BLECharacteristic* pCharacteristic1 = NULL;
BLECharacteristic* pCharacteristic2 = NULL;
bool deviceConnected = false; bool deviceConnected = false;
bool oldDeviceConnected = false; bool oldDeviceConnected = false;
uint32_t value = 0; uint32_t value = 0;
@ -32,9 +34,10 @@ String btmessage;
// See the following for generating UUIDs: // See the following for generating UUIDs:
// https://www.uuidgenerator.net/ // https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" uint16_t SERVICE_UUID = 20241115;
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" const int panelAmount = 25;
#define CHARACTERISTIC_UUIDS {"beb5483e-36e1-4688-b7f5-ea07361b26a8","beb5483e-36e1-4688-b7f5-ea07361b26a9","beb5483e-36e1-4688-b7f5-ea07361b26b0"} BLECharacteristic* pCharacteristics[panelAmount];
char* CHARACTERISTIC_UUIDS[panelAmount];
class MyServerCallbacks: public BLEServerCallbacks { class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) { void onConnect(BLEServer* pServer) {
@ -45,21 +48,17 @@ class MyServerCallbacks: public BLEServerCallbacks {
deviceConnected = false; deviceConnected = false;
} }
}; };
//void setupCharacteristic(BLEService *pService){
//Function for setting up BLE Characteristic // for(int i = 0; i < panelAmount - 1; i++){
const int panelAmount = 3; // pCharacteristics[i] = pService->createCharacteristic(
BLECharacteristic* pCharacteristics[] = {}; // CHARACTERISTIC_UUIDS[i],
void setupCharacteristic(pService){ // BLECharacteristic::PROPERTY_READ |
for(int i = 0; i < panelAmount - 1; i++){ // BLECharacteristic::PROPERTY_WRITE |
pCharacteristics[i] = pService->createCharacteristic( // BLECharacteristic::PROPERTY_NOTIFY |
CHARACTERISTIC_UUIDS[i], // BLECharacteristic::PROPERTY_INDICATE
BLECharacteristic::PROPERTY_READ | // );
BLECharacteristic::PROPERTY_WRITE | // };
BLECharacteristic::PROPERTY_NOTIFY | //};
BLECharacteristic::PROPERTY_INDICATE
);
};
};
// Defining BOOT button on ESP32 as our built-in button. // Defining BOOT button on ESP32 as our built-in button.
@ -281,20 +280,26 @@ void setup() {
pServer->setCallbacks(new MyServerCallbacks()); pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service // Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID); BLEService *pService = pServer->createService(SERVICE_UUID,52);
// Create a BLE Characteristic // Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID, for (uint32_t i = 0; i < panelAmount; i++){
BLECharacteristic::PROPERTY_READ | UUID uuid;
BLECharacteristic::PROPERTY_WRITE | uuid.seed(i+1);
BLECharacteristic::PROPERTY_NOTIFY | uuid.generate();
BLECharacteristic::PROPERTY_INDICATE Serial.printf("Creating BLE Characteristic with UUID %s ...", uuid.toCharArray());
);
pCharacteristics[i] = pService->createCharacteristic(
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml i+1,
// Create a BLE Descriptor BLECharacteristic::PROPERTY_READ |
pCharacteristic->addDescriptor(new BLE2902()); BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
// pCharacteristics[i]->addDescriptor(new BLE2902());
Serial.printf("Done\n");
};
// Start the service // Start the service
pService->start(); pService->start();
@ -305,7 +310,6 @@ void setup() {
pAdvertising->setScanResponse(false); pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
BLEDevice::startAdvertising(); BLEDevice::startAdvertising();
Serial.println("\nWaiting a client connection to notify...");
pinMode(INTERRUPT_PIN, INPUT_PULLUP); pinMode(INTERRUPT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), isr, RISING); attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), isr, RISING);
@ -318,14 +322,17 @@ void loop() {
dmx_send(DMX_NUM_1); dmx_send(DMX_NUM_1);
// Save Old Mode // Save Old Mode
int modeOld = mode; int modeOld = mode;
uint8_t* btMessage[panelAmount];
uint8_t dmxData[DMX_PACKET_SIZE] = {0};
// notify changed value // notify changed value
if (deviceConnected) { if (deviceConnected) {
Serial.printf("\nI received the following bytes: "); Serial.printf("\nI received the following bytes: ");
uint8_t* btMessage = pCharacteristic->getData(); for (int i = 0; i < panelAmount;i++){
for (int i = 0; i < sizeof(btMessage); i++){ btMessage[i] = pCharacteristics[i]->getData();
Serial.printf("%d ",btMessage[i]); for (int j = 0; j < sizeof(btMessage[i]); j++){
Serial.printf("%d ",btMessage[i][j]);
}; };
};
} }
// disconnecting // disconnecting
if (!deviceConnected && oldDeviceConnected) { if (!deviceConnected && oldDeviceConnected) {
@ -349,8 +356,25 @@ void loop() {
if (modeOld != mode){ if (modeOld != mode){
Serial.printf("\nChanging Lighting Preset to Preset %d", mode); Serial.printf("\nChanging Lighting Preset to Preset %d", mode);
} }
// Serial.printf("\nConstructing Payload using ");
if (deviceConnected){
// Serial.printf("Bluetooth Data ...");
for (int i = 0; i < panelAmount; i++){
for (int j = 0; j < sizeof(btMessage[i]); j++){
dmxData[i*4 + j + 1] = btMessage[i][j];
// Serial.printf(".");
};
};
}
else{
// Serial.printf("Preset Data ...");
for (int i = 0; i < sizeof(dataSeq[mode]); i++){
dmxData[i] = dataSeq[mode][i];
};
};
// Serial.printf(" Done");
// Wait until the packet is finished being sent before proceeding. // Wait until the packet is finished being sent before proceeding.
dmx_wait_sent(DMX_NUM_1, DMX_TIMEOUT_TICK); dmx_wait_sent(DMX_NUM_1, DMX_TIMEOUT_TICK);
// Now write the packet synchronously! // Now write the packet synchronously!
dmx_write(DMX_NUM_1, dataSeq[mode], 100); dmx_write(DMX_NUM_1, dmxData, 100);
} }