arduino esp32 ble app案例
时间: 2023-08-27 12:18:25 浏览: 176
arduino ESP32环境包
当使用Arduino ESP32和BLE时,你可以开发很多有趣的应用程序。下面是一个简单的Arduino ESP32 BLE应用程序案例,它使用一个智能手机应用程序来控制LED灯的开关。
硬件所需材料:
- Arduino ESP32开发板
- 一个LED灯
- 一个220欧姆电阻
- 面包板和杜邦线
软件所需材料:
- Arduino IDE
- Blynk App
步骤:
1. 使用Arduino IDE将以下代码上传到你的ESP32开发板中:
```
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;
int ledPin = 2; // GPIO 2
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(115200);
BLEDevice::init("LED Controller");
BLEServer *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 = pServer->getAdvertising();
pAdvertising->addServiceUUID(pService->getUUID());
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06);
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println("Waiting for a client connection to notify...");
}
void loop() {
// notify changed value
if (deviceConnected) {
pCharacteristic->setValue("1");
pCharacteristic->notify();
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
// 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;
}
}
```
2. 打开Blynk App,并创建一个新的项目。在该项目中添加一个按钮小部件,并将其设置为控制器模式。将控制器的输出引脚设置为虚拟引脚V1。
3. 将以下代码添加到Arduino IDE中:
```
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32_BLE.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
}
BLYNK_WRITE(V1) {
int buttonState = param.asInt();
if (buttonState == 1) {
Serial.println("LED ON");
BLEDevice::getAdvertising()->stop();
BLEDevice::getAdvertising()->start();
} else {
Serial.println("LED OFF");
}
}
```
4. 将项目编译并上传到ESP32开发板中。
5. 在Blynk App中,点击按钮控制器来控制LED灯的开关。
这个应用程序演示了如何使用Arduino ESP32和BLE来控制一个简单的电路。当你点击Blynk App中的按钮控制器时,它会向ESP32发送一个指令,以控制LED灯的开关。由于BLE的低功耗特性,这个应用程序非常适合于物联网应用,因为它可以运行在电池供电的设备上,而且具有长时间的使用寿命。
阅读全文