Advertisement
pleasedontcode

**BLE Broadcast** rev_01

Mar 27th, 2025
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **BLE Broadcast**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-03-28 03:07:53
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement a Bluetooth Low Energy (BLE) */
  21.     /* communication system using NimBLEDevice to connect */
  22.     /* and control connected components remotely. Ensure */
  23.     /* seamless data transfer and device management for */
  24.     /* enhanced user experience. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <NimBLEDevice.h>   //https://github.com/h2zero/NimBLE-Arduino
  31. #include <DHT.h>    //https://github.com/adafruit/DHT-sensor-library
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t myDHT22_DHT22_DOUT_PIN_D4     = 4;
  39.  
  40. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  41. // Create a DHT object
  42. DHT dht(myDHT22_DHT22_DOUT_PIN_D4, DHT22); // Assuming DHT22 sensor
  43.  
  44. // BLE server instance
  45. NimBLEServer* pServer = nullptr;
  46. NimBLECharacteristic* pCharacteristic = nullptr;
  47.  
  48. // Variable to hold the sensor data
  49. float temperature;
  50. float humidity;
  51.  
  52. // Callback for BLE connection
  53. class MyServerCallbacks : public NimBLEServerCallbacks {
  54.     void onConnect(NimBLEServer* pServer) {
  55.         // Code to execute when a client connects
  56.     }
  57.  
  58.     void onDisconnect(NimBLEServer* pServer) {
  59.         // Code to execute when a client disconnects
  60.     }
  61. };
  62.  
  63. void setup(void)
  64. {
  65.     // Initialize Serial for debugging
  66.     Serial.begin(115200);
  67.    
  68.     // Initialize DHT sensor
  69.     dht.begin();
  70.  
  71.     // Initialize BLE
  72.     NimBLEDevice::init("ESP32_BLE_Device"); // Set the BLE device name
  73.     pServer = NimBLEDevice::createServer(); // Create the BLE Server
  74.     pServer->setCallbacks(new MyServerCallbacks()); // Set server callbacks
  75.  
  76.     // Create a BLE Characteristic
  77.     pCharacteristic = pServer->createCharacteristic(
  78.         "Temperature_Humidity",
  79.         NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY
  80.     );
  81.  
  82.     // Start the server
  83.     pServer->startAdvertising(); // Start advertising
  84.     Serial.println("BLE Device is advertising...");
  85. }
  86.  
  87. void loop(void)
  88. {
  89.     // Read temperature and humidity
  90.     humidity = dht.readHumidity();
  91.     temperature = dht.readTemperature();
  92.  
  93.     // Check if any reads failed and exit early (to try again).
  94.     if (isnan(humidity) || isnan(temperature)) {
  95.         Serial.println("Failed to read from DHT sensor!");
  96.         return;
  97.     }
  98.  
  99.     // Create a string to hold the sensor data
  100.     String data = "Temp: " + String(temperature) + " C, Humidity: " + String(humidity) + " %";
  101.    
  102.     // Update the characteristic value
  103.     pCharacteristic->setValue(data.c_str());
  104.     pCharacteristic->notify(); // Notify clients of the new value
  105.  
  106.     // Print the values to the Serial Monitor
  107.     Serial.println(data);
  108.  
  109.     // Wait a bit before the next loop
  110.     delay(2000); // Adjust the delay as needed
  111. }
  112.  
  113. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement