lora_ble.ino 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. Video: https://www.youtube.com/watch?v=oCMOYS71NIU
  3. Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
  4. Ported to Arduino ESP32 by Evandro Copercini
  5. Create a BLE server that, once we receive a connection, will send periodic notifications.
  6. The service advertises itself as: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
  7. Has a characteristic of: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E - used for receiving data with "WRITE"
  8. Has a characteristic of: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E - used to send data with "NOTIFY"
  9. The design of creating the BLE server is:
  10. 1. Create a BLE Server
  11. 2. Create a BLE Service
  12. 3. Create a BLE Characteristic on the Service
  13. 4. Create a BLE Descriptor on the characteristic
  14. 5. Start the service.
  15. 6. Start advertising.
  16. In this example rxValue is the data received (only accessible inside that function).
  17. And txValue is the data to be sent, in this example just a byte incremented every second.
  18. */
  19. #include <BLEDevice.h>
  20. #include <BLEServer.h>
  21. #include <BLEUtils.h>
  22. #include <BLE2902.h>
  23. #include "heltec.h"
  24. #define BAND 915E6 //you can set band here directly,e.g. 868E6,915E6
  25. String packSize = "--";
  26. String packet ;
  27. BLEServer *pServer = NULL;
  28. BLECharacteristic * pTxCharacteristic;
  29. bool deviceConnected = false;
  30. bool oldDeviceConnected = false;
  31. uint8_t txValue = 0;
  32. // See the following for generating UUIDs:
  33. // https://www.uuidgenerator.net/
  34. #define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
  35. #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
  36. #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
  37. class MyServerCallbacks: public BLEServerCallbacks {
  38. void onConnect(BLEServer* pServer) {
  39. Serial.println("just got a connection wooo");
  40. deviceConnected = true;
  41. };
  42. void onDisconnect(BLEServer* pServer) {
  43. deviceConnected = false;
  44. }
  45. };
  46. class MyCallbacks: public BLECharacteristicCallbacks {
  47. void onWrite(BLECharacteristic *pCharacteristic) {
  48. std::string rxValue = pCharacteristic->getValue();
  49. for (int i = 0; i < rxValue.length(); i++) {
  50. Serial.print(rxValue[i]);
  51. }
  52. Serial.println();
  53. String loraPacket = String(rxValue.c_str());
  54. if (rxValue.length() > 0) {
  55. LoRa.beginPacket();
  56. LoRa.print(loraPacket);
  57. LoRa.endPacket();
  58. }
  59. LoRa.receive();
  60. }
  61. };
  62. void cbk(int packetSize) {
  63. packet ="";
  64. packSize = String(packetSize,DEC);
  65. Serial.println("***********");
  66. for (int i = 0; i < packetSize; i++) { packet += (char) LoRa.read(); }
  67. Serial.println(packet);
  68. Serial.println("***********");
  69. pTxCharacteristic->setValue(packet.c_str());
  70. pTxCharacteristic->notify();
  71. LoRa.receive();
  72. }
  73. void setup() {
  74. Heltec.begin(false /*DisplayEnable Enable*/, true /*Heltec.Heltec.Heltec.LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /*long BAND*/);
  75. Serial.begin(115200);
  76. Serial.setRxBufferSize(1024);
  77. LoRa.setSignalBandwidth(250E3);
  78. LoRa.receive();
  79. // Create the BLE Device
  80. BLEDevice::init("UART Service");
  81. // Create the BLE Server
  82. pServer = BLEDevice::createServer();
  83. pServer->setCallbacks(new MyServerCallbacks());
  84. // Create the BLE Service
  85. BLEService *pService = pServer->createService(SERVICE_UUID);
  86. // Create a BLE Characteristic
  87. pTxCharacteristic = pService->createCharacteristic(
  88. CHARACTERISTIC_UUID_TX,
  89. BLECharacteristic::PROPERTY_NOTIFY
  90. );
  91. pTxCharacteristic->addDescriptor(new BLE2902());
  92. BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
  93. CHARACTERISTIC_UUID_RX,
  94. BLECharacteristic::PROPERTY_WRITE
  95. );
  96. pRxCharacteristic->setCallbacks(new MyCallbacks());
  97. // Start the service
  98. pService->start();
  99. // Start advertising
  100. pServer->getAdvertising()->start();
  101. Serial.println("Waiting a client connection to notify...");
  102. }
  103. void loop() {
  104. delay(5);
  105. // disconnecting
  106. if (!deviceConnected && oldDeviceConnected) {
  107. delay(500); // give the bluetooth stack the chance to get things ready
  108. pServer->startAdvertising(); // restart advertising
  109. Serial.println("start advertising");
  110. oldDeviceConnected = deviceConnected;
  111. }
  112. // connecting
  113. if (deviceConnected && !oldDeviceConnected) {
  114. // do stuff here on connecting
  115. oldDeviceConnected = deviceConnected;
  116. }
  117. int packetSize = LoRa.parsePacket();
  118. if (deviceConnected && packetSize) {
  119. cbk(packetSize);
  120. }
  121. }