From 2aad32f6f3a5ab969f594888e1d206a11f54fc38 Mon Sep 17 00:00:00 2001 From: Glahera Date: Fri, 1 Nov 2024 16:47:39 +0700 Subject: [PATCH] Finished with prototyping pCharacteristic communication --- .../SimpleBleDevice/SimpleBleDevice.ino | 94 ++++++++++++++----- .../esp32_test0/esp32_test0.ino | 15 +-- 2 files changed, 80 insertions(+), 29 deletions(-) diff --git a/src/lightingFirmware/SimpleBleDevice/SimpleBleDevice.ino b/src/lightingFirmware/SimpleBleDevice/SimpleBleDevice.ino index a424df8..5f520fa 100644 --- a/src/lightingFirmware/SimpleBleDevice/SimpleBleDevice.ino +++ b/src/lightingFirmware/SimpleBleDevice/SimpleBleDevice.ino @@ -1,39 +1,89 @@ #include -#include #include - +#include +#include + +BLEServer* pServer = NULL; +BLECharacteristic* pCharacteristic = NULL; +bool deviceConnected = false; +bool oldDeviceConnected = false; +uint32_t value = 0; +String btmessage; + // 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); - Serial.println("Starting BLE work!"); - - BLEDevice::init("Long name works now"); - BLEServer *pServer = BLEDevice::createServer(); + + // 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); - BLECharacteristic *pCharacteristic = pService->createCharacteristic( - CHARACTERISTIC_UUID, - BLECharacteristic::PROPERTY_READ | - BLECharacteristic::PROPERTY_WRITE - ); - - pCharacteristic->setValue("Hello World says Neil"); + + // 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 + pCharacteristic->addDescriptor(new BLE2902()); + + // Start the service pService->start(); - // BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility + + // Start advertising BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); - pAdvertising->setScanResponse(true); - pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue - pAdvertising->setMinPreferred(0x12); + pAdvertising->setScanResponse(false); + pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter BLEDevice::startAdvertising(); - Serial.println("Characteristic defined! Now you can read it in your phone!"); + Serial.println("Waiting a client connection to notify..."); } void loop() { - // put your main code here, to run repeatedly: - delay(2000); + // notify changed value + if (deviceConnected) { + btmessage = pCharacteristic->getValue().c_str(); + Serial.print(btmessage); + delay(1000); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms + } + // 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; + } } \ No newline at end of file diff --git a/src/lightingFirmware/esp32_test0/esp32_test0.ino b/src/lightingFirmware/esp32_test0/esp32_test0.ino index b45c72c..dfa62e3 100644 --- a/src/lightingFirmware/esp32_test0/esp32_test0.ino +++ b/src/lightingFirmware/esp32_test0/esp32_test0.ino @@ -35,7 +35,7 @@ struct Panel { int led0; int led1; int led2; - int led3; + int led3 = 0; }; @@ -262,27 +262,28 @@ void setup() { pinMode(INTERRUPT_PIN, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), isr, RISING); delay(2000); - Serial.println("If you receive this message, ESP32 module has finished setting up Serial Interface for communication."); + Serial.print("\nIf you receive this message, ESP32 module has finished setting up Serial Interface for communication."); dmxSetup(); const dmx_port_t dmx_num = DMX_NUM_1; } void loop() { // Send the DMX packet. + int modeOld = mode; + serialRead(); dmx_send(DMX_NUM_1); // Preparing next packet if (button1.pressed){ if (mode < modeAmount - 1){mode++;} else {mode = 0;}; - Serial.printf("\nChanging to mode %d", mode); // Increment the value of each slot, excluding the start code. + // Increment the value of each slot, excluding the start code. button1.pressed = false; // Reset button status to FALSE }; + if (modeOld != mode){ + Serial.printf("\nChanging Lighting Preset to Preset %d", mode); + } // Wait until the packet is finished being sent before proceeding. dmx_wait_sent(DMX_NUM_1, DMX_TIMEOUT_TICK); // Now write the packet synchronously! dmx_write(DMX_NUM_1, dataSeq[mode], 100); serialRead(); - if (SerialBT.available()) { - btmessage=SerialBT.read(); - Serial.print(btmessage); - }; }