Skip to content

Commit d37b1df

Browse files
committed
replaced original iBeacon example with a known-good one
1 parent 9762b23 commit d37b1df

File tree

1 file changed

+131
-104
lines changed

1 file changed

+131
-104
lines changed
Lines changed: 131 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,131 @@
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+
*/
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+
20+
#define DEVICE_NAME "ESP32"
21+
#define SERVICE_UUID "7A0247E7-8E88-409B-A959-AB5092DDB03E"
22+
#define BEACON_UUID "2D7A9F0C-E0E8-4CC9-A71B-A21DB2D034A1"
23+
#define BEACON_UUID_REV "A134D0B2-1DA2-1BA7-C94C-E8E00C9F7A2D"
24+
#define CHARACTERISTIC_UUID "82258BAA-DF72-47E8-99BC-B73D7ECD08A5"
25+
26+
BLEServer *pServer;
27+
BLECharacteristic *pCharacteristic;
28+
bool deviceConnected = false;
29+
uint8_t value = 0;
30+
31+
class MyServerCallbacks: public BLEServerCallbacks {
32+
void onConnect(BLEServer* pServer) {
33+
deviceConnected = true;
34+
Serial.println("deviceConnected = true");
35+
};
36+
37+
void onDisconnect(BLEServer* pServer) {
38+
deviceConnected = false;
39+
Serial.println("deviceConnected = false");
40+
}
41+
};
42+
43+
class MyCallbacks: public BLECharacteristicCallbacks {
44+
void onWrite(BLECharacteristic *pCharacteristic) {
45+
std::string rxValue = pCharacteristic->getValue();
46+
47+
if (rxValue.length() > 0) {
48+
Serial.println("*********");
49+
Serial.print("Received Value: ");
50+
for (int i = 0; i < rxValue.length(); i++) {
51+
Serial.print(rxValue[i]);
52+
}
53+
Serial.println();
54+
Serial.println("*********");
55+
56+
}
57+
}
58+
};
59+
60+
61+
void init_service() {
62+
BLEAdvertising* pAdvertising;
63+
pAdvertising = pServer->getAdvertising();
64+
pAdvertising->stop();
65+
66+
// Create the BLE Service
67+
BLEService *pService = pServer->createService(BLEUUID(SERVICE_UUID));
68+
69+
// Create a BLE Characteristic
70+
pCharacteristic = pService->createCharacteristic(
71+
CHARACTERISTIC_UUID,
72+
BLECharacteristic::PROPERTY_READ |
73+
BLECharacteristic::PROPERTY_WRITE |
74+
BLECharacteristic::PROPERTY_NOTIFY
75+
);
76+
pCharacteristic->setCallbacks(new MyCallbacks());
77+
pCharacteristic->addDescriptor(new BLE2902());
78+
79+
pAdvertising->addServiceUUID(BLEUUID(SERVICE_UUID));
80+
81+
// Start the service
82+
pService->start();
83+
84+
pAdvertising->start();
85+
}
86+
87+
void init_beacon() {
88+
BLEAdvertising* pAdvertising;
89+
pAdvertising = pServer->getAdvertising();
90+
pAdvertising->stop();
91+
// iBeacon
92+
BLEBeacon myBeacon;
93+
myBeacon.setManufacturerId(0x4c00);
94+
myBeacon.setMajor(5);
95+
myBeacon.setMinor(88);
96+
myBeacon.setSignalPower(0xc5);
97+
myBeacon.setProximityUUID(BLEUUID(BEACON_UUID_REV));
98+
99+
BLEAdvertisementData advertisementData;
100+
advertisementData.setFlags(0x1A);
101+
advertisementData.setManufacturerData(myBeacon.getData());
102+
pAdvertising->setAdvertisementData(advertisementData);
103+
104+
pAdvertising->start();
105+
}
106+
107+
void setup() {
108+
Serial.begin(115200);
109+
Serial.println();
110+
Serial.println("Initializing...");
111+
Serial.flush();
112+
113+
BLEDevice::init(DEVICE_NAME);
114+
pServer = BLEDevice::createServer();
115+
pServer->setCallbacks(new MyServerCallbacks());
116+
117+
init_service();
118+
init_beacon();
119+
120+
Serial.println("iBeacon + service defined and advertising!");
121+
}
122+
123+
void loop() {
124+
if (deviceConnected) {
125+
Serial.printf("*** NOTIFY: %d ***\n", value);
126+
pCharacteristic->setValue(&value, 1);
127+
pCharacteristic->notify();
128+
value++;
129+
}
130+
delay(2000);
131+
}

0 commit comments

Comments
 (0)