TolentinoCotesta

BLE Environment

Nov 10th, 2021 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 KB | None | 0 0
  1. /*
  2.    The design of creating the BLE server is:
  3.    1. Create a BLE Server
  4.    2. Create a BLE Service
  5.    3. Create a BLE Characteristic on the Service
  6.    4. Create a BLE Descriptor on the characteristic
  7.    5. Start the service.
  8.    6. Start advertising.
  9.  
  10.    MIT App Inventor project
  11.  
  12. */
  13.  
  14. #include <BLEDevice.h>
  15. #include <BLEServer.h>
  16. #include <BLEUtils.h>
  17. #include <BLE2902.h>
  18.  
  19.  
  20. // Definite dal protocollo GATT/BLE
  21. const uint16_t temperatureUUID  = 0x2A6E;
  22. const uint16_t humidityUUID     = 0x2A6F;
  23. const uint16_t environmentUUID  = 0x181A;
  24. const uint16_t descriptionUUID   = 0x2901;
  25.  
  26.  
  27. // Variabili che saranno esposte dal servizio BLE
  28. float temperature = -15.0F;
  29. float humidity = 45.8F;
  30.  
  31. // Definizione del server BLE
  32. BLEServer* pServer = NULL;
  33.  
  34. // Definizione delle caratteristiche per umidità e temepratura
  35. BLECharacteristic temperatureCharact(
  36.            BLEUUID(temperatureUUID),
  37.            BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
  38. BLECharacteristic humidityCharact(
  39.            BLEUUID(humidityUUID),
  40.            BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
  41.  
  42. bool deviceConnected = false;
  43. bool oldDeviceConnected = false;
  44.  
  45.  
  46. // Funzioni di callback che sarannoe eseguite quando un client si connette/disconnette
  47. class MyServerCallbacks: public BLEServerCallbacks {
  48.     void onConnect(BLEServer* pServer) {
  49.       deviceConnected = true;
  50.     };
  51.  
  52.     void onDisconnect(BLEServer* pServer) {
  53.       deviceConnected = false;
  54.     }
  55. };
  56.  
  57.  
  58.  
  59. void setup() {
  60.   Serial.begin(115200);
  61.  
  62.   // Create the BLE Device
  63.   BLEDevice::init("ESP32");
  64.  
  65.   // Create the BLE Server
  66.   pServer = BLEDevice::createServer();
  67.   pServer->setCallbacks(new MyServerCallbacks());
  68.  
  69.   // Create the BLE Service
  70.   BLEService *pService = pServer->createService(BLEUUID(environmentUUID));
  71.  
  72.   // Aggiungi la descrizione alla caratteristica
  73.   temperatureCharact.addDescriptor(new BLE2902());
  74.   humidityCharact.addDescriptor(new BLE2902());
  75.  
  76.   // Aggiungi le caratteristiche su configurate al servizio ed avvia
  77.   pService->addCharacteristic(&temperatureCharact);
  78.   pService->addCharacteristic(&humidityCharact);
  79.   pService->start();
  80.  
  81.   // Start advertising (facciamoci vedere dal resto del mondo)
  82.   BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  83.   pAdvertising->addServiceUUID(BLEUUID(environmentUUID));
  84.   pAdvertising->setScanResponse(false);
  85.   pAdvertising->setMinPreferred(0x0);  // set value to 0x00 to not advertise this parameter
  86.   BLEDevice::startAdvertising();
  87.  
  88.   Serial.printf("\n\nBLE service UUID: %s\n", BLEUUID(environmentUUID).toString().c_str());
  89.   Serial.printf("Temperature characteristic UUID: %s\n", BLEUUID(temperatureUUID).toString().c_str());
  90.   Serial.printf("Humidity characteristic UUID: %s\n", BLEUUID(humidityUUID).toString().c_str());
  91.  
  92.   Serial.println("\nWaiting a client connection to notify...");
  93. }
  94.  
  95. void loop() {
  96.  
  97.   // notify changed value
  98.   if (deviceConnected) {
  99.    
  100.     // Aggiona valori di temperatura e umidità se il client è connesso (es. smartphone)
  101.     // temperature = dht.getTemperature();
  102.     // humidity = dht.getHumidity();
  103.  
  104.     /* Temperature characteristic
  105.       Unit is in degrees Celsius with a resolution of 0.01 degrees Celsius
  106.       Exponent: Decimal, -2
  107.     */
  108.     uint16_t charValue;
  109.     charValue = (int16_t)(temperature *100); // multiply by 100 to get 2 digits mantissa and convert into uint16_t
  110.     temperatureCharact.setValue(charValue);  // set characteristic value
  111.     temperatureCharact.notify();
  112.  
  113.     // Stessa cosa per umidità
  114.     charValue = (int16_t)(humidity *100); // multiply by 100 to get 2 digits mantissa and convert into uint16_t
  115.     humidityCharact.setValue(charValue);  // set characteristic value
  116.     humidityCharact.notify();
  117.  
  118.     delay(5); // 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
  119.   }
  120.   // disconnecting
  121.   if (!deviceConnected && oldDeviceConnected) {
  122.     delay(500); // give the bluetooth stack the chance to get things ready
  123.     pServer->startAdvertising(); // restart advertising
  124.     Serial.println("start advertising");
  125.     oldDeviceConnected = deviceConnected;
  126.   }
  127.   // connecting
  128.   if (deviceConnected && !oldDeviceConnected) {
  129.     // do stuff here on connecting
  130.     oldDeviceConnected = deviceConnected;
  131.   }
  132. }
Add Comment
Please, Sign In to add comment