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