This is the scenario. Arduino A has button A attached. When button A is pressed it changes LED A ON and LED B Off. When clicked again LED A OFF and LED B ON. I have this along with some other aspects totally under control and working. The next step I'm trying to accomplish is to incorporate BLE Notify into the arduino. When the button is clicked and LED A turns on I would like a BLE notification to send something ('G' for example). Then if the button is pressed again a BLE notification sends something different ('R').
I tried combining the 'BLE_Notify' Sketch example into my code but when I connect the Arduino to a BLE phone app I receive endless amounts of changed notifications without doing anything. Below is my combined code.
#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;
#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;
}
};
const int bt1 = 27;
boolean bt1g = true;
int bt1t = 0;
void setup() {
pinMode(33, OUTPUT);
pinMode(15, OUTPUT);
pinMode(bt1, INPUT_PULLUP);
Serial.begin(9600);
BLEDevice::init("ESP32");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
pCharacteristic->addDescriptor(new BLE2902());
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0);
BLEDevice::startAdvertising();
Serial.println("Waiting a client connection to notify...");
}
void loop()
{
if (deviceConnected) {
pCharacteristic->setValue((uint8_t*)&value, 4);
pCharacteristic->notify();
value++;
delay(3); /
}
if (!deviceConnected && oldDeviceConnected) {
delay(500);
pServer->startAdvertising();
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
}
if (deviceConnected && !oldDeviceConnected) {
oldDeviceConnected = deviceConnected;
}
if (bt1g) {
if (digitalRead(bt1) == LOW ) {
bt1t = (bt1t + 1) % 2;
Serial.println(bt1t);
bt1g = false;
}
}
if (!bt1g) {
if (digitalRead(bt1) == HIGH) {
bt1g = true;
}
}
if (bt1t == 0) {
digitalWrite(33, LOW);
digitalWrite(15, HIGH);
pCharacteristic->setValue(0);
pCharacteristic->notify();
value++;
}
if (bt1t == 1) {
digitalWrite(33, HIGH);
digitalWrite(15, LOW);
pCharacteristic->setValue(1);
pCharacteristic->notify();
value++;
}
delay(50);
}
I am very new to Arduino and C++ environment so sorry if my code is bad.