What would you do if you don't have an Android device, but you still want to build Bluetooth® Low Energy Skydive Altimeter? Use ESP32 to adjust altitude on RSL10-SENSE-DB-GEVK and OLED display, of course.
Software:Instead of building an app, we will use Arduino IDE to write a sketch to control RSL10-SENSE-DB-GEVK. I started with the BLE Client example and modified it to meet the requirements for this project.
We start the code with some declarations:
#include "BLEDevice.h"
static BLEUUID ServiceUUID("e093f3b5-00a3-a9e5-9eca-40016e0edc24");
static BLEUUID WriteCharacteristicUUID("e093f3b7-00a3-a9e5-9eca-40036e0edc24");
static String ThisMAC = "60:c0:bf:28:99:7d";
static BLERemoteCharacteristic* WriteCharacteristic;
static BLEAdvertisedDevice* ThisDevice;
static boolean Connected = false;
We need a call back for when ESP32 finds a BLE device. Once we find our RSL10-SENSE-DB-GEVK by MAC address we need to stop the scan.
class AdvertisedDeviceCB: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
if ((String)advertisedDevice.getAddress().toString().c_str() == ThisMAC) {
BLEDevice::getScan()->stop();
ThisDevice = new BLEAdvertisedDevice(advertisedDevice);
}
}
};
Next, we need a call back to capture BLE connecting and disconnecting. This will also give a visual confirmation using the on board blue LED which will illuminate when a connection is made.
class ClientCB : public BLEClientCallbacks {
void onConnect(BLEClient* ThisClient) {
Connected = true;
digitalWrite(LED_BUILTIN, HIGH);
}
void onDisconnect(BLEClient* ThisClient) {
Connected = false;
digitalWrite(LED_BUILTIN, LOW);
}
};
Here we initialize our buttons and start BLE scan:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(22, INPUT_PULLUP);
pinMode(23, INPUT_PULLUP);
BLEDevice::init("");
BLEScan* ThisBLEScan = BLEDevice::getScan();
ThisBLEScan->setAdvertisedDeviceCallbacks(new AdvertisedDeviceCB());
ThisBLEScan->setInterval(1349);
ThisBLEScan->setWindow(449);
ThisBLEScan->setActiveScan(true);
ThisBLEScan->start(5, false);
}
Finally, we create the client and wait for the connection to be made then specify the service and characteristics we are interested in. Base on which button we press, we will either add or subtract 50 feet from the number on the display.
void loop() {
if (!Connected) {
BLEClient* ThisClient = BLEDevice::createClient();
do {
delay(1000);
ThisClient->setClientCallbacks(new ClientCB());
ThisClient->connect(ThisDevice);
} while (!Connected);
BLERemoteService* ThisRemoteService = ThisClient->getService(ServiceUUID);
WriteCharacteristic = ThisRemoteService->getCharacteristic(WriteCharacteristicUUID);
}
else {
if (digitalRead(22) == LOW) {
WriteCharacteristic->writeValue("+50", 3);
delay(200);
}
if (digitalRead(23) == LOW) {
WriteCharacteristic->writeValue("-50", 3);
delay(200);
}
}
}
Demo:
Comments