123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #include "WS_Bluetooth.h"
- BLEServer* pServer; // Used to represent a BLE server
- BLECharacteristic* pTxCharacteristic;
- BLECharacteristic* pRxCharacteristic;
- /********************************************************** Bluetooth *********************************************************/
- class MyServerCallbacks : public BLEServerCallbacks { //By overriding the onConnect() and onDisconnect() functions
- void onConnect(BLEServer* pServer) { // When the Device is connected, "Device connected" is printed.
- Serial.println("Device connected");
- }
- void onDisconnect(BLEServer* pServer) { // "Device disconnected" will be printed when the device is disconnected
- Serial.println("Device disconnected");
- BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); // Re-broadcast so that the device can query
- pAdvertising->addServiceUUID(SERVICE_UUID); // Re-broadcast so that the device can query
- pAdvertising->setScanResponse(true); // Re-broadcast so that the device can query
- pAdvertising->setMinPreferred(0x06); // Re-broadcast so that the device can query
- pAdvertising->setMinPreferred(0x12); // Re-broadcast so that the device can query
- BLEDevice::startAdvertising(); // Re-broadcast so that the device can query
- pRxCharacteristic->notify(); // Re-broadcast so that the device can query
- pAdvertising->start(); // Re-broadcast so that the device can query
- }
- };
- class MyRXCallback : public BLECharacteristicCallbacks {
- void onWrite(BLECharacteristic* pCharacteristic) { // The onWrite function is called when the remote device sends data to your feature
- std::string rxValue = pCharacteristic->getValue();
- if (!rxValue.empty()) {
- // The received data rxValue is processed here
- if(rxValue.length() == 1)
- {
- printf("%s\n", rxValue.c_str()); // Print output through the serial port
- uint8_t* valueBytes = reinterpret_cast<uint8_t*>(const_cast<char*>(rxValue.c_str())); // Convert value to uint8 t*
- Relay_Analysis(valueBytes,Bluetooth_Mode); // pilot relay
- }
- else if(rxValue.length() == 2)
- {
- if(Extension_Enable)
- {
- printf("%s\n", rxValue.c_str()); // Print output through the serial port
- uint8_t* valueBytes = reinterpret_cast<uint8_t*>(const_cast<char*>(rxValue.c_str())); // Convert value to uint8 t*
- if(valueBytes[0] == 0x06) // Instruction check correct
- RS485_Analysis(valueBytes); // Control external relay
- else
- printf("Note : Non-instruction data was received - Bluetooth !\r\n");
- }
- else
- printf("Note : Non-instruction data was received - Bluetooth !\r\n");
- }
- else
- {
- printf("Note : Non-instruction data was received - Bluetooth !\r\n");
- }
- pRxCharacteristic->setValue(""); // After data is read, set it to blank for next read
- }
- }
- };
- void Bluetooth_SendData(char* Data) { // Send data using Bluetooth
- if (Data != nullptr && strlen(Data) > 0) {
- if (pServer->getConnectedCount() > 0) {
- std::string SendValue = Data;
- pTxCharacteristic->setValue(SendValue); // Set SendValue to the eigenvalue
- pTxCharacteristic->notify(); // Sends a notification to all connected devices
- }
- }
- }
- void Bluetooth_Init()
- {
- /*************************************************************************
- Bluetooth
- *************************************************************************/
- BLEDevice::init("ESP32 S3 Relay 6CH"); // Initialize Bluetooth and start broadcasting
- pServer = BLEDevice::createServer();
- pServer->setCallbacks(new MyServerCallbacks());
- BLEService* pService = pServer->createService(SERVICE_UUID);
- pTxCharacteristic = pService->createCharacteristic(
- TX_CHARACTERISTIC_UUID,
- BLECharacteristic:: PROPERTY_READ); // The eigenvalues are readable and can be read by remote devices
- pRxCharacteristic = pService->createCharacteristic(
- RX_CHARACTERISTIC_UUID,
- BLECharacteristic::PROPERTY_WRITE); // The eigenvalues are writable and can be written to by remote devices
- pRxCharacteristic->setCallbacks(new MyRXCallback());
- pRxCharacteristic->setValue("Successfully Connect To ESP32-S3-Relay-6CH");
- pService->start();
- BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
- pAdvertising->addServiceUUID(SERVICE_UUID);
- pAdvertising->setScanResponse(true);
- pAdvertising->setMinPreferred(0x06);
- pAdvertising->setMinPreferred(0x12);
- BLEDevice::startAdvertising();
- pRxCharacteristic->notify();
- pAdvertising->start();
- RGB_Light(0, 0, 60);
- delay(1000);
- RGB_Light(0, 0, 0);
- printf("Now you can read it in your phone!\r\n");
- }
|