Advertisement
pleasedontcode

**BLE Server** rev_02

Mar 27th, 2025
745
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 Server**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-03-28 03:13:13
  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. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* Write a Code for esp32-C3 in arduino IDE that */
  27.     /* always is on deep sleep. Increment a value every 3 */
  28.     /* seconds and transmit the value by bluetooth using */
  29.     /* Nimble library and the same data will transmitted */
  30.     /* by espnow every 9 seconds. */
  31. /****** END SYSTEM REQUIREMENTS *****/
  32.  
  33. /* START CODE */
  34.  
  35. /****** DEFINITION OF LIBRARIES *****/
  36. #include <NimBLEDevice.h> // https://github.com/h2zero/NimBLE-Arduino
  37. #include <DHT.h>          // https://github.com/adafruit/DHT-sensor-library
  38.  
  39. /****** FUNCTION PROTOTYPES *****/
  40. void setup(void);
  41. void loop(void);
  42.  
  43. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  44. const uint8_t myDHT22_DHT22_DOUT_PIN_D4 = 4;
  45. const uint8_t myDHT11_DHT11_DOUT_PIN_D13 = 13;
  46.  
  47. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  48. // BLE variables
  49. NimBLEServer* pServer = nullptr;
  50. NimBLECharacteristic* pCharacteristic = nullptr;
  51. uint32_t valueToSend = 0; // Value to be incremented and sent
  52.  
  53. /****** SETUP FUNCTION *****/
  54. void setup(void)
  55. {
  56.     // Initialize Serial for debugging
  57.     Serial.begin(115200);
  58.  
  59.     // Initialize BLE
  60.     NimBLEDevice::init("ESP32-C3");
  61.  
  62.     // Create BLE Server
  63.     pServer = NimBLEDevice::createServer();
  64.     pCharacteristic = pServer->createCharacteristic(
  65.         "12345678-1234-5678-1234-56789abcdef0", // UUID
  66.         NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::READ
  67.     );
  68.  
  69.     // Set pin modes
  70.     pinMode(myDHT22_DHT22_DOUT_PIN_D4, INPUT_PULLUP);
  71.     pinMode(myDHT11_DHT11_DOUT_PIN_D13, INPUT_PULLUP);
  72.  
  73.     // Start advertising
  74.     pServer->getAdvertising()->start();
  75.     Serial.println("BLE Server is advertising");
  76. }
  77.  
  78. /****** LOOP FUNCTION *****/
  79. void loop(void)
  80. {
  81.     // Increment the value every 3 seconds
  82.     delay(3000);
  83.     valueToSend++;
  84.     Serial.print("Value to send: ");
  85.     Serial.println(valueToSend);
  86.  
  87.     // Update the characteristic with the new value
  88.     pCharacteristic->setValue(valueToSend);
  89.     pCharacteristic->notify();
  90.  
  91.     // Deep sleep for 9 seconds
  92.     Serial.println("Going to deep sleep for 9 seconds");
  93.     esp_sleep_enable_timer_wakeup(9000000); // 9 seconds in microseconds
  94.     esp_deep_sleep_start();
  95. }
  96.  
  97. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement