C++ ESP32触摸传感器,用于深度睡眠

C++ ESP32触摸传感器,用于深度睡眠,c++,bluetooth-lowenergy,esp32,arduino-ide,C++,Bluetooth Lowenergy,Esp32,Arduino Ide,我试图唤醒我的esp32,在nRF Connect中通过蓝牙接收数据,但我没有温度、嗡嗡声和压力值。我不知道为什么我什么也看不到,因为我的传感器(DHT22和BMP280)连接正确,它们在串行监视器中传输良好的值,但在应用程序值字段中为空。我尝试在回调函数或设置中输入特征,但同样的问题是,空值。有什么问题?有人能给我提建议吗? 我在下面附上我的代码。提前谢谢 #include <BLEDevice.h> #include <BLEServer.h> #include &l

我试图唤醒我的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, "%4.4f", h);
    sprintf(pStr, "%4.4f", 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, if too many packets are sent, 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
   }
#包括
#包括
#包括
//#包括
#包括“DHT.h”
//#包括
#包括
#包括
#包括
#包括
BLEServer*pServer=NULL;
BLECharacteristic*temp_pCharacteristic=NULL;
BLECharacteristic*hum_pCharacteristic=NULL;
BLECharacteristic*pres_pCharacteristic=NULL;
布尔设备连接=错误;
bool oldDeviceConnected=错误;
#定义连接到DHT传感器的DHTPIN 4//数字引脚
#定义DHT类型DHT22//DHT 22(AM2302)、AM2321
DHT-DHT(DHTPIN,DHTTYPE);
Adafruit_BMP280 bmp;//实例化(创建)BMP280对象并设置I2C操作
#定义阈值40/*值越大,灵敏度越高*/
RTC_DATA_ATTR int bootCount=0;
触摸板触摸针;
类MyServerCallbacks:public BLEServerCallbacks{
void onConnect(BLEServer*pServer){
deviceConnected=true;
};
void onDisconnect(BLEServer*pServer){
设备连接=错误;
}
};
void init_传感器(){
dht.begin();
布尔状态;
状态=bmp.begin(0x76);
如果(!状态){
Serial.println(“找不到有效的BMP280传感器,请检查接线!”);
而(1),;
}
}
/*
方法打印使用ESP32的原因
已经从睡梦中醒来
*/
无效打印\唤醒\原因(){
尤其是睡眠和醒来的原因;
wakeup_reason=esp_sleep_get_wakeup_cause();
开关(唤醒原因)
{
案例ESP_SLEEP_WAKEUP_EXT0:Serial.println(“使用RTC_IO的外部信号引起的唤醒”);
打破
案例ESP_SLEEP_WAKEUP_EXT1:Serial.println(“使用RTC_CNTL的外部信号引起的唤醒”);
打破
案例ESP_SLEEP_WAKEUP_TIMER:Serial.println(“由定时器引起的唤醒”);中断;
案例ESP_SLEEP_WAKEUP_TOUCHPAD:Serial.println(“由TOUCHPAD引起的唤醒”);中断;
案例ESP_SLEEP_WAKEUP_ULP:Serial.println(“由ULP程序引起的唤醒”);中断;
默认值:Serial.printf(“唤醒不是由深度睡眠引起的:%d\n”,唤醒原因);中断;
}
}
/*
方法打印使用ESP32的触摸板
已经从睡梦中醒来
*/
无效打印\唤醒\触摸板(){
touchPin=esp_sleep_get_touchpad_wakeup_status();
开关(触针)
{
案例0:Serial.println(“在GPIO 4上检测到触摸”);中断;
案例1:Serial.println(“在GPIO 0上检测到触摸”);中断;
案例2:Serial.println(“在GPIO 2上检测到触摸”);中断;
案例3:Serial.println(“在GPIO 15上检测到触摸”);中断;
案例4:Serial.println(“在GPIO 13上检测到触摸”);中断;
案例5:Serial.println(“在GPIO 12上检测到触摸”);中断;
案例6:Serial.println(“在GPIO 14上检测到触摸”);中断;
案例7:Serial.println(“在GPIO 27上检测到触摸”);中断;
案例8:Serial.println(“在GPIO 33上检测到触摸”);中断;
案例9:Serial.println(“在GPIO 32上检测到触摸”);中断;
默认值:Serial.println(“非触摸板唤醒”);中断;
}
}
void callback(){
//占位符回调函数
//通知更改的值
/*初始化传感器();
如果(设备连接){
uint32_t t=dht.readTemperature();
uint32_t h=dht.read湿度();
uint32_t p=bmp.readPressure();
温度特性->设定值(t);
temp_pCharacteristic->notify();
hum_p特征->设定值(h);
hum_pCharacteristic->notify();
压力特性->设定值(p);
pres_pCharacteristic->notify();
延迟(2000);//如果发送的数据包太多,蓝牙协议栈将陷入拥塞状态,时间为6
小时测试我可以低到3毫秒
}  */
}
无效设置(){
序列号开始(115200);
//花些时间打开串行监视器
init_sensors();//apelarea functiei pentru配置传感器
延迟(2000年);
//浮动温度=dht.readTemperature();
//float hum=dht.read湿度();
//float pres=bmp.readPressure();
//创建可扩展设备
BLEDevice::init(“T”);
//创建可扩展服务器
pServer=BLEDevice::createServer();
pServer->setCallbacks(新的MyServerCallbacks());
//创建可扩展的服务
BLEService*pService=pServer->createService(BLEUUID((uint16_t)0x181A));
//创造出一个可改变的特征
temp\u pCharacteristic=pService->createCharacteristic(
BLEUUID((uint16_t)0x2A6E),
BLECharacteristic::属性\读取|
BLECharacteristic::属性\写入|
BLECharacteristic::属性通知|
BLECharacteristic::属性_表示
);
hum\u pCharacteristic=pService->createCharacteristic(
BLEUUID((uint16_t)0x2A6F),
BLECharacteristic::属性\读取|
BLECharacteristic::属性\写入|
BLECharacteristic::属性通知|
BLECharacteristic::属性_表示
);
pres_pCharacteristic=pService->createCharacteristic(
BLEUUID((uint16_t)0x2A6D),
BLECharacteristic::属性\读取|
BLECharacteristic::属性\写入|
BLECharacteristic::属性通知|
BLECharacteristic::属性_表示
);
//创建一个可扩展的描述符
//hum_pCharacteristic->addDescriptor(新BLE2902());
//启动服务
pService->start();
float t = dht.readTemperature(true);
 class MyServerCallbacks: public BLEServerCallbacks {
     void onConnect(BLEServer* pServer) {
        deviceConnected = true;
  };