|
1 |
| -/* |
2 |
| - Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp |
3 |
| - Ported to Arduino ESP32 by pcbreflux |
4 |
| -*/ |
5 |
| - |
6 |
| - |
7 |
| -/* |
8 |
| - Create a BLE server that will send periodic iBeacon frames. |
9 |
| - The design of creating the BLE server is: |
10 |
| - 1. Create a BLE Server |
11 |
| - 2. Create advertising data |
12 |
| - 3. Start advertising. |
13 |
| - 4. wait |
14 |
| - 5. Stop advertising. |
15 |
| - 6. deep sleep |
16 |
| - |
17 |
| -*/ |
18 |
| -#include "sys/time.h" |
19 |
| - |
20 |
| -#include "BLEDevice.h" |
21 |
| -#include "BLEUtils.h" |
22 |
| -#include "BLEBeacon.h" |
23 |
| -#include "esp_sleep.h" |
24 |
| - |
25 |
| -#define GPIO_DEEP_SLEEP_DURATION 10 // sleep x seconds and then wake up |
26 |
| -RTC_DATA_ATTR static time_t last; // remember last boot in RTC Memory |
27 |
| -RTC_DATA_ATTR static uint32_t bootcount; // remember number of boots in RTC Memory |
28 |
| - |
29 |
| -#ifdef __cplusplus |
30 |
| -extern "C" { |
31 |
| -#endif |
32 |
| - |
33 |
| -uint8_t temprature_sens_read(); |
34 |
| -//uint8_t g_phyFuns; |
35 |
| - |
36 |
| -#ifdef __cplusplus |
37 |
| -} |
38 |
| -#endif |
39 |
| - |
40 |
| -// See the following for generating UUIDs: |
41 |
| -// https://www.uuidgenerator.net/ |
42 |
| -BLEAdvertising *pAdvertising; |
43 |
| -struct timeval now; |
44 |
| - |
45 |
| -#define BEACON_UUID "8ec76ea3-6668-48da-9866-75be8bc86f4d" // UUID 1 128-Bit (may use linux tool uuidgen or random numbers via https://www.uuidgenerator.net/) |
46 |
| - |
47 |
| -void setBeacon() { |
48 |
| - |
49 |
| - BLEBeacon oBeacon = BLEBeacon(); |
50 |
| - oBeacon.setManufacturerId(0x4C00); // fake Apple 0x004C LSB (ENDIAN_CHANGE_U16!) |
51 |
| - oBeacon.setProximityUUID(BLEUUID(BEACON_UUID)); |
52 |
| - oBeacon.setMajor((bootcount & 0xFFFF0000) >> 16); |
53 |
| - oBeacon.setMinor(bootcount&0xFFFF); |
54 |
| - BLEAdvertisementData oAdvertisementData = BLEAdvertisementData(); |
55 |
| - BLEAdvertisementData oScanResponseData = BLEAdvertisementData(); |
56 |
| - |
57 |
| - oAdvertisementData.setFlags(0x04); // BR_EDR_NOT_SUPPORTED 0x04 |
58 |
| - |
59 |
| - std::string strServiceData = ""; |
60 |
| - |
61 |
| - strServiceData += (char)26; // Len |
62 |
| - strServiceData += (char)0xFF; // Type |
63 |
| - strServiceData += oBeacon.getData(); |
64 |
| - oAdvertisementData.addData(strServiceData); |
65 |
| - |
66 |
| - pAdvertising->setAdvertisementData(oAdvertisementData); |
67 |
| - pAdvertising->setScanResponseData(oScanResponseData); |
68 |
| - pAdvertising->setAdvertisementType(ADV_TYPE_NONCONN_IND); |
69 |
| - |
70 |
| -} |
71 |
| - |
72 |
| -void setup() { |
73 |
| - |
74 |
| - |
75 |
| - Serial.begin(115200); |
76 |
| - gettimeofday(&now, NULL); |
77 |
| - |
78 |
| - Serial.printf("start ESP32 %d\n",bootcount++); |
79 |
| - |
80 |
| - Serial.printf("deep sleep (%lds since last reset, %lds since last boot)\n",now.tv_sec,now.tv_sec-last); |
81 |
| - |
82 |
| - last = now.tv_sec; |
83 |
| - |
84 |
| - // Create the BLE Device |
85 |
| - BLEDevice::init(""); |
86 |
| - |
87 |
| - // Create the BLE Server |
88 |
| - // BLEServer *pServer = BLEDevice::createServer(); // <-- no longer required to instantiate BLEServer, less flash and ram usage |
89 |
| - |
90 |
| - pAdvertising = BLEDevice::getAdvertising(); |
91 |
| - |
92 |
| - setBeacon(); |
93 |
| - // Start advertising |
94 |
| - pAdvertising->start(); |
95 |
| - Serial.println("Advertizing started..."); |
96 |
| - delay(100); |
97 |
| - pAdvertising->stop(); |
98 |
| - Serial.printf("enter deep sleep\n"); |
99 |
| - esp_deep_sleep(1000000LL * GPIO_DEEP_SLEEP_DURATION); |
100 |
| - Serial.printf("in deep sleep\n"); |
101 |
| -} |
102 |
| - |
103 |
| -void loop() { |
104 |
| -} |
| 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 | +/* |
| 7 | + Create a BLE server that will send periodic iBeacon frames. |
| 8 | + The design of creating the BLE server is: |
| 9 | + 1. Create a BLE Server |
| 10 | + 2. Create advertising data |
| 11 | + 3. Start advertising. |
| 12 | + 4. wait |
| 13 | + 5. Stop advertising. |
| 14 | +*/ |
| 15 | +#include <BLEDevice.h> |
| 16 | +#include <BLEServer.h> |
| 17 | +#include <BLEUtils.h> |
| 18 | +#include <BLE2902.h> |
| 19 | +#include <BLEBeacon.h> |
| 20 | + |
| 21 | +#define DEVICE_NAME "ESP32" |
| 22 | +#define SERVICE_UUID "7A0247E7-8E88-409B-A959-AB5092DDB03E" |
| 23 | +#define BEACON_UUID "2D7A9F0C-E0E8-4CC9-A71B-A21DB2D034A1" |
| 24 | +#define BEACON_UUID_REV "A134D0B2-1DA2-1BA7-C94C-E8E00C9F7A2D" |
| 25 | +#define CHARACTERISTIC_UUID "82258BAA-DF72-47E8-99BC-B73D7ECD08A5" |
| 26 | + |
| 27 | +BLEServer *pServer; |
| 28 | +BLECharacteristic *pCharacteristic; |
| 29 | +bool deviceConnected = false; |
| 30 | +uint8_t value = 0; |
| 31 | + |
| 32 | +class MyServerCallbacks: public BLEServerCallbacks { |
| 33 | + void onConnect(BLEServer* pServer) { |
| 34 | + deviceConnected = true; |
| 35 | + Serial.println("deviceConnected = true"); |
| 36 | + }; |
| 37 | + |
| 38 | + void onDisconnect(BLEServer* pServer) { |
| 39 | + deviceConnected = false; |
| 40 | + Serial.println("deviceConnected = false"); |
| 41 | + |
| 42 | + // Restart advertising to be visible and connectable again |
| 43 | + BLEAdvertising* pAdvertising; |
| 44 | + pAdvertising = pServer->getAdvertising(); |
| 45 | + pAdvertising->start(); |
| 46 | + Serial.println("iBeacon advertising restarted"); |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +class MyCallbacks: public BLECharacteristicCallbacks { |
| 51 | + void onWrite(BLECharacteristic *pCharacteristic) { |
| 52 | + std::string rxValue = pCharacteristic->getValue(); |
| 53 | + |
| 54 | + if (rxValue.length() > 0) { |
| 55 | + Serial.println("*********"); |
| 56 | + Serial.print("Received Value: "); |
| 57 | + for (int i = 0; i < rxValue.length(); i++) { |
| 58 | + Serial.print(rxValue[i]); |
| 59 | + } |
| 60 | + Serial.println(); |
| 61 | + Serial.println("*********"); |
| 62 | + |
| 63 | + } |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | + |
| 68 | +void init_service() { |
| 69 | + BLEAdvertising* pAdvertising; |
| 70 | + pAdvertising = pServer->getAdvertising(); |
| 71 | + pAdvertising->stop(); |
| 72 | + |
| 73 | + // Create the BLE Service |
| 74 | + BLEService *pService = pServer->createService(BLEUUID(SERVICE_UUID)); |
| 75 | + |
| 76 | + // Create a BLE Characteristic |
| 77 | + pCharacteristic = pService->createCharacteristic( |
| 78 | + CHARACTERISTIC_UUID, |
| 79 | + BLECharacteristic::PROPERTY_READ | |
| 80 | + BLECharacteristic::PROPERTY_WRITE | |
| 81 | + BLECharacteristic::PROPERTY_NOTIFY |
| 82 | + ); |
| 83 | + pCharacteristic->setCallbacks(new MyCallbacks()); |
| 84 | + pCharacteristic->addDescriptor(new BLE2902()); |
| 85 | + |
| 86 | + pAdvertising->addServiceUUID(BLEUUID(SERVICE_UUID)); |
| 87 | + |
| 88 | + // Start the service |
| 89 | + pService->start(); |
| 90 | + |
| 91 | + pAdvertising->start(); |
| 92 | +} |
| 93 | + |
| 94 | +void init_beacon() { |
| 95 | + BLEAdvertising* pAdvertising; |
| 96 | + pAdvertising = pServer->getAdvertising(); |
| 97 | + pAdvertising->stop(); |
| 98 | + // iBeacon |
| 99 | + BLEBeacon myBeacon; |
| 100 | + myBeacon.setManufacturerId(0x4c00); |
| 101 | + myBeacon.setMajor(5); |
| 102 | + myBeacon.setMinor(88); |
| 103 | + myBeacon.setSignalPower(0xc5); |
| 104 | + myBeacon.setProximityUUID(BLEUUID(BEACON_UUID_REV)); |
| 105 | + |
| 106 | + BLEAdvertisementData advertisementData; |
| 107 | + advertisementData.setFlags(0x1A); |
| 108 | + advertisementData.setManufacturerData(myBeacon.getData()); |
| 109 | + pAdvertising->setAdvertisementData(advertisementData); |
| 110 | + |
| 111 | + pAdvertising->start(); |
| 112 | +} |
| 113 | + |
| 114 | +void setup() { |
| 115 | + Serial.begin(115200); |
| 116 | + Serial.println(); |
| 117 | + Serial.println("Initializing..."); |
| 118 | + Serial.flush(); |
| 119 | + |
| 120 | + BLEDevice::init(DEVICE_NAME); |
| 121 | + pServer = BLEDevice::createServer(); |
| 122 | + pServer->setCallbacks(new MyServerCallbacks()); |
| 123 | + |
| 124 | + init_service(); |
| 125 | + init_beacon(); |
| 126 | + |
| 127 | + Serial.println("iBeacon + service defined and advertising!"); |
| 128 | +} |
| 129 | + |
| 130 | +void loop() { |
| 131 | + if (deviceConnected) { |
| 132 | + Serial.printf("*** NOTIFY: %d ***\n", value); |
| 133 | + pCharacteristic->setValue(&value, 1); |
| 134 | + pCharacteristic->notify(); |
| 135 | + value++; |
| 136 | + } |
| 137 | + delay(2000); |
| 138 | +} |
0 commit comments