summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorNeil Kolban <kolban1@kolban.com>2018-04-03 19:34:25 -0500
committerNeil Kolban <kolban1@kolban.com>2018-04-03 19:34:25 -0500
commitb5d960bacc2c92770a42b4b4565f0e82c797bbc5 (patch)
tree4dfa590a744a7f282698cae709173391ebbe9011 /examples
parentSync for 0.4.9 (diff)
downloadthermostat-b5d960bacc2c92770a42b4b4565f0e82c797bbc5.tar.gz
thermostat-b5d960bacc2c92770a42b4b4565f0e82c797bbc5.tar.bz2
thermostat-b5d960bacc2c92770a42b4b4565f0e82c797bbc5.zip
Commit for 0.4.10
Diffstat (limited to 'examples')
-rw-r--r--examples/BLE_iBeacon/BLE_iBeacon.ino104
-rw-r--r--examples/BLE_notify/BLE_notify.ino35
-rw-r--r--examples/BLE_uart/BLE_uart.ino52
3 files changed, 160 insertions, 31 deletions
diff --git a/examples/BLE_iBeacon/BLE_iBeacon.ino b/examples/BLE_iBeacon/BLE_iBeacon.ino
new file mode 100644
index 0000000..5f6ed00
--- /dev/null
+++ b/examples/BLE_iBeacon/BLE_iBeacon.ino
@@ -0,0 +1,104 @@
+/*
+ Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
+ Ported to Arduino ESP32 by pcbreflux
+*/
+
+
+/*
+ Create a BLE server that will send periodic iBeacon frames.
+ The design of creating the BLE server is:
+ 1. Create a BLE Server
+ 2. Create advertising data
+ 3. Start advertising.
+ 4. wait
+ 5. Stop advertising.
+ 6. deep sleep
+
+*/
+#include "sys/time.h"
+
+#include "BLEDevice.h"
+#include "BLEServer.h"
+#include "BLEUtils.h"
+#include "BLEBeacon.h"
+#include "esp_sleep.h"
+
+#define GPIO_DEEP_SLEEP_DURATION 10 // sleep x seconds and then wake up
+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
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+uint8_t temprature_sens_read();
+//uint8_t g_phyFuns;
+
+#ifdef __cplusplus
+}
+#endif
+
+// See the following for generating UUIDs:
+// https://www.uuidgenerator.net/
+BLEAdvertising *pAdvertising;
+struct timeval now;
+
+#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/)
+
+void setBeacon() {
+
+ BLEBeacon oBeacon = BLEBeacon();
+ oBeacon.setManufacturerId(0x4C00); // fake Apple 0x004C LSB (ENDIAN_CHANGE_U16!)
+ oBeacon.setProximityUUID(BLEUUID(BEACON_UUID));
+ oBeacon.setMajor((bootcount & 0xFFFF0000) >> 16);
+ oBeacon.setMinor(bootcount&0xFFFF);
+ BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
+ BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
+
+ oAdvertisementData.setFlags(0x04); // BR_EDR_NOT_SUPPORTED 0x04
+
+ std::string strServiceData = "";
+
+ strServiceData += (char)26; // Len
+ strServiceData += (char)0xFF; // Type
+ strServiceData += oBeacon.getData();
+ oAdvertisementData.addData(strServiceData);
+
+ pAdvertising->setAdvertisementData(oAdvertisementData);
+ pAdvertising->setScanResponseData(oScanResponseData);
+
+}
+
+void setup() {
+
+
+ Serial.begin(115200);
+ gettimeofday(&now, NULL);
+
+ Serial.printf("start ESP32 %d\n",bootcount++);
+
+ Serial.printf("deep sleep (%lds since last reset, %lds since last boot)\n",now.tv_sec,now.tv_sec-last);
+
+ last = now.tv_sec;
+
+ // Create the BLE Device
+ BLEDevice::init("");
+
+ // Create the BLE Server
+ BLEServer *pServer = BLEDevice::createServer();
+
+ pAdvertising = pServer->getAdvertising();
+
+ setBeacon();
+ // Start advertising
+ pAdvertising->start();
+ Serial.println("Advertizing started...");
+ delay(100);
+ pAdvertising->stop();
+ Serial.printf("enter deep sleep\n");
+ esp_deep_sleep(1000000LL * GPIO_DEEP_SLEEP_DURATION);
+ Serial.printf("in deep sleep\n");
+}
+
+void loop() {
+}
diff --git a/examples/BLE_notify/BLE_notify.ino b/examples/BLE_notify/BLE_notify.ino
index 8d329c2..57ad7a7 100644
--- a/examples/BLE_notify/BLE_notify.ino
+++ b/examples/BLE_notify/BLE_notify.ino
@@ -23,8 +23,9 @@
#include <BLEUtils.h>
#include <BLE2902.h>
-BLECharacteristic *pCharacteristic;
+BLEServer *pServer = NULL;
bool deviceConnected = false;
+bool oldDeviceConnected = false;
uint8_t value = 0;
// See the following for generating UUIDs:
@@ -53,14 +54,14 @@ void setup() {
BLEDevice::init("MyESP32");
// Create the BLE Server
- BLEServer *pServer = BLEDevice::createServer();
+ pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
- pCharacteristic = pService->createCharacteristic(
+ BLECharacteristic * pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
@@ -81,13 +82,23 @@ void setup() {
}
void loop() {
-
- if (deviceConnected) {
- Serial.printf("*** NOTIFY: %d ***\n", value);
- pCharacteristic->setValue(&value, 1);
- pCharacteristic->notify();
- //pCharacteristic->indicate();
- value++;
- }
- delay(2000);
+ // notify changed value
+ if (deviceConnected) {
+ pCharacteristic->setValue(&value, 1);
+ pCharacteristic->notify();
+ value++;
+ delay(10); // bluetooth stack will go into congestion, if too many packets are sent
+ }
+ // disconnecting
+ if (!deviceConnected && oldDeviceConnected) {
+ delay(500); // give the bluetooth stack the chance to get things ready
+ pServer->startAdvertising(); // restart advertising
+ Serial.println("start advertising");
+ oldDeviceConnected = deviceConnected;
+ }
+ // connecting
+ if (deviceConnected && !oldDeviceConnected) {
+ // do stuff here on connecting
+ oldDeviceConnected = deviceConnected;
+ }
} \ No newline at end of file
diff --git a/examples/BLE_uart/BLE_uart.ino b/examples/BLE_uart/BLE_uart.ino
index a348a66..35b570b 100644
--- a/examples/BLE_uart/BLE_uart.ino
+++ b/examples/BLE_uart/BLE_uart.ino
@@ -24,8 +24,10 @@
#include <BLEUtils.h>
#include <BLE2902.h>
-BLECharacteristic *pCharacteristic;
+BLEServer *pServer = NULL;
+BLECharacteristic * pTxCharacteristic;
bool deviceConnected = false;
+bool oldDeviceConnected = false;
uint8_t txValue = 0;
// See the following for generating UUIDs:
@@ -70,26 +72,26 @@ void setup() {
BLEDevice::init("UART Service");
// Create the BLE Server
- BLEServer *pServer = BLEDevice::createServer();
+ pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
- pCharacteristic = pService->createCharacteristic(
- CHARACTERISTIC_UUID_TX,
- BLECharacteristic::PROPERTY_NOTIFY
- );
+ pTxCharacteristic = pService->createCharacteristic(
+ CHARACTERISTIC_UUID_TX,
+ BLECharacteristic::PROPERTY_NOTIFY
+ );
- pCharacteristic->addDescriptor(new BLE2902());
+ pTxCharacteristic->addDescriptor(new BLE2902());
- BLECharacteristic *pCharacteristic = pService->createCharacteristic(
- CHARACTERISTIC_UUID_RX,
- BLECharacteristic::PROPERTY_WRITE
- );
+ BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
+ CHARACTERISTIC_UUID_RX,
+ BLECharacteristic::PROPERTY_WRITE
+ );
- pCharacteristic->setCallbacks(new MyCallbacks());
+ pRxCharacteristic->setCallbacks(new MyCallbacks());
// Start the service
pService->start();
@@ -101,11 +103,23 @@ void setup() {
void loop() {
- if (deviceConnected) {
- Serial.printf("*** Sent Value: %d ***\n", txValue);
- pCharacteristic->setValue(&txValue, 1);
- pCharacteristic->notify();
- txValue++;
- }
- delay(1000);
+ if (deviceConnected) {
+ pTxCharacteristic->setValue(&txValue, 1);
+ pTxCharacteristic->notify();
+ txValue++;
+ delay(10); // bluetooth stack will go into congestion, if too many packets are sent
+ }
+
+ // disconnecting
+ if (!deviceConnected && oldDeviceConnected) {
+ delay(500); // give the bluetooth stack the chance to get things ready
+ pServer->startAdvertising(); // restart advertising
+ Serial.println("start advertising");
+ oldDeviceConnected = deviceConnected;
+ }
+ // connecting
+ if (deviceConnected && !oldDeviceConnected) {
+ // do stuff here on connecting
+ oldDeviceConnected = deviceConnected;
+ }
}