Skip to content

Commit 3ee9d53

Browse files
committed
Fixed temp
1 parent 50e1c7e commit 3ee9d53

File tree

6 files changed

+65
-115
lines changed

6 files changed

+65
-115
lines changed

libraries/BLE/examples/BLE_Beacon_Scanner/BLE_Beacon_Scanner.ino

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,30 +99,17 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
9999
Serial.printf("TX power %d\n", foundEddyURL.getPower());
100100
Serial.println("\n");
101101
}
102-
else if (payLoad[11] == 0x20)
102+
if (advertisedDevice.getPayloadLength() >= 22 && payLoad[22] == 0x20)
103103
{
104-
Serial.println("Found an EddystoneTLM beacon!");
105-
BLEEddystoneTLM foundEddyURL = BLEEddystoneTLM();
106-
std::string eddyContent((char *)&payLoad[11]); // incomplete EddystoneURL struct!
107-
108-
eddyContent = "01234567890123";
109-
110-
for (int idx = 0; idx < 14; idx++)
111-
{
112-
eddyContent[idx] = payLoad[idx + 11];
113-
}
114-
115-
foundEddyURL.setData(eddyContent);
116-
Serial.printf("Reported battery voltage: %dmV\n", foundEddyURL.getVolt());
117-
Serial.printf("Reported temperature from TLM class: %.2fC\n", (double)foundEddyURL.getTemp());
118-
int16_t temp = (int16_t)payLoad[16] + (int16_t)(payLoad[15] << 8);
119-
float calcTemp = temp / 256.0f;
120-
Serial.printf("Reported temperature from data: %.2fC\n", calcTemp);
121-
Serial.printf("Reported advertise count: %d\n", foundEddyURL.getCount());
122-
Serial.printf("Reported time since last reboot: %ds\n", foundEddyURL.getTime());
123-
Serial.println("\n");
124-
Serial.print(foundEddyURL.toString().c_str());
125-
Serial.println("\n");
104+
Serial.printf("Found an EddystoneTLM beacon! payload length = %d B; minus 22 = %d\n", advertisedDevice.getPayloadLength(), advertisedDevice.getPayloadLength() - 22);
105+
BLEEddystoneTLM eddystoneTLM;
106+
//std::string TLMframe(payLoad[22], advertisedDevice.getPayloadLength() - 22);
107+
//eddystoneTLM.setData(TLMFrame);
108+
eddystoneTLM.setData(std::string((char*)payLoad+22, advertisedDevice.getPayloadLength() - 22));
109+
Serial.printf("Reported battery voltage: %dmV\n", eddystoneTLM.getVolt());
110+
Serial.printf("Reported temperature: %.2f°C (raw data=0x%04X)\n", eddystoneTLM.getFloatTemp(), eddystoneTLM.getTemp());
111+
Serial.printf("Reported advertise count: %d\n", eddystoneTLM.getCount());
112+
Serial.printf("Reported time since last reboot: %ds\n", eddystoneTLM.getTime());
126113
}
127114
}
128115
}

libraries/BLE/examples/BLE_EddystoneTLM_Beacon/BLE_EddystoneTLM_Beacon.ino

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
5. Stop advertising.
1414
6. deep sleep
1515
16+
To read data advertised by this beacon use second ESP with example sketch BLE_Beacon_Scanner
1617
*/
1718
#include "sys/time.h"
1819

@@ -22,7 +23,7 @@
2223
#include "BLEUtils.h"
2324
#include "BLEBeacon.h"
2425
#include "BLEAdvertising.h"
25-
#include "BLEEddystoneURL.h"
26+
#include "BLEEddystoneTLM.h"
2627

2728
#include "esp_sleep.h"
2829

@@ -48,10 +49,10 @@ void setBeacon()
4849
char beacon_data[25];
4950
uint16_t beconUUID = 0xFEAA;
5051
uint16_t volt = random(2800, 3700); // 3300mV = 3.3V
51-
float tempFloat = random(2000, 3100) / 100.0f;
52-
Serial.printf("Random temperature is %.2fC\n", tempFloat);
53-
int temp = (int)(tempFloat * 256); //(uint16_t)((float)23.00);
54-
Serial.printf("Converted to 8.8 format %0X%0X\n", (temp >> 8), (temp & 0xFF));
52+
float tempFloat = random(-3000, 3000) / 100.0f;
53+
Serial.printf("Random temperature is %.2f°C\n", tempFloat);
54+
int temp = EDDYSTONE_TEMP_FLOAT_TO_U16(tempFloat);
55+
Serial.printf("Converted to 8.8 format 0x%04X\n", temp);
5556

5657
BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
5758
BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
@@ -82,13 +83,11 @@ void setBeacon()
8283

8384
void setup()
8485
{
85-
8686
Serial.begin(115200);
8787
gettimeofday(&nowTimeStruct, NULL);
8888

89-
Serial.printf("start ESP32 %d\n", bootcount++);
90-
91-
Serial.printf("deep sleep (%lds since last reset, %lds since last boot)\n", nowTimeStruct.tv_sec, nowTimeStruct.tv_sec - last);
89+
Serial.printf("Starting ESP32. Bootcount = %d\n", bootcount++);
90+
Serial.printf("Deep sleep (%lds since last reset, %lds since last boot)\n", nowTimeStruct.tv_sec, nowTimeStruct.tv_sec - last);
9291

9392
last = nowTimeStruct.tv_sec;
9493
lastTenth = nowTimeStruct.tv_sec * 10; // Time since last reset as 0.1 second resolution counter
@@ -103,12 +102,11 @@ void setup()
103102
setBeacon();
104103
// Start advertising
105104
pAdvertising->start();
106-
Serial.println("Advertizing started for 10s ...");
105+
Serial.println("Advertising started for 10s ...");
107106
delay(10000);
108107
pAdvertising->stop();
109-
Serial.printf("enter deep sleep for 10s\n");
108+
Serial.printf("Enter deep sleep for 10s\n");
110109
esp_deep_sleep(1000000LL * GPIO_DEEP_SLEEP_DURATION);
111-
Serial.printf("in deep sleep\n");
112110
}
113111

114112
void loop()

libraries/BLE/examples/BLE_EddystoneTLM_Client/BLE_EddystoneTLM_Client.ino

Lines changed: 0 additions & 48 deletions
This file was deleted.

libraries/BLE/src/BLEAdvertising.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,11 @@ void BLEAdvertisementData::setPartialServices(BLEUUID uuid) {
443443
* @brief Set the service data (UUID + data)
444444
* @param [in] uuid The UUID to set with the service data. Size of UUID will be used.
445445
* @param [in] data The data to be associated with the service data advert.
446+
* Data frame:
447+
* | Field || Len | Type | UUID | EddyStone Frame |
448+
* | Offset || 0 | 1 | 2 | 4 |
449+
* | Len || 1 | 1 | 2 | up to 20 |
450+
* | Data || ?? | ?? | 0xAA | 0xFE | ??? |
446451
*/
447452
void BLEAdvertisementData::setServiceData(BLEUUID uuid, std::string data) {
448453
char cdata[2];

libraries/BLE/src/BLEEddystoneTLM.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
#include "BLEEddystoneTLM.h"
1818

1919
static const char LOG_TAG[] = "BLEEddystoneTLM";
20-
#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8))
21-
#define ENDIAN_CHANGE_U32(x) ((((x)&0xFF000000)>>24) + (((x)&0x00FF0000)>>8)) + ((((x)&0xFF00)<<8) + (((x)&0xFF)<<24))
22-
#define EDDYSTONE_TEMP_U16_TO_FLOAT(tempU16) (((int16_t)ENDIAN_CHANGE_U16(tempU16)) / 256.0f)
23-
#define EDDYSTONE_TEMP_FLOAT_TO_U16(tempFloat) (ENDIAN_CHANGE_U16(((int)(tempFloat * 256))))
2420

2521
BLEEddystoneTLM::BLEEddystoneTLM() {
2622
beaconUUID = 0xFEAA;
@@ -48,10 +44,14 @@ uint16_t BLEEddystoneTLM::getVolt() {
4844
return ENDIAN_CHANGE_U16(m_eddystoneData.volt);
4945
} // getVolt
5046

51-
float BLEEddystoneTLM::getTemp() {
52-
return ENDIAN_CHANGE_U16(m_eddystoneData.temp) / 256.0f;
47+
uint16_t BLEEddystoneTLM::getTemp() {
48+
return ENDIAN_CHANGE_U16(m_eddystoneData.temp);
5349
} // getTemp
5450

51+
float BLEEddystoneTLM::getFloatTemp() {
52+
return EDDYSTONE_TEMP_U16_TO_FLOAT(ENDIAN_CHANGE_U16(m_eddystoneData.temp));
53+
} // getFloatTemp
54+
5555
uint32_t BLEEddystoneTLM::getCount() {
5656
return ENDIAN_CHANGE_U32(m_eddystoneData.advCount);
5757
} // getCount
@@ -112,6 +112,9 @@ std::string BLEEddystoneTLM::toString() {
112112

113113
/**
114114
* Set the raw data for the beacon record.
115+
* Example:
116+
* uint8_t *payLoad = advertisedDevice.getPayload();
117+
* eddystoneTLM.setData(std::string((char*)payLoad+22, advertisedDevice.getPayloadLength() - 22));
115118
*/
116119
void BLEEddystoneTLM::setData(std::string data) {
117120
if (data.length() != sizeof(m_eddystoneData)) {
@@ -134,7 +137,7 @@ void BLEEddystoneTLM::setVolt(uint16_t volt) {
134137
} // setVolt
135138

136139
void BLEEddystoneTLM::setTemp(float temp) {
137-
m_eddystoneData.temp = (uint16_t)temp;
140+
m_eddystoneData.temp = EDDYSTONE_TEMP_FLOAT_TO_U16(temp);
138141
} // setTemp
139142

140143
void BLEEddystoneTLM::setCount(uint32_t advCount) {

libraries/BLE/src/BLEEddystoneTLM.h

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#include "BLEUUID.h"
1111

1212
#define EDDYSTONE_TLM_FRAME_TYPE 0x20
13+
#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8))
14+
#define ENDIAN_CHANGE_U32(x) ((((x)&0xFF000000)>>24) + (((x)&0x00FF0000)>>8)) + ((((x)&0xFF00)<<8) + (((x)&0xFF)<<24))
15+
#define EDDYSTONE_TEMP_U16_TO_FLOAT(tempU16) (((int16_t)ENDIAN_CHANGE_U16(tempU16)) / 256.0f)
16+
#define EDDYSTONE_TEMP_FLOAT_TO_U16(tempFloat) (ENDIAN_CHANGE_U16(((int)((tempFloat) * 256))))
1317

1418
/**
1519
* @brief Representation of a beacon.
@@ -18,33 +22,34 @@
1822
*/
1923
class BLEEddystoneTLM {
2024
public:
21-
BLEEddystoneTLM();
22-
std::string getData();
23-
BLEUUID getUUID();
24-
uint8_t getVersion();
25-
uint16_t getVolt();
26-
float getTemp();
27-
uint32_t getCount();
28-
uint32_t getTime();
29-
std::string toString();
30-
void setData(std::string data);
31-
void setUUID(BLEUUID l_uuid);
32-
void setVersion(uint8_t version);
33-
void setVolt(uint16_t volt);
34-
void setTemp(float temp);
35-
void setCount(uint32_t advCount);
36-
void setTime(uint32_t tmil);
25+
BLEEddystoneTLM();
26+
std::string getData();
27+
BLEUUID getUUID();
28+
uint8_t getVersion();
29+
uint16_t getVolt();
30+
uint16_t getTemp();
31+
float getFloatTemp();
32+
uint32_t getCount();
33+
uint32_t getTime();
34+
std::string toString();
35+
void setData(std::string data);
36+
void setUUID(BLEUUID l_uuid);
37+
void setVersion(uint8_t version);
38+
void setVolt(uint16_t volt);
39+
void setTemp(float temp);
40+
void setCount(uint32_t advCount);
41+
void setTime(uint32_t tmil);
3742

3843
private:
39-
uint16_t beaconUUID;
40-
struct {
41-
uint8_t frameType;
42-
uint8_t version;
43-
uint16_t volt;
44-
uint16_t temp;
45-
uint32_t advCount;
46-
uint32_t tmil;
47-
} __attribute__((packed)) m_eddystoneData;
44+
uint16_t beaconUUID;
45+
struct {
46+
uint8_t frameType;
47+
uint8_t version;
48+
uint16_t volt;
49+
uint16_t temp;
50+
uint32_t advCount;
51+
uint32_t tmil;
52+
} __attribute__((packed)) m_eddystoneData;
4853

4954
}; // BLEEddystoneTLM
5055

0 commit comments

Comments
 (0)