- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从事一个涉及 2 个 ESP32 Wemos D1 迷你板的项目。我正在使用 BLE 功能将传感器读数从“服务器”传输到“客户端”。我向接收传感器读数的客户端使用特征通知。如果我想对服务器实现深度 sleep 功能,客户端会发生什么情况?客户端是否也必须在某个时候重置?另外,是否建议在深度 sleep 场景中使用 Characteristic.Notify?
谢谢。
服务器代码:
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT22
#define uS_TO_S_FACTOR 1000000 //Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP 15 //Time ESP32 will go to sleep (in seconds)
RTC_DATA_ATTR int bootCount = 0;
DHT dht(DHTPIN, DHTTYPE);
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
uint8_t txValue = 50;
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
//Function that prints the reason by which ESP32 has been awaken from sleep
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case 3 : Serial.println("Wakeup caused by timer"); break;
case 4 : Serial.println("Wakeup caused by touchpad"); break;
case 5 : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.println("Wakeup was not caused by deep sleep"); break;
}
}
void setup() {
Serial.begin(115200);
Serial.println(F("initating DHT22..."));
dht.begin();
// Create the BLE Device
BLEDevice::init("UART"); // Name must not be longer than 5 chars!!!
// Create the BLE Server
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY|BLECharacteristic::PROPERTY_READ|BLECharacteristic::PROPERTY_WRITE
);
BLE2902 *desc = new BLE2902();
desc->setNotifications(true);
pCharacteristic->addDescriptor(desc);
// Start the service
pService->start();
pServer->getAdvertising()->addServiceUUID(SERVICE_UUID);
// Start advertising
pServer->getAdvertising()->start();
Serial.println(pService->getUUID().toString().c_str());
Serial.println("Waiting a client connection to notify...");
if (deviceConnected) {
float f = dht.readTemperature(true);
char fStr[10];
sprintf(fStr, "%4.4f", f);
Serial.print("Temperature reading: ");
Serial.println(fStr);
Serial.printf("*** Sent Value: %d ***\n", fStr);
pCharacteristic->setValue(fStr);
pCharacteristic->notify();
//Set timer to 5 seconds
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
" Seconds");
//Go to sleep now
esp_deep_sleep_start();
}
//delay(60000);
}
void loop() {}
客户端代码:
#include "BLEDevice.h"
#include <WiFi.h>
// The remote service we wish to connect to.
static BLEUUID serviceUUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
static BLEUUID charUUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E");
static BLEAddress *pServerAddress;
static boolean doConnect = false;
static boolean connected = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
const char* ssid = "Kings";
const char* password = "GoCanada";
const char* host = "menezes-service.herokuapp.com";
WiFiClient client;
static void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData,
size_t length,
bool isNotify) {
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
Serial.print(" of data length ");
Serial.println(length);
//std::string value = pBLERemoteCharacteristic->readValue();
byte buffer[42];
Serial.print("The characteristic value sent was: ");
//Serial.println(pBLERemoteCharacteristic->readValue().c_str());
//Serial.println(pBLERemoteCharacteristic->readUInt8());
std::string farhenheight = pRemoteCharacteristic->readValue();
Serial.print("Farheinheight: ");
Serial.println(farhenheight.c_str());
Serial.println(F("Posting to api!"));
Serial.println();
Serial.println("closing connection");
}
class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclient) {
Serial.println("connected again ... ");
}
void onDisconnect(BLEClient* pclient) {
connected = false;
Serial.println("onDisconnect");
}
};
bool connectToServer(BLEAddress pAddress) {
Serial.print("Forming a connection to ");
Serial.println(pAddress.toString().c_str());
BLEClient* pClient = BLEDevice::createClient();
Serial.println(" - Created client");
pClient->setClientCallbacks(new MyClientCallback());
// Connect to the remove BLE Server.
pClient->connect(pAddress);
Serial.println(" - Connected to server");
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
Serial.println(pRemoteService->toString().c_str());
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID.toString().c_str());
return false;
}
Serial.println(" - Found our service");
// Obtain a reference to the characteristic in the service of the remote BLE server.
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (pRemoteCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(charUUID.toString().c_str());
return false;
}
Serial.println(" - Found our characteristic");
pRemoteCharacteristic->registerForNotify(notifyCallback);
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.print("BLE Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());
//Serial.print(advertisedDevice.haveServiceUUID());
if(advertisedDevice.haveServiceUUID()){
Serial.println(advertisedDevice.getServiceUUID().toString().c_str());
}
// We have found a device, let us now see if it contains the service we are looking for.
if (advertisedDevice.haveServiceUUID() && advertisedDevice.getServiceUUID().equals(serviceUUID)) {
//
Serial.print("Found our device! address: ");
advertisedDevice.getScan()->stop();
pServerAddress = new BLEAddress(advertisedDevice.getAddress());
doConnect = true;
} // Found our server
} // onResult
}; // MyAdvertisedDeviceCallbacks
void setup() {
Serial.begin(115200);
Serial.println("Starting Arduino BLE Client application...");
BLEDevice::init("");
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
pBLEScan->start(30);
} // End of setup.
// This is the Arduino main loop function.
void loop() {
if (doConnect == true) {
if (connectToServer(*pServerAddress)) {
Serial.println("We are now connected to the BLE Server.");
connected = true;
} else {
Serial.println("We have failed to connect to the server; there is nothin more we will do.");
}
//doConnect = false;
}
if (connected == false){
BLEDevice::getScan()->start(0);
}
else{
doConnect = false;
}
delay(1000); // Delay a second between loops.
} // End of loop
最佳答案
请在下面查看您的问题的答案:-
如果我想对服务器实现深度 sleep 功能,客户端会发生什么情况?客户端是否也必须在某个时候重置?
不,客户端不必重置,但您可能必须重新连接到服务器,因为在深度 sleep 中 BLE 连接丢失。如果您希望服务器一唤醒就自动建立连接,那么您必须更新客户端代码,以便它不断尝试重新连接到服务器。这样,一旦服务器唤醒,客户端就会连接到它并继续接收通知。
另外,是否建议在深度 sleep 场景中使用 Characteristic.Notify?
在深度 sleep 中,CPU 将关闭,因此您将无法在此状态下发送通知。在此模式下,您将只能通过定时器或外部外围设备(例如触摸引脚)唤醒 CPU。请参阅下面的链接以获取更多信息:-
希望对您有所帮助。
关于arduino - ESP32 Arduino BLE 深度 sleep ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61811536/
我正在开发一个使用 BLE Android 到 iOS 的聊天应用程序,现在我正在使用下面两个库作为引用 https://github.com/izumin5210/Bletia https://gi
我正在从 HCI 套接字接收 EVT_LE_ADVERTISING_REPORT。我想区分 BLE 信标和普通 BLE 设备(解析设备名称等)我有点困惑是否所有 BLE 设备都会发出信标或者它们是不同
我正在从 HCI 套接字接收 EVT_LE_ADVERTISING_REPORT。我想区分 BLE 信标和普通 BLE 设备(解析设备名称等)我有点困惑是否所有 BLE 设备都会发出信标或者它们是不同
我在我的 iOS 应用程序项目中使用核心蓝牙框架。我有以下关于蓝牙低功耗的问题 - iOS 中单个中央设备是否可以连接多个外围设备? 多个中央设备可以与单个外围设备连接吗? 单个 iOS 设备可以同时
我正在创建一个 iOS 和一个 Android 应用程序,它们从蓝牙传感器读取一些数据并将它们保存在数据库中。 即使应用程序终止,我也想提供保存传感器数据的能力。 仅供引用。当应用程序在后台时,我已经
我正在使用核心蓝牙框架并扫描一些设备,例如 micromax A250、micromax A116、samsung grand neo、HTC 610 和 ipod 5s,然后我无法扫描 samsun
这个任务的目的是通过BLE设备连接iPhone的BLE,访问iphone的ANCS。请注意,iPhone 中没有安装应用程序来打开 iPhone 的 BLE。那么,如果我们从 iPhone 设置中启用
我在为我的 BLE 设备开发安卓软件时遇到了问题。我的软件可以找到我的设备和 GATT 服务,但在我的服务中找不到任何特征。 我检查了 android-sdk-4.4.2 源码,找到了一些代码。 ht
有什么方法可以从 BleExplr、LightBlue 等通用 BLE 扫描应用程序中隐藏 BLE 设备? 最佳答案 您可以配置外围设备以使用服务请求。在这种模式下,中央为它们提供服务和外设扫描 -
我有 3 个组件。 Activity1 有连接和断开 BLE 连接的按钮 Activity2 需要从 BLE 设备获取数据。 Service 所有连接逻辑(如 getRemoteDevice()、co
我有一个 BLE 设备,它在通过相当标准的用户界面(点击 UITableView 中显示的设备条目)选择后连接到 iOS 设备。 连接非常简单 - 一些内部处理,然后调用 CBCentralManag
我正在尝试使用 Meteor 和这个 Cordova 插件 - https://github.com/don/cordova-plugin-ble-central - 使用 meteor add co
我坚持在 Android Lollipop 智能手机和 BLE 设备(带有 BLE 模块的 TI 实验板)之间实现连接。我使用以下调用进行连接: device.connectGatt(context,
我正在使用 react-native-ble-plx 在我的应用程序中实现蓝牙 蓝牙扫描工作正常,但它在 android 中包含重复项,而在 iOS 中工作正常,因为 allowDuplicates
我正在尝试在我的 Windows 笔记本电脑上设置一个基于 Nodejs 的演示,并使用额外的 BLE 适配器将我的笔记本电脑连接到另一个 BLE 设备 (Anki Overdrive)。我在互联网上
我正在研究基于 Android 的 BLE 接近感应功能,需要一些信息。目前我看到没有适用于 android 的 BLE 信标制造商。到目前为止,我为 iPhone 找到了 2 个。1) http:/
我正在使用 cordova 和 BLE 插件开发一个应用程序。我想通过 BLE 根据硬编码的已知 device.name 自动连接到 ESP32,而无需用户按下连接按钮。 我的想法是: 在设备准备就绪
我正在尝试实现从/向蓝牙设备接收和发送消息的模块。 我一直在寻找可以通过蓝牙搜索、连接和发送消息的 NuGet 包,但找不到适用于 Linux 的任何东西。 我正在使用 .NET Core 2.1 和
我正在尝试寻找一种方法来了解如何在 iOS 中获取 BLE mac??? 这将适用于所有 BLE,不会存在制造商依赖性。我们正在寻找扫描时间的解决方案。我可以在扫描时间内区分 BLE 吗? 如果获取M
我已阅读技术规范并试图了解为什么 BLE 4.2 比 BLE 4.1 更快? 我们能否发送大于 20 字节的数据包或者连接间隔是否更快? 我想了解是什么让 BLE 4.2 更快。 最佳答案 与早期相比
我是一名优秀的程序员,十分优秀!