pleasedontcode

BLE CarLock rev_01

Dec 26th, 2025
30
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 CarLock
  13.     - Source Code NOT compiled for: XIAO ESP32S3
  14.     - Source Code created on: 2025-12-26 20:54:16
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Enable BLE advertising on the XIAO ESP32S3 to */
  21.     /* broadcast device presence and identify it to */
  22.     /* nearby BLE devices. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <BLEDevice.h>  //https://github.com/nkolban/ESP32_BLE_Arduino
  30. #include <BLEUtils.h>  //https://github.com/nkolban/ESP32_BLE_Arduino
  31. #include <BLEScan.h>   //https://github.com/nkolban/ESP32_BLE_Arduino
  32. #include <BLEAdvertisedDevice.h>  //https://github.com/nkolban/ESP32_BLE_Arduino
  33. #include <ArduinoBLE.h>  //https://github.com/arduino-libraries/ArduinoBLE
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40.  
  41. // Define UUID for BLE service and characteristics
  42. #define SERVICE_UUID        "0000180a-0000-1000-8000-00805f9b34fb"
  43. #define CHARACTERISTIC_UUID "00002a00-0000-1000-8000-00805f9b34fb"
  44.  
  45. // Define pin for controlling car door lock
  46. #define DOOR_LOCK_PIN       2
  47.  
  48. // Create BLE server and characteristics
  49. BLEServer* pServer;
  50. BLECharacteristic* pCharacteristic;
  51.  
  52. // Create variable to store lock status
  53. bool isLocked = false;
  54.  
  55. // Function to handle BLE write events
  56. class MyServerCallbacks: public BLEServerCallbacks {
  57.     void onWrite(BLECharacteristic *pCharacteristic) {
  58.         // Get data from BLE write event
  59.         std::string value = pCharacteristic->getValue();
  60.  
  61.         // Check if data is "lock" or "unlock"
  62.         if (value == "lock") {
  63.             // Lock the car door
  64.             digitalWrite(DOOR_LOCK_PIN, HIGH);
  65.             isLocked = true;
  66.         } else if (value == "unlock") {
  67.             // Unlock the car door
  68.             digitalWrite(DOOR_LOCK_PIN, LOW);
  69.             isLocked = false;
  70.         }
  71.     }
  72. };
  73.  
  74. void setup() {
  75.     // Initialize serial communication
  76.     Serial.begin(9600);
  77.  
  78.     // Set pin modes
  79.     pinMode(DOOR_LOCK_PIN, OUTPUT);
  80.  
  81.     // Initialize BLE
  82.     BLEDevice::init("Car Door Lock");
  83.     pServer = BLEDevice::createServer();
  84.     pServer->setCallbacks(new MyServerCallbacks());
  85.  
  86.     // Create BLE service
  87.     BLEService *pService = pServer->createService(SERVICE_UUID);
  88.  
  89.     // Create BLE characteristic
  90.     pCharacteristic = pService->createCharacteristic(
  91.                         CHARACTERISTIC_UUID,
  92.                         BLECharacteristic::PROPERTY_WRITE
  93.                       );
  94.  
  95.     // Start the service
  96.     pService->start();
  97.  
  98.     // Start advertising
  99.     BLEAdvertising *pAdvertising = pServer->getAdvertising();
  100.     pAdvertising->start();
  101. }
  102.  
  103. void loop() {
  104.     // Check if car door is locked
  105.     if (isLocked) {
  106.         Serial.println("Car door is locked.");
  107.     } else {
  108.         Serial.println("Car door is unlocked.");
  109.     }
  110.  
  111.     // Delay for 1 second
  112.     delay(1000);
  113. }
  114.  
  115. // Reference: https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/
  116.  
  117. /* END CODE */
  118.  
Advertisement
Add Comment
Please, Sign In to add comment