aboutsummaryrefslogtreecommitdiff
path: root/_mussel2.c
blob: 2867777586a46f3b7272b55134c869d853511c61 (plain)
  1. /*
  2.    Based on 31337Ghost's reference code from https://github.com/nkolban/esp32-snippets/issues/385#issuecomment-362535434
  3. which is based on pcbreflux's Arduino ESP32 port of Neil Kolban's example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
  4. */
  5. /*
  6. Create a BLE server that will send periodic iBeacon frames.
  7. The design of creating the BLE server is:
  8. 1. Create a BLE Server
  9. 2. Create advertising data
  10. 3. Start advertising.
  11. 4. wait
  12. 5. Stop advertising.
  13. */
  14. #include <BLEDevice.h>
  15. #include <BLEServer.h>
  16. #include <BLEUtils.h>
  17. #include <BLE2902.h>
  18. #include <BLEBeacon.h>
  19. #define DEVICE_NAME "Cycles of five good then one bad"
  20. #define SERVICE_UUID "32eed4b3-e233-4808-a120-c4cee1a8e0c5"
  21. #define BEACON_UUID "84b7f168-a742-49b7-bd15-dc808dddb672"
  22. #define BEACON_UUID_REV "87a3c0f9-695f-4c04-8195-3a89d6cd44f0"
  23. #define CHARACTERISTIC_UUID "738e7085-9717-4288-ab21-c59ff7c5dd96"
  24. BLEServer *pServer;
  25. BLECharacteristic *pCharacteristic;
  26. bool deviceConnected = false;
  27. uint8_t value = 0;
  28. class MyServerCallbacks : public BLEServerCallbacks {
  29. void onConnect(BLEServer *pServer) {
  30. deviceConnected = true;
  31. Serial.println("deviceConnected = true");
  32. };
  33. void onDisconnect(BLEServer *pServer) {
  34. deviceConnected = false;
  35. Serial.println("deviceConnected = false");
  36. // Restart advertising to be visible and connectable again
  37. BLEAdvertising *pAdvertising;
  38. pAdvertising = pServer->getAdvertising();
  39. pAdvertising->start();
  40. Serial.println("iBeacon advertising restarted");
  41. }
  42. };
  43. class MyCallbacks : public BLECharacteristicCallbacks {
  44. void onWrite(BLECharacteristic *pCharacteristic) {
  45. String rxValue = pCharacteristic->getValue();
  46. if (rxValue.length() > 0) {
  47. Serial.println("*********");
  48. Serial.print("Received Value: ");
  49. for (int i = 0; i < rxValue.length(); i++) {
  50. Serial.print(rxValue[i]);
  51. }
  52. Serial.println();
  53. Serial.println("*********");
  54. }
  55. }
  56. };
  57. void init_service() {
  58. BLEAdvertising *pAdvertising;
  59. pAdvertising = pServer->getAdvertising();
  60. pAdvertising->stop();
  61. // Create the BLE Service
  62. BLEService *pService = pServer->createService(BLEUUID(SERVICE_UUID));
  63. // Create a BLE Characteristic
  64. pCharacteristic = pService->createCharacteristic(
  65. CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY
  66. );
  67. pCharacteristic->setCallbacks(new MyCallbacks());
  68. pCharacteristic->addDescriptor(new BLE2902());
  69. pAdvertising->addServiceUUID(BLEUUID(SERVICE_UUID));
  70. // Start the service
  71. pService->start();
  72. pAdvertising->start();
  73. }
  74. void init_beacon() {
  75. BLEAdvertising *pAdvertising;
  76. pAdvertising = pServer->getAdvertising();
  77. pAdvertising->stop();
  78. // iBeacon
  79. BLEBeacon myBeacon;
  80. myBeacon.setManufacturerId(0x4c00);
  81. myBeacon.setMajor(5);
  82. myBeacon.setMinor(88);
  83. myBeacon.setSignalPower(0xc5);
  84. myBeacon.setProximityUUID(BLEUUID(BEACON_UUID_REV));
  85. BLEAdvertisementData advertisementData;
  86. advertisementData.setFlags(0x1A);
  87. advertisementData.setManufacturerData(myBeacon.getData());
  88. pAdvertising->setAdvertisementData(advertisementData);
  89. pAdvertising->start();
  90. }
  91. void setup() {
  92. Serial.begin(115200);
  93. Serial.println();
  94. Serial.println("Initializing...");
  95. Serial.flush();
  96. BLEDevice::init(DEVICE_NAME);
  97. pServer = BLEDevice::createServer();
  98. pServer->setCallbacks(new MyServerCallbacks());
  99. init_service();
  100. init_beacon();
  101. Serial.printf("iBeacon + service defined and advertising: %s",
  102. DEVICE_NAME);
  103. }
  104. void loop() {
  105. if (deviceConnected) {
  106. // Get second-long cyclic counter 0-11
  107. unsigned long currentTime = millis();
  108. unsigned long totalSeconds = currentTime / 1000;
  109. unsigned long currentSecond = totalSeconds % 12;
  110. String msg = "good";
  111. if (currentSecond >= 9)
  112. msg = "bad";
  113. Serial.printf("*** NOTIFY: %s ***\n", msg);
  114. pCharacteristic->setValue(msg);
  115. pCharacteristic->notify();
  116. value++;
  117. }
  118. delay(2000);
  119. }