如何解决用于深度睡眠的 ESP32 触摸传感器
我正在尝试唤醒我的 esp32 以通过 nRF Connect 中的蓝牙接收数据,但我没有温度、湿度和压力的值。我不知道为什么我什么也没看到,因为我的传感器(DHT22 和 BMP280)正确连接并且它们在串行监视器中传输良好的值,但在应用程序值字段中为空。我试图在回调函数中放入特性或在设置中,但同样的问题,空值。问题是什么?任何人都可以给我建议吗? 我在下面附上我的代码。提前致谢!
#include <BLEDevice.h>
#include <BLEServer.h>
#include <bleutils.h>
//#include <BLE2902.h>
#include "DHT.h"
//#include <Wire.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_Sensor.h>
#include <WiFi.h>
#include <HTTPClient.h>
BLEServer* pServer = NULL;
BLECharacteristic* temp_pCharacteristic = NULL;
BLECharacteristic* hum_pCharacteristic = NULL;
BLECharacteristic* pres_pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302),AM2321
DHT dht(DHTPIN,DHTTYPE);
Adafruit_BMP280 bmp; // Instantiate (create) a BMP280 object and set-up for I2C operation
#define Threshold 40 /* Greater the value,more the sensitivity */
RTC_DATA_ATTR int bootCount = 0;
touch_pad_t touchPin;
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void ondisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void init_sensors(){
dht.begin();
bool status;
status = bmp.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BMP280 sensor,check wiring!");
while (1);
}
}
/*
Method to print 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 ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO");
break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL");
break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}
/*
Method to print the touchpad by which ESP32
has been awaken from sleep
*/
void print_wakeup_touchpad(){
touchPin = esp_sleep_get_touchpad_wakeup_status();
switch(touchPin)
{
case 0 : Serial.println("Touch detected on GPIO 4"); break;
case 1 : Serial.println("Touch detected on GPIO 0"); break;
case 2 : Serial.println("Touch detected on GPIO 2"); break;
case 3 : Serial.println("Touch detected on GPIO 15"); break;
case 4 : Serial.println("Touch detected on GPIO 13"); break;
case 5 : Serial.println("Touch detected on GPIO 12"); break;
case 6 : Serial.println("Touch detected on GPIO 14"); break;
case 7 : Serial.println("Touch detected on GPIO 27"); break;
case 8 : Serial.println("Touch detected on GPIO 33"); break;
case 9 : Serial.println("Touch detected on GPIO 32"); break;
default : Serial.println("Wakeup not by touchpad"); break;
}
}
void callback(){
//placeholder callback function
// notify changed value
/* init_sensors();
if (deviceConnected){
uint32_t t = dht.readTemperature();
uint32_t h = dht.readHumidity();
uint32_t p = bmp.readPressure();
temp_pCharacteristic->setValue(t);
temp_pCharacteristic->notify();
hum_pCharacteristic->setValue(h);
hum_pCharacteristic->notify();
pres_pCharacteristic->setValue(p);
pres_pCharacteristic->notify();
delay(2000); // bluetooth stack will go into congestion,if too many packets are sent,in 6
hours test i was able to go as low as 3ms
} */
}
void setup(){
Serial.begin(115200);
//Take some time to open up the Serial Monitor
init_sensors(); //apelarea functiei pentru configurarea senzorilor
delay(2000);
// float temp = dht.readTemperature();
// float hum = dht.readHumidity();
// float pres = bmp.readPressure();
// Create the BLE Device
BLEDevice::init("T");
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(bleuUID((uint16_t)0x181A));
// Create a BLE Characteristic
temp_pCharacteristic = pService->createCharacteristic(
bleuUID((uint16_t)0x2A6E),BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
hum_pCharacteristic = pService->createCharacteristic(
bleuUID((uint16_t)0x2A6F),BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
pres_pCharacteristic = pService->createCharacteristic(
bleuUID((uint16_t)0x2A6D),BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
// Create a BLE Descriptor
//hum_pCharacteristic->addDescriptor(new BLE2902());
// Start the service
pService->start();
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(bleuUID((uint16_t)0x181A));
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
BLEDevice::startAdvertising();
Serial.println("Waiting a client connection to notify...");
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
//Print the wakeup reason for ESP32 and touchpad too
print_wakeup_reason();
print_wakeup_touchpad();
//Setup interrupt on Touch Pad 3 (GPIO15)
touchAttachInterrupt(T3,callback,Threshold);
//init_sensors();
if (deviceConnected){
float t = dht.readTemperature(true);
float h = dht.readHumidity(true);
float p = bmp.readPressure();
char tStr[10];
char hStr[10];
char pStr[10];
sprintf(tStr,"%4.4f",t);
sprintf(hStr,h);
sprintf(pStr,p);
temp_pCharacteristic->setValue(tStr);
temp_pCharacteristic->notify();
hum_pCharacteristic->setValue(hStr);
hum_pCharacteristic->notify();
pres_pCharacteristic->setValue(pStr);
pres_pCharacteristic->notify();
delay(2000); // bluetooth stack will go into congestion,in 6 hours
test i was able to go as low as 3ms
}
//delay(2000);
//Serial.println("Temperature: ");
//Serial.print(temp);
//Serial.println("Hummidity: ");
//Serial.print(hum);
//Serial.println("Atmospheric Pressure: ");
//Serial.print(pres);
delay(30000);
//Configure Touchpad as wakeup source
esp_sleep_enable_touchpad_wakeup();
//Go to sleep Now
Serial.println("Going to sleep Now");
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
void loop(){
//This will never be reached
}
解决方法
“值字段为空”
您的代码中没有任何名称类似于“值字段”。你指的是
float t = dht.readTemperature(true);
如果是这样,你确定这段代码被执行了吗?也许 deviceConnected
永远不会是真的?
你依赖于这个被执行
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
这对我来说看起来很狡猾。方法不应该是公开的吗?无论如何,添加一个打印语句以确保它确实在运行。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。