From 703a3e0c380902796bfd4759b7b7af1ba7a828c0 Mon Sep 17 00:00:00 2001 From: Tempest Date: Fri, 1 Nov 2024 21:11:44 +0700 Subject: [PATCH] Created a basic interface for BLE on ESP32 --- .DS_Store | Bin 0 -> 6148 bytes .../SimpleBleDevice/SimpleBleDevice.ino | 3 +- .../esp32_test0/esp32_test0.ino | 118 +++++++++++++----- 3 files changed, 86 insertions(+), 35 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d5f27415f5624580f833dae2d6716ae65515bf00 GIT binary patch literal 6148 zcmeHKF>V4u3>*gvqBN8#_XSA#!Gey0B2D-}1W0KvoCJdUx;!UOOvYYE0xh%@!6SKR z&uib_PO&}%klA7L0*nESXv+A~p@y!jRd{gEfGqZoSIjZNCpL?=ioWd7+(*1IZ;A#d z`Q3Web}O#xr%h>-9|LE=0DJ7PUbJUX-` z=;_XNh~tL>p+K#G-VceUoSpl^c6D&D7JxjY+rYW?66B-~a(3cmLu_Zh$9ER*P{ekpKUp{=7mhI$2nEg+(B7LEYyF?ouJr#J69@%Dfq$xi z44Y*$#y@n`rN}KQPw9IanzsTPfy)mg47=xTJHgXXn1KrPFWe_!N HEfn|#h}S7= literal 0 HcmV?d00001 diff --git a/src/lightingFirmware/SimpleBleDevice/SimpleBleDevice.ino b/src/lightingFirmware/SimpleBleDevice/SimpleBleDevice.ino index 5f520fa..531d07a 100644 --- a/src/lightingFirmware/SimpleBleDevice/SimpleBleDevice.ino +++ b/src/lightingFirmware/SimpleBleDevice/SimpleBleDevice.ino @@ -71,8 +71,7 @@ void loop() { // 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 + mode = btmessage; } // disconnecting if (!deviceConnected && oldDeviceConnected) { diff --git a/src/lightingFirmware/esp32_test0/esp32_test0.ino b/src/lightingFirmware/esp32_test0/esp32_test0.ino index dfa62e3..a5c9dc5 100644 --- a/src/lightingFirmware/esp32_test0/esp32_test0.ino +++ b/src/lightingFirmware/esp32_test0/esp32_test0.ino @@ -1,30 +1,9 @@ // Include Section #include "esp_dmx.h" -#include "BluetoothSerial.h" -#include -#include -#include -// Name of Bluetooth client. -String BTDeviceName = "3D_SkyLED_LEDBoard"; -String BTMacAddress; -// Check if Bluetooth is available. -#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) -#error Bluetooth is not enabled! -#endif - -// Check if Serial Port Profile (SPP) is available. -#if !defined(CONFIG_BT_SPP_ENABLED) -#error Serial Port Profile for Bluetooth is not available or not enabled! -#endif -// Define Section -#define BT_DISCOVER_TIME 30000 #define INTERRUPT_PIN 0 -BluetoothSerial SerialBT; -char btmessage; - struct Button { const uint8_t PIN; uint32_t numberKeyPresses; @@ -38,6 +17,34 @@ struct Panel { int led3 = 0; }; +#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; + } +}; // Defining BOOT button on ESP32 as our built-in button. Button button1 = {INTERRUPT_PIN, 0, false}; @@ -47,14 +54,6 @@ const int modeAmount = 16; uint8_t brightnessMax = 20; uint8_t universalBrightness = 10; - -uint8_t data[DMX_PACKET_SIZE] = {0, - 0,universalBrightness,0,0, - 0,universalBrightness,0,0, - 0,universalBrightness,0,0, - 0,universalBrightness,0,0 -}; - uint8_t dataSeq[modeAmount][DMX_PACKET_SIZE] = { { @@ -256,21 +255,74 @@ void dmxSetup() { void setup() { Serial.begin(115200); - SerialBT.begin(BTDeviceName); - BTMacAddress = SerialBT.getBtAddressString(); - Serial.println(BTMacAddress.c_str()); + Serial.print("\nIf you receive this message, ESP32 module has finished setting up Serial Interface for communication."); + // Create the BLE Device + BLEDevice::init("Pupilometer LED Billboard"); + + // 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 + pCharacteristic->addDescriptor(new BLE2902()); + + // 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..."); + pinMode(INTERRUPT_PIN, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), isr, RISING); delay(2000); - 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; + Serial.println("Welcome to Pupilometer LED Billboard!"); } void loop() { + // Send the DMX packet. int modeOld = mode; + + // notify changed value + if (deviceConnected) { + btmessage = pCharacteristic->getValue().c_str(); + mode = btmessage.toInt(); + } + // 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; + } + serialRead(); + dmx_send(DMX_NUM_1); // Preparing next packet if (button1.pressed){