Finished with prototyping pCharacteristic communication
This commit is contained in:
parent
8f601ec030
commit
2aad32f6f3
@ -1,39 +1,89 @@
|
|||||||
#include <BLEDevice.h>
|
#include <BLEDevice.h>
|
||||||
#include <BLEUtils.h>
|
|
||||||
#include <BLEServer.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:
|
// See the following for generating UUIDs:
|
||||||
// https://www.uuidgenerator.net/
|
// https://www.uuidgenerator.net/
|
||||||
|
|
||||||
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
|
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
|
||||||
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
|
#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() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.println("Starting BLE work!");
|
|
||||||
|
// Create the BLE Device
|
||||||
BLEDevice::init("Long name works now");
|
BLEDevice::init("ESP32");
|
||||||
BLEServer *pServer = BLEDevice::createServer();
|
|
||||||
|
// Create the BLE Server
|
||||||
|
pServer = BLEDevice::createServer();
|
||||||
|
pServer->setCallbacks(new MyServerCallbacks());
|
||||||
|
|
||||||
|
// Create the BLE Service
|
||||||
BLEService *pService = pServer->createService(SERVICE_UUID);
|
BLEService *pService = pServer->createService(SERVICE_UUID);
|
||||||
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
|
|
||||||
CHARACTERISTIC_UUID,
|
// Create a BLE Characteristic
|
||||||
BLECharacteristic::PROPERTY_READ |
|
pCharacteristic = pService->createCharacteristic(
|
||||||
BLECharacteristic::PROPERTY_WRITE
|
CHARACTERISTIC_UUID,
|
||||||
);
|
BLECharacteristic::PROPERTY_READ |
|
||||||
|
BLECharacteristic::PROPERTY_WRITE |
|
||||||
pCharacteristic->setValue("Hello World says Neil");
|
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();
|
pService->start();
|
||||||
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
|
|
||||||
|
// Start advertising
|
||||||
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
||||||
pAdvertising->addServiceUUID(SERVICE_UUID);
|
pAdvertising->addServiceUUID(SERVICE_UUID);
|
||||||
pAdvertising->setScanResponse(true);
|
pAdvertising->setScanResponse(false);
|
||||||
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
|
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
|
||||||
pAdvertising->setMinPreferred(0x12);
|
|
||||||
BLEDevice::startAdvertising();
|
BLEDevice::startAdvertising();
|
||||||
Serial.println("Characteristic defined! Now you can read it in your phone!");
|
Serial.println("Waiting a client connection to notify...");
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// put your main code here, to run repeatedly:
|
// notify changed value
|
||||||
delay(2000);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -35,7 +35,7 @@ struct Panel {
|
|||||||
int led0;
|
int led0;
|
||||||
int led1;
|
int led1;
|
||||||
int led2;
|
int led2;
|
||||||
int led3;
|
int led3 = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -262,27 +262,28 @@ void setup() {
|
|||||||
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);
|
||||||
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();
|
dmxSetup();
|
||||||
const dmx_port_t dmx_num = DMX_NUM_1;
|
const dmx_port_t dmx_num = DMX_NUM_1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// Send the DMX packet.
|
// Send the DMX packet.
|
||||||
|
int modeOld = mode;
|
||||||
|
serialRead();
|
||||||
dmx_send(DMX_NUM_1);
|
dmx_send(DMX_NUM_1);
|
||||||
// Preparing next packet
|
// Preparing next packet
|
||||||
if (button1.pressed){
|
if (button1.pressed){
|
||||||
if (mode < modeAmount - 1){mode++;} else {mode = 0;};
|
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
|
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.
|
// Wait until the packet is finished being sent before proceeding.
|
||||||
dmx_wait_sent(DMX_NUM_1, DMX_TIMEOUT_TICK);
|
dmx_wait_sent(DMX_NUM_1, DMX_TIMEOUT_TICK);
|
||||||
// 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);
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user