/
ESP32_Eddystone_TLM_deepsleep.ino
125 lines (98 loc) · 3.68 KB
/
ESP32_Eddystone_TLM_deepsleep.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* Copyright (c) 2018 pcbreflux. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*/
/*
Create a BLE server that will send periodic eddystone 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 "esp_sleep.h"
#define GPIO_DEEP_SLEEP_DURATION 2 // sleep 2 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;
void setBeacon() {
char beacon_data[22];
uint16_t beconUUID = 0xFEAA;
uint16_t volt = 3300; // 3300mV = 3.3V
uint16_t temp = (uint16_t)((float)23.00);
uint32_t tmil = now.tv_sec*10;
uint8_t temp_farenheit;
float temp_celsius;
temp_farenheit= temprature_sens_read();
temp_celsius = ( temp_farenheit - 32 ) / 1.8;
temp = (uint16_t)(temp_celsius);
BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
oAdvertisementData.setFlags(0x06); // GENERAL_DISC_MODE 0x02 | BR_EDR_NOT_SUPPORTED 0x04
oAdvertisementData.setCompleteServices(BLEUUID(beconUUID));
beacon_data[0] = 0x20; // Eddystone Frame Type (Unencrypted Eddystone-TLM)
beacon_data[1] = 0x00; // TLM version
beacon_data[2] = (volt>>8); // Battery voltage, 1 mV/bit i.e. 0xCE4 = 3300mV = 3.3V
beacon_data[3] = (volt&0xFF); //
beacon_data[4] = (temp&0xFF); // Beacon temperature
beacon_data[5] = (temp>>8); //
beacon_data[6] = ((bootcount & 0xFF000000) >> 24); // Advertising PDU count
beacon_data[7] = ((bootcount & 0xFF0000) >> 16); //
beacon_data[8] = ((bootcount & 0xFF00) >> 8); //
beacon_data[9] = (bootcount&0xFF); //
beacon_data[10] = ((tmil & 0xFF000000) >> 24); // Time since power-on or reboot
beacon_data[11] = ((tmil & 0xFF0000) >> 16); //
beacon_data[12] = ((tmil & 0xFF00) >> 8); //
beacon_data[13] = (tmil&0xFF); //
oAdvertisementData.setServiceData(BLEUUID(beconUUID), std::string(beacon_data, 14));
pAdvertising->setScanResponseData(oAdvertisementData);
}
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("ESP32");
// 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() {
}