BLE Temperature e Humidty with ESP32

zarak225
Posts: 2
Joined: Fri Mar 19, 2021 3:41 pm

BLE Temperature e Humidty with ESP32

Postby zarak225 » Wed May 18, 2022 6:10 pm

This is a code to use with Jinou Beacon sensor. Its working but has a lot to do, i need some help to work with multiple beacons. I think its needed to use this:
  1. #include "BLEDevice.h"
  2.  
  3. // The remote service we wish to connect to.
  4. static BLEUUID serviceUUID("0000AA20");
  5. // The characteristic of the remote service we are interested in.
  6. static BLEUUID    charUUID("0000AA21");
  7.  
  8. static boolean doConnect = false;
  9. static boolean connected = false;
  10. static boolean doScan = false;
  11. static BLERemoteCharacteristic* pRemoteCharacteristic;
  12. static BLEAdvertisedDevice* myDevice;
  13.  
  14. static void notifyCallback(
  15.   BLERemoteCharacteristic* pBLERemoteCharacteristic,
  16.   uint8_t* pData,
  17.   size_t length,
  18.   bool isNotify) {  
  19. }
  20.  
  21. class MyClientCallback : public BLEClientCallbacks {
  22.   void onConnect(BLEClient* pclient) {
  23.   }
  24.  
  25.   void onDisconnect(BLEClient* pclient) {
  26.     connected = false;
  27.     Serial.println("onDisconnect");
  28.   }
  29. };
  30.  
  31. bool connectToServer() {
  32.     Serial.print("Forming a connection to ");
  33.     Serial.println(myDevice->getAddress().toString().c_str());
  34.    
  35.     BLEClient*  pClient  = BLEDevice::createClient();
  36.     Serial.println(" - Created client");
  37.  
  38.     pClient->setClientCallbacks(new MyClientCallback());
  39.  
  40.     // Connect to the remote BLE Server.
  41.     pClient->connect(myDevice);  // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
  42.     Serial.println(" - Connected to server");
  43.  
  44.     // Obtain a reference to the service we are after in the remote BLE server.
  45.     BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
  46.     if (pRemoteService == nullptr) {
  47.       Serial.print("Failed to find our service UUID: ");
  48.       Serial.println(serviceUUID.toString().c_str());
  49.       pClient->disconnect();
  50.       return false;
  51.     }
  52.     Serial.println(" - Found our service");
  53.  
  54.  
  55.     // Obtain a reference to the characteristic in the service of the remote BLE server.
  56.     pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
  57.     if (pRemoteCharacteristic == nullptr) {
  58.       Serial.print("Failed to find our characteristic UUID: ");
  59.       Serial.println(charUUID.toString().c_str());
  60.       pClient->disconnect();
  61.       return false;
  62.     }
  63.     Serial.println(" - Found our characteristic");
  64.  
  65.     // Read the value of the characteristic.
  66.     if(pRemoteCharacteristic->canRead()) {
  67.       std::string value = pRemoteCharacteristic->readValue();
  68.       Serial.print("The characteristic value was: ");
  69.       Serial.println(value.c_str());
  70.     }
  71.  
  72.     if(pRemoteCharacteristic->canNotify())
  73.       pRemoteCharacteristic->registerForNotify(notifyCallback);
  74.  
  75.     connected = true;
  76. }
  77. /**
  78.  * Scan for BLE servers and find the first one that advertises the service we are looking for.
  79.  */
  80. class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
  81.  /**
  82.    * Called for each advertising BLE server.
  83.    */
  84.   void onResult(BLEAdvertisedDevice advertisedDevice) {
  85.     Serial.print("BLE Advertised Device found: ");
  86.     Serial.println(advertisedDevice.toString().c_str());
  87.  
  88.     // We have found a device, let us now see if it contains the service we are looking for.
  89.     if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {
  90.  
  91.       BLEDevice::getScan()->stop();
  92.       myDevice = new BLEAdvertisedDevice(advertisedDevice);
  93.       doConnect = true;
  94.       doScan = true;
  95.  
  96.     } // Found our server
  97.   } // onResult
  98. }; // MyAdvertisedDeviceCallbacks
  99.  
  100.  
  101. void setup() {
  102.   Serial.begin(115200);
  103.   Serial.println("Starting Arduino BLE Client application...");
  104.   BLEDevice::init("");
  105.  
  106.   // Retrieve a Scanner and set the callback we want to use to be informed when we
  107.   // have detected a new device.  Specify that we want active scanning and start the
  108.   // scan to run for 5 seconds.
  109.   BLEScan* pBLEScan = BLEDevice::getScan();
  110.   pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  111.   pBLEScan->setInterval(1349);
  112.   pBLEScan->setWindow(449);
  113.   pBLEScan->setActiveScan(true);
  114.   pBLEScan->start(5, false);
  115. } // End of setup.
  116.  
  117.  
  118. // This is the Arduino main loop function.
  119. void loop() {
  120.  
  121.   // If the flag "doConnect" is true then we have scanned for and found the desired
  122.   // BLE Server with which we wish to connect.  Now we connect to it.  Once we are
  123.   // connected we set the connected flag to be true.
  124.   if (doConnect == true) {
  125.     if (connectToServer()) {
  126.       Serial.println("We are now connected to the BLE Server.");
  127.     } else {
  128.       Serial.println("We have failed to connect to the server; there is nothin more we will do.");
  129.     }
  130.     doConnect = false;
  131.   }
  132.  
  133.   // If we are connected to a peer BLE Server, update the characteristic each time we are reached
  134.   // with the current time since boot.
  135.   if (connected) {
  136.     String newValue = "Canopus: " + String(millis()/1000);
  137.     pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
  138.     Serial.print("\r\n - Result sensor");
  139.     std::string value = pRemoteCharacteristic->readValue();
  140.     Serial.printf("\r\nLength : %d\r\n", value.length());
  141.     Serial.print(" 0x"); Serial.print((uint8_t)value[0], HEX);
  142.     Serial.print(" 0x"); Serial.print((uint8_t)value[1], HEX);
  143.     Serial.print(" 0x"); Serial.print((uint8_t)value[2], HEX);
  144.     Serial.print(" 0x"); Serial.print((uint8_t)value[3], HEX);
  145.     Serial.print(" 0x"); Serial.print((uint8_t)value[4], HEX);
  146.     Serial.print(" 0x"); Serial.print((uint8_t)value[5], HEX);
  147.  
  148.     float temp = value[1]*100+value[2];
  149.     if(value[1]==1)
  150.     {
  151.       temp=temp*-1;
  152.     }
  153.     Serial.printf("\r\nTemp: %.02f*C", temp/100);
  154.     float humi = value[4]*100+value[5];
  155.     Serial.printf("\r\nHumi: %.02f/%", humi/100);
  156.                  
  157.   }else if(doScan){
  158.     BLEDevice::getScan()->start(0);  // this is just example to start scan after disconnect, most likely there is better way to do it in arduino
  159.   }
  160.  
  161.   delay(3000); // Delay a second between loops.
  162. } // End of loop

Who is online

Users browsing this forum: No registered users and 31 guests