Advertisement
pleasedontcode

"BLE LED-Control" rev_01

May 28th, 2024
648
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 LED-Control"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-05-28 07:58:37
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement a BLE server. The BLE server must be */
  21.     /* able to accept or reject paring requests from */
  22.     /* other BLE client devices. Every 300 milliseconds, */
  23.     /* 50 bytes of binary data should be sent to other */
  24.     /* parched BLE client devices. The user is notified */
  25.     /* of the con */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <NimBLEDevice.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs();
  35.  
  36. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  37. const uint8_t LED_LED_PIN_D4 = 4;
  38.  
  39. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  40. /***** used to store raw data *****/
  41. bool LED_LED_PIN_D4_rawData = 0;
  42.  
  43. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  44. /***** used to store data after characteristic curve transformation *****/
  45. float LED_LED_PIN_D4_phyData = 0.0;
  46.  
  47. /***** BLE SERVER VARIABLES *****/
  48. NimBLEServer* pServer = nullptr;
  49. NimBLECharacteristic* pCharacteristic = nullptr;
  50. bool deviceConnected = false;
  51. uint8_t txValue[50] = {0};  // 50 bytes of binary data
  52.  
  53. class MyServerCallbacks: public NimBLEServerCallbacks {
  54.     void onConnect(NimBLEServer* pServer) {
  55.         deviceConnected = true;
  56.         Serial.println("Client connected");
  57.     };
  58.  
  59.     void onDisconnect(NimBLEServer* pServer) {
  60.         deviceConnected = false;
  61.         Serial.println("Client disconnected");
  62.     }
  63.  
  64.     uint32_t onPassKeyRequest() {
  65.         Serial.println("Server PassKeyRequest");
  66.         return 123456;  // Example passkey
  67.     }
  68.  
  69.     bool onConfirmPIN(uint32_t pass_key) {
  70.         Serial.print("The passkey YES/NO number: ");
  71.         Serial.println(pass_key);
  72.         return true;  // Confirm the passkey
  73.     }
  74.  
  75.     void onAuthenticationComplete(ble_gap_conn_desc* desc) {
  76.         if (!desc->sec_state.encrypted) {
  77.             NimBLEDevice::getServer()->disconnect(desc->conn_handle);
  78.             Serial.println("Encrypt connection failed - disconnecting client");
  79.             return;
  80.         }
  81.         Serial.println("Starting BLE work!");
  82.     }
  83. };
  84.  
  85. void setup(void) {
  86.     // put your setup code here, to run once:
  87.     Serial.begin(115200);
  88.     pinMode(LED_LED_PIN_D4, OUTPUT);
  89.  
  90.     // Initialize BLE
  91.     NimBLEDevice::init("ESP32_BLE_Server");
  92.     NimBLEDevice::setSecurityAuth(true, true, true);
  93.     NimBLEDevice::setSecurityPasskey(123456);
  94.     NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_ONLY);
  95.  
  96.     // Create BLE Server
  97.     pServer = NimBLEDevice::createServer();
  98.     pServer->setCallbacks(new MyServerCallbacks());
  99.  
  100.     // Create BLE Service
  101.     NimBLEService* pService = pServer->createService("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
  102.  
  103.     // Create BLE Characteristic
  104.     pCharacteristic = pService->createCharacteristic(
  105.         "beb5483e-36e1-4688-b7f5-ea07361b26a8",
  106.         NIMBLE_PROPERTY::NOTIFY
  107.     );
  108.  
  109.     // Start the service
  110.     pService->start();
  111.  
  112.     // Start advertising
  113.     NimBLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising();
  114.     pAdvertising->addServiceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
  115.     pAdvertising->setScanResponse(true);
  116.     pAdvertising->start();
  117.  
  118.     Serial.println("Waiting for a client connection to notify...");
  119. }
  120.  
  121. void loop(void) {
  122.     // put your main code here, to run repeatedly:
  123.     updateOutputs(); // Refresh output data
  124.  
  125.     if (deviceConnected) {
  126.         // Update the 50 bytes of binary data
  127.         for (int i = 0; i < 50; i++) {
  128.             txValue[i] = random(0, 256);  // Random binary data
  129.         }
  130.         pCharacteristic->setValue(txValue, 50);
  131.         pCharacteristic->notify();
  132.         delay(300);  // Send data every 300 milliseconds
  133.     }
  134. }
  135.  
  136. void updateOutputs() {
  137.     digitalWrite(LED_LED_PIN_D4, LED_LED_PIN_D4_rawData);
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement