Created a basic interface for BLE on ESP32
This commit is contained in:
parent
2aad32f6f3
commit
703a3e0c38
@ -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) {
|
||||
|
||||
@ -1,30 +1,9 @@
|
||||
// Include Section
|
||||
|
||||
#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 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 <BLEDevice.h>
|
||||
#include <BLEServer.h>
|
||||
#include <BLEUtils.h>
|
||||
#include <BLE2902.h>
|
||||
|
||||
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){
|
||||
|
||||
Loading…
Reference in New Issue
Block a user