Added Example for Simple BLE device

This commit is contained in:
Tempest 2024-10-31 18:55:33 +07:00
parent 8b2a97b364
commit 8f601ec030
3 changed files with 80 additions and 5 deletions

View File

@ -0,0 +1,39 @@
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// 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"
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
BLEDevice::init("Long name works now");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setValue("Hello World says Neil");
pService->start();
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println("Characteristic defined! Now you can read it in your phone!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}

View File

@ -0,0 +1,6 @@
{
"requires": [
"CONFIG_BT_ENABLED=y",
"CONFIG_BLUEDROID_ENABLED=y"
]
}

View File

@ -1,19 +1,43 @@
// Include Section // Include Section
#include "esp_dmx.h" #include "esp_dmx.h"
#include "BluetoothSerial.h"
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// 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 Section
#define BT_DISCOVER_TIME 30000
#define ledPin 2
#define INTERRUPT_PIN 0 #define INTERRUPT_PIN 0
BluetoothSerial SerialBT;
char btmessage;
struct Button { struct Button {
const uint8_t PIN; const uint8_t PIN;
uint32_t numberKeyPresses; uint32_t numberKeyPresses;
bool pressed; bool pressed;
}; };
struct Panel [led0,led1,led2,led3]; struct Panel {
int led0;
int led1;
int led2;
int led3;
};
// Defining BOOT button on ESP32 as our built-in button. // Defining BOOT button on ESP32 as our built-in button.
Button button1 = {INTERRUPT_PIN, 0, false}; Button button1 = {INTERRUPT_PIN, 0, false};
@ -150,7 +174,7 @@ uint8_t dataSeq[modeAmount][DMX_PACKET_SIZE] =
{ {
0, 0,
//Start Inner Round //Start Inner Round
universalBrightness,0,0,0, 0,0,universalBrightness,0,
0,0,0,0, 0,0,0,0,
0,0,0,0, 0,0,0,0,
0,0,0,0, 0,0,0,0,
@ -232,7 +256,9 @@ void dmxSetup() {
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
pinMode(ledPin,OUTPUT); SerialBT.begin(BTDeviceName);
BTMacAddress = SerialBT.getBtAddressString();
Serial.println(BTMacAddress.c_str());
pinMode(INTERRUPT_PIN, INPUT_PULLUP); pinMode(INTERRUPT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), isr, RISING); attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), isr, RISING);
delay(2000); delay(2000);
@ -255,4 +281,8 @@ void loop() {
// Now write the packet synchronously! // Now write the packet synchronously!
dmx_write(DMX_NUM_1, dataSeq[mode], 100); dmx_write(DMX_NUM_1, dataSeq[mode], 100);
serialRead(); serialRead();
if (SerialBT.available()) {
btmessage=SerialBT.read();
Serial.print(btmessage);
};
} }