如果您没有 Android 设备,但仍想构建蓝牙® 低能耗跳伞高度计,您会怎么做?当然,使用 ESP32 在 RSL10-SENSE-DB-GEVK 和 OLED 显示器上调整高度。
我们将使用 Arduino IDE 编写程序来控制 RSL10-SENSE-DB-GEVK,而不是构建应用程序。我从 BLE 客户端示例开始并对其进行了修改以满足该项目的要求。
我们以一些声明开始代码:
#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;
当 ESP32 找到 BLE 设备时,我们需要回调。一旦我们通过 MAC 地址找到我们的 RSL10-SENSE-DB-GEVK,我们需要停止扫描。
class AdvertisedDeviceCB: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
if ((String)advertisedDevice.getAddress().toString().c_str() == ThisMAC) {
BLEDevice::getScan()->stop();
ThisDevice = new BLEAdvertisedDevice(advertisedDevice);
}
}
};
接下来,我们需要回调来捕获 BLE 连接和断开连接。这也将使用板载蓝色 LED 进行视觉确认,当建立连接时该 LED 会亮起。
class ClientCB : public BLEClientCallbacks {
void onConnect(BLEClient* ThisClient) {
Connected = true;
digitalWrite(LED_BUILTIN, HIGH);
}
void onDisconnect(BLEClient* ThisClient) {
Connected = false;
digitalWrite(LED_BUILTIN, LOW);
}
};
在这里,我们初始化我们的按钮并开始 BLE 扫描:
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);
}
最后,我们创建客户端并等待建立连接,然后指定我们感兴趣的服务和特性。根据我们按下的按钮,我们将从显示屏上的数字中增加或减少 50 英尺。
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);
}
}
}
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
全部0条评论
快来发表一下你的评论吧 !