WS_Bluetooth.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "WS_Bluetooth.h"
  2. BLEServer* pServer; // Used to represent a BLE server
  3. BLECharacteristic* pTxCharacteristic;
  4. BLECharacteristic* pRxCharacteristic;
  5. /********************************************************** Bluetooth *********************************************************/
  6. class MyServerCallbacks : public BLEServerCallbacks { //By overriding the onConnect() and onDisconnect() functions
  7. void onConnect(BLEServer* pServer) { // When the Device is connected, "Device connected" is printed.
  8. Serial.println("Device connected");
  9. }
  10. void onDisconnect(BLEServer* pServer) { // "Device disconnected" will be printed when the device is disconnected
  11. Serial.println("Device disconnected");
  12. BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); // Re-broadcast so that the device can query
  13. pAdvertising->addServiceUUID(SERVICE_UUID); // Re-broadcast so that the device can query
  14. pAdvertising->setScanResponse(true); // Re-broadcast so that the device can query
  15. pAdvertising->setMinPreferred(0x06); // Re-broadcast so that the device can query
  16. pAdvertising->setMinPreferred(0x12); // Re-broadcast so that the device can query
  17. BLEDevice::startAdvertising(); // Re-broadcast so that the device can query
  18. pRxCharacteristic->notify(); // Re-broadcast so that the device can query
  19. pAdvertising->start(); // Re-broadcast so that the device can query
  20. }
  21. };
  22. class MyRXCallback : public BLECharacteristicCallbacks {
  23. void onWrite(BLECharacteristic* pCharacteristic) { // The onWrite function is called when the remote device sends data to your feature
  24. std::string rxValue = pCharacteristic->getValue();
  25. if (!rxValue.empty()) {
  26. // The received data rxValue is processed here
  27. if(rxValue.length() == 1)
  28. {
  29. printf("%s\n", rxValue.c_str()); // Print output through the serial port
  30. uint8_t* valueBytes = reinterpret_cast<uint8_t*>(const_cast<char*>(rxValue.c_str())); // Convert value to uint8 t*
  31. Relay_Analysis(valueBytes,Bluetooth_Mode); // pilot relay
  32. }
  33. else if(rxValue.length() == 2)
  34. {
  35. if(Extension_Enable)
  36. {
  37. printf("%s\n", rxValue.c_str()); // Print output through the serial port
  38. uint8_t* valueBytes = reinterpret_cast<uint8_t*>(const_cast<char*>(rxValue.c_str())); // Convert value to uint8 t*
  39. if(valueBytes[0] == 0x06) // Instruction check correct
  40. RS485_Analysis(valueBytes); // Control external relay
  41. else
  42. printf("Note : Non-instruction data was received - Bluetooth !\r\n");
  43. }
  44. else
  45. printf("Note : Non-instruction data was received - Bluetooth !\r\n");
  46. }
  47. else
  48. {
  49. printf("Note : Non-instruction data was received - Bluetooth !\r\n");
  50. }
  51. pRxCharacteristic->setValue(""); // After data is read, set it to blank for next read
  52. }
  53. }
  54. };
  55. void Bluetooth_SendData(char* Data) { // Send data using Bluetooth
  56. if (Data != nullptr && strlen(Data) > 0) {
  57. if (pServer->getConnectedCount() > 0) {
  58. std::string SendValue = Data;
  59. pTxCharacteristic->setValue(SendValue); // Set SendValue to the eigenvalue
  60. pTxCharacteristic->notify(); // Sends a notification to all connected devices
  61. }
  62. }
  63. }
  64. void Bluetooth_Init()
  65. {
  66. /*************************************************************************
  67. Bluetooth
  68. *************************************************************************/
  69. BLEDevice::init("ESP32 S3 Relay 6CH"); // Initialize Bluetooth and start broadcasting
  70. pServer = BLEDevice::createServer();
  71. pServer->setCallbacks(new MyServerCallbacks());
  72. BLEService* pService = pServer->createService(SERVICE_UUID);
  73. pTxCharacteristic = pService->createCharacteristic(
  74. TX_CHARACTERISTIC_UUID,
  75. BLECharacteristic:: PROPERTY_READ); // The eigenvalues are readable and can be read by remote devices
  76. pRxCharacteristic = pService->createCharacteristic(
  77. RX_CHARACTERISTIC_UUID,
  78. BLECharacteristic::PROPERTY_WRITE); // The eigenvalues are writable and can be written to by remote devices
  79. pRxCharacteristic->setCallbacks(new MyRXCallback());
  80. pRxCharacteristic->setValue("Successfully Connect To ESP32-S3-Relay-6CH");
  81. pService->start();
  82. BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  83. pAdvertising->addServiceUUID(SERVICE_UUID);
  84. pAdvertising->setScanResponse(true);
  85. pAdvertising->setMinPreferred(0x06);
  86. pAdvertising->setMinPreferred(0x12);
  87. BLEDevice::startAdvertising();
  88. pRxCharacteristic->notify();
  89. pAdvertising->start();
  90. RGB_Light(0, 0, 60);
  91. delay(1000);
  92. RGB_Light(0, 0, 0);
  93. printf("Now you can read it in your phone!\r\n");
  94. }