replaced original BLE_iBeacon.ino example with a known-good one (#7470)

* replaced original iBeacon example with a known-good one

* addressed VojtechBartoska's comment

* incorporated P-R-O-C-H-Y's change

Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com>
This commit is contained in:
Bob Igo 2022-12-21 10:35:56 -05:00 committed by GitHub
parent e69081c12f
commit e9c125fb22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,104 +1,138 @@
/* /*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp Based on 31337Ghost's reference code from https://github.com/nkolban/esp32-snippets/issues/385#issuecomment-362535434
Ported to Arduino ESP32 by pcbreflux 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
*/ */
/*
/* Create a BLE server that will send periodic iBeacon frames.
Create a BLE server that will send periodic iBeacon frames. The design of creating the BLE server is:
The design of creating the BLE server is: 1. Create a BLE Server
1. Create a BLE Server 2. Create advertising data
2. Create advertising data 3. Start advertising.
3. Start advertising. 4. wait
4. wait 5. Stop advertising.
5. Stop advertising. */
6. deep sleep #include <BLEDevice.h>
#include <BLEServer.h>
*/ #include <BLEUtils.h>
#include "sys/time.h" #include <BLE2902.h>
#include <BLEBeacon.h>
#include "BLEDevice.h"
#include "BLEUtils.h" #define DEVICE_NAME "ESP32"
#include "BLEBeacon.h" #define SERVICE_UUID "7A0247E7-8E88-409B-A959-AB5092DDB03E"
#include "esp_sleep.h" #define BEACON_UUID "2D7A9F0C-E0E8-4CC9-A71B-A21DB2D034A1"
#define BEACON_UUID_REV "A134D0B2-1DA2-1BA7-C94C-E8E00C9F7A2D"
#define GPIO_DEEP_SLEEP_DURATION 10 // sleep x seconds and then wake up #define CHARACTERISTIC_UUID "82258BAA-DF72-47E8-99BC-B73D7ECD08A5"
RTC_DATA_ATTR static time_t last; // remember last boot in RTC Memory
RTC_DATA_ATTR static uint32_t bootcount; // remember number of boots in RTC Memory BLEServer *pServer;
BLECharacteristic *pCharacteristic;
#ifdef __cplusplus bool deviceConnected = false;
extern "C" { uint8_t value = 0;
#endif
class MyServerCallbacks: public BLEServerCallbacks {
uint8_t temprature_sens_read(); void onConnect(BLEServer* pServer) {
//uint8_t g_phyFuns; deviceConnected = true;
Serial.println("deviceConnected = true");
#ifdef __cplusplus };
}
#endif void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
// See the following for generating UUIDs: Serial.println("deviceConnected = false");
// https://www.uuidgenerator.net/
BLEAdvertising *pAdvertising; // Restart advertising to be visible and connectable again
struct timeval now; BLEAdvertising* pAdvertising;
pAdvertising = pServer->getAdvertising();
#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/) pAdvertising->start();
Serial.println("iBeacon advertising restarted");
void setBeacon() { }
};
BLEBeacon oBeacon = BLEBeacon();
oBeacon.setManufacturerId(0x4C00); // fake Apple 0x004C LSB (ENDIAN_CHANGE_U16!) class MyCallbacks: public BLECharacteristicCallbacks {
oBeacon.setProximityUUID(BLEUUID(BEACON_UUID)); void onWrite(BLECharacteristic *pCharacteristic) {
oBeacon.setMajor((bootcount & 0xFFFF0000) >> 16); std::string rxValue = pCharacteristic->getValue();
oBeacon.setMinor(bootcount&0xFFFF);
BLEAdvertisementData oAdvertisementData = BLEAdvertisementData(); if (rxValue.length() > 0) {
BLEAdvertisementData oScanResponseData = BLEAdvertisementData(); Serial.println("*********");
Serial.print("Received Value: ");
oAdvertisementData.setFlags(0x04); // BR_EDR_NOT_SUPPORTED 0x04 for (int i = 0; i < rxValue.length(); i++) {
Serial.print(rxValue[i]);
std::string strServiceData = ""; }
Serial.println();
strServiceData += (char)26; // Len Serial.println("*********");
strServiceData += (char)0xFF; // Type
strServiceData += oBeacon.getData(); }
oAdvertisementData.addData(strServiceData); }
};
pAdvertising->setAdvertisementData(oAdvertisementData);
pAdvertising->setScanResponseData(oScanResponseData);
pAdvertising->setAdvertisementType(ADV_TYPE_NONCONN_IND); void init_service() {
BLEAdvertising* pAdvertising;
} pAdvertising = pServer->getAdvertising();
pAdvertising->stop();
void setup() {
// Create the BLE Service
BLEService *pService = pServer->createService(BLEUUID(SERVICE_UUID));
Serial.begin(115200);
gettimeofday(&now, NULL); // Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
Serial.printf("start ESP32 %d\n",bootcount++); CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
Serial.printf("deep sleep (%lds since last reset, %lds since last boot)\n",now.tv_sec,now.tv_sec-last); BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY
last = now.tv_sec; );
pCharacteristic->setCallbacks(new MyCallbacks());
// Create the BLE Device pCharacteristic->addDescriptor(new BLE2902());
BLEDevice::init("");
pAdvertising->addServiceUUID(BLEUUID(SERVICE_UUID));
// Create the BLE Server
// BLEServer *pServer = BLEDevice::createServer(); // <-- no longer required to instantiate BLEServer, less flash and ram usage // Start the service
pService->start();
pAdvertising = BLEDevice::getAdvertising();
pAdvertising->start();
setBeacon(); }
// Start advertising
pAdvertising->start(); void init_beacon() {
Serial.println("Advertizing started..."); BLEAdvertising* pAdvertising;
delay(100); pAdvertising = pServer->getAdvertising();
pAdvertising->stop(); pAdvertising->stop();
Serial.printf("enter deep sleep\n"); // iBeacon
esp_deep_sleep(1000000LL * GPIO_DEEP_SLEEP_DURATION); BLEBeacon myBeacon;
Serial.printf("in deep sleep\n"); myBeacon.setManufacturerId(0x4c00);
} myBeacon.setMajor(5);
myBeacon.setMinor(88);
void loop() { myBeacon.setSignalPower(0xc5);
} myBeacon.setProximityUUID(BLEUUID(BEACON_UUID_REV));
BLEAdvertisementData advertisementData;
advertisementData.setFlags(0x1A);
advertisementData.setManufacturerData(myBeacon.getData());
pAdvertising->setAdvertisementData(advertisementData);
pAdvertising->start();
}
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Initializing...");
Serial.flush();
BLEDevice::init(DEVICE_NAME);
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
init_service();
init_beacon();
Serial.println("iBeacon + service defined and advertising!");
}
void loop() {
if (deviceConnected) {
Serial.printf("*** NOTIFY: %d ***\n", value);
pCharacteristic->setValue(&value, 1);
pCharacteristic->notify();
value++;
}
delay(2000);
}