/********* 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 LED-Control"
- Source Code NOT compiled for: ESP32 DevKit V1
- Source Code created on: 2024-05-28 07:58:37
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* Implement a BLE server. The BLE server must be */
/* able to accept or reject paring requests from */
/* other BLE client devices. Every 300 milliseconds, */
/* 50 bytes of binary data should be sent to other */
/* parched BLE client devices. The user is notified */
/* of the con */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs();
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t LED_LED_PIN_D4 = 4;
/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool LED_LED_PIN_D4_rawData = 0;
/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float LED_LED_PIN_D4_phyData = 0.0;
/***** BLE SERVER VARIABLES *****/
NimBLEServer* pServer = nullptr;
NimBLECharacteristic* pCharacteristic = nullptr;
bool deviceConnected = false;
uint8_t txValue[50] = {0}; // 50 bytes of binary data
class MyServerCallbacks: public NimBLEServerCallbacks {
void onConnect(NimBLEServer* pServer) {
deviceConnected = true;
Serial.println("Client connected");
};
void onDisconnect(NimBLEServer* pServer) {
deviceConnected = false;
Serial.println("Client disconnected");
}
uint32_t onPassKeyRequest() {
Serial.println("Server PassKeyRequest");
return 123456; // Example passkey
}
bool onConfirmPIN(uint32_t pass_key) {
Serial.print("The passkey YES/NO number: ");
Serial.println(pass_key);
return true; // Confirm the passkey
}
void onAuthenticationComplete(ble_gap_conn_desc* desc) {
if (!desc->sec_state.encrypted) {
NimBLEDevice::getServer()->disconnect(desc->conn_handle);
Serial.println("Encrypt connection failed - disconnecting client");
return;
}
Serial.println("Starting BLE work!");
}
};
void setup(void) {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LED_LED_PIN_D4, OUTPUT);
// Initialize BLE
NimBLEDevice::init("ESP32_BLE_Server");
NimBLEDevice::setSecurityAuth(true, true, true);
NimBLEDevice::setSecurityPasskey(123456);
NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_ONLY);
// Create BLE Server
pServer = NimBLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create BLE Service
NimBLEService* pService = pServer->createService("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
// Create BLE Characteristic
pCharacteristic = pService->createCharacteristic(
"beb5483e-36e1-4688-b7f5-ea07361b26a8",
NIMBLE_PROPERTY::NOTIFY
);
// Start the service
pService->start();
// Start advertising
NimBLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising();
pAdvertising->addServiceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
pAdvertising->setScanResponse(true);
pAdvertising->start();
Serial.println("Waiting for a client connection to notify...");
}
void loop(void) {
// put your main code here, to run repeatedly:
updateOutputs(); // Refresh output data
if (deviceConnected) {
// Update the 50 bytes of binary data
for (int i = 0; i < 50; i++) {
txValue[i] = random(0, 256); // Random binary data
}
pCharacteristic->setValue(txValue, 50);
pCharacteristic->notify();
delay(300); // Send data every 300 milliseconds
}
}
void updateOutputs() {
digitalWrite(LED_LED_PIN_D4, LED_LED_PIN_D4_rawData);
}