Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **BLE Broadcast**
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-03-28 03:07:53
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Implement a Bluetooth Low Energy (BLE) */
- /* communication system using NimBLEDevice to connect */
- /* and control connected components remotely. Ensure */
- /* seamless data transfer and device management for */
- /* enhanced user experience. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <NimBLEDevice.h> //https://github.com/h2zero/NimBLE-Arduino
- #include <DHT.h> //https://github.com/adafruit/DHT-sensor-library
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t myDHT22_DHT22_DOUT_PIN_D4 = 4;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Create a DHT object
- DHT dht(myDHT22_DHT22_DOUT_PIN_D4, DHT22); // Assuming DHT22 sensor
- // BLE server instance
- NimBLEServer* pServer = nullptr;
- NimBLECharacteristic* pCharacteristic = nullptr;
- // Variable to hold the sensor data
- float temperature;
- float humidity;
- // Callback for BLE connection
- class MyServerCallbacks : public NimBLEServerCallbacks {
- void onConnect(NimBLEServer* pServer) {
- // Code to execute when a client connects
- }
- void onDisconnect(NimBLEServer* pServer) {
- // Code to execute when a client disconnects
- }
- };
- void setup(void)
- {
- // Initialize Serial for debugging
- Serial.begin(115200);
- // Initialize DHT sensor
- dht.begin();
- // Initialize BLE
- NimBLEDevice::init("ESP32_BLE_Device"); // Set the BLE device name
- pServer = NimBLEDevice::createServer(); // Create the BLE Server
- pServer->setCallbacks(new MyServerCallbacks()); // Set server callbacks
- // Create a BLE Characteristic
- pCharacteristic = pServer->createCharacteristic(
- "Temperature_Humidity",
- NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY
- );
- // Start the server
- pServer->startAdvertising(); // Start advertising
- Serial.println("BLE Device is advertising...");
- }
- void loop(void)
- {
- // Read temperature and humidity
- humidity = dht.readHumidity();
- temperature = dht.readTemperature();
- // Check if any reads failed and exit early (to try again).
- if (isnan(humidity) || isnan(temperature)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- // Create a string to hold the sensor data
- String data = "Temp: " + String(temperature) + " C, Humidity: " + String(humidity) + " %";
- // Update the characteristic value
- pCharacteristic->setValue(data.c_str());
- pCharacteristic->notify(); // Notify clients of the new value
- // Print the values to the Serial Monitor
- Serial.println(data);
- // Wait a bit before the next loop
- delay(2000); // Adjust the delay as needed
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement