diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..d5f2741 Binary files /dev/null and b/.DS_Store differ 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){