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 CarLock
- - Source Code NOT compiled for: XIAO ESP32S3
- - Source Code created on: 2025-12-26 20:54:16
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Enable BLE advertising on the XIAO ESP32S3 to */
- /* broadcast device presence and identify it to */
- /* nearby BLE devices. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <BLEDevice.h> //https://github.com/nkolban/ESP32_BLE_Arduino
- #include <BLEUtils.h> //https://github.com/nkolban/ESP32_BLE_Arduino
- #include <BLEScan.h> //https://github.com/nkolban/ESP32_BLE_Arduino
- #include <BLEAdvertisedDevice.h> //https://github.com/nkolban/ESP32_BLE_Arduino
- #include <ArduinoBLE.h> //https://github.com/arduino-libraries/ArduinoBLE
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Define UUID for BLE service and characteristics
- #define SERVICE_UUID "0000180a-0000-1000-8000-00805f9b34fb"
- #define CHARACTERISTIC_UUID "00002a00-0000-1000-8000-00805f9b34fb"
- // Define pin for controlling car door lock
- #define DOOR_LOCK_PIN 2
- // Create BLE server and characteristics
- BLEServer* pServer;
- BLECharacteristic* pCharacteristic;
- // Create variable to store lock status
- bool isLocked = false;
- // Function to handle BLE write events
- class MyServerCallbacks: public BLEServerCallbacks {
- void onWrite(BLECharacteristic *pCharacteristic) {
- // Get data from BLE write event
- std::string value = pCharacteristic->getValue();
- // Check if data is "lock" or "unlock"
- if (value == "lock") {
- // Lock the car door
- digitalWrite(DOOR_LOCK_PIN, HIGH);
- isLocked = true;
- } else if (value == "unlock") {
- // Unlock the car door
- digitalWrite(DOOR_LOCK_PIN, LOW);
- isLocked = false;
- }
- }
- };
- void setup() {
- // Initialize serial communication
- Serial.begin(9600);
- // Set pin modes
- pinMode(DOOR_LOCK_PIN, OUTPUT);
- // Initialize BLE
- BLEDevice::init("Car Door Lock");
- pServer = BLEDevice::createServer();
- pServer->setCallbacks(new MyServerCallbacks());
- // Create BLE service
- BLEService *pService = pServer->createService(SERVICE_UUID);
- // Create BLE characteristic
- pCharacteristic = pService->createCharacteristic(
- CHARACTERISTIC_UUID,
- BLECharacteristic::PROPERTY_WRITE
- );
- // Start the service
- pService->start();
- // Start advertising
- BLEAdvertising *pAdvertising = pServer->getAdvertising();
- pAdvertising->start();
- }
- void loop() {
- // Check if car door is locked
- if (isLocked) {
- Serial.println("Car door is locked.");
- } else {
- Serial.println("Car door is unlocked.");
- }
- // Delay for 1 second
- delay(1000);
- }
- // Reference: https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment