Add battery level to the BLE advertisement packet
This commit is contained in:
parent
9c2d95569d
commit
321e70a188
3 changed files with 39 additions and 15 deletions
|
|
@ -1,11 +1,27 @@
|
|||
#include "ble_advertisement_data.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace parasite {
|
||||
|
||||
void BLEAdvertisementData::SetRawSoilMoisture(int raw_soil_moisture) {
|
||||
uint16_t packed_value = raw_soil_moisture;
|
||||
void BLEAdvertisementData::SetSoilMoistureRaw(int soil_moisture_raw) {
|
||||
uint16_t packed_value = soil_moisture_raw;
|
||||
data_[kRawSoilMoistureOffset] = packed_value >> 8;
|
||||
data_[kRawSoilMoistureOffset + 1] = packed_value & 0xff;
|
||||
}
|
||||
|
||||
void BLEAdvertisementData::SetSoilMoisturePercent(
|
||||
double soil_moisture_percent) {
|
||||
// TODO: 1.0 * MAX is too close to overflow for comfort?
|
||||
uint16_t packed_value = ((1 << 16) - 1) * soil_moisture_percent;
|
||||
data_[kPercentSoilMoistureOffset] = packed_value >> 8;
|
||||
data_[kPercentSoilMoistureOffset + 1] = packed_value & 0xff;
|
||||
}
|
||||
|
||||
void BLEAdvertisementData::SetBatteryVoltage(double battery_voltage) {
|
||||
uint16_t packed_value = 1000 * battery_voltage;
|
||||
data_[kBatteryVoltageOffset] = packed_value >> 8;
|
||||
data_[kBatteryVoltageOffset + 1] = packed_value & 0xff;
|
||||
}
|
||||
|
||||
} // namespace parasite
|
||||
|
|
@ -8,20 +8,22 @@
|
|||
|
||||
namespace parasite {
|
||||
|
||||
constexpr size_t kAdvertisementDataLen = 18;
|
||||
constexpr size_t kAdvertisementDataLen = 21;
|
||||
constexpr size_t kRawSoilMoistureOffset = 14;
|
||||
constexpr size_t kPercentSoilMoistureOffset = kRawSoilMoistureOffset + 2;
|
||||
constexpr size_t kTemperatureOfsset = kRawSoilMoistureOffset + 4;
|
||||
constexpr size_t kBatteryVoltageOffset = kRawSoilMoistureOffset + 4;
|
||||
|
||||
class BLEAdvertisementData {
|
||||
public:
|
||||
void SetRawSoilMoisture(int raw_soil_moisture);
|
||||
void SetSoilMoistureRaw(int soil_moisture_raw);
|
||||
void SetSoilMoisturePercent(double soil_moisture_percent);
|
||||
void SetBatteryVoltage(double battery_voltage);
|
||||
const uint8_t* GetRawData() const { return data_; }
|
||||
size_t GetDataLen() const { return kAdvertisementDataLen; }
|
||||
|
||||
private:
|
||||
uint8_t data_[kAdvertisementDataLen] = {
|
||||
9, // Length of name.
|
||||
9, // Length of name + data type.
|
||||
BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME,
|
||||
'P',
|
||||
'a',
|
||||
|
|
@ -31,14 +33,17 @@ class BLEAdvertisementData {
|
|||
'i',
|
||||
't',
|
||||
'e',
|
||||
7, // Length of the service data.
|
||||
10, // Length of the service data + data type.
|
||||
BLE_GAP_AD_TYPE_SERVICE_DATA,
|
||||
0x1a,
|
||||
0x18, // Environment sensor service UUID.
|
||||
0x18, // Environment sensor service UUID (0x181a).
|
||||
0x00,
|
||||
0x00, // Raw soil humidity.
|
||||
0x00, // Raw soil humidity (2 bytes).
|
||||
0x00,
|
||||
0x00, // Percentage soil humidity (2 bytes).
|
||||
0x00,
|
||||
0x00, // Battery voltage (2 bytes representing millivolts).
|
||||
0x00,
|
||||
0x00, // Percentage soil humidity.
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -25,10 +25,13 @@ parasite::SoilMonitor soil_monitor(kSoilMonitorAirVal, kSoilMonitorWaterVal,
|
|||
kSoilAnalogPin);
|
||||
|
||||
void updateAdvertisingData(parasite::BLEAdvertiser* advertiser,
|
||||
const parasite::soil_reading_t& soil_reading) {
|
||||
const parasite::soil_reading_t& soil_reading,
|
||||
double battery_voltage) {
|
||||
parasite::BLEAdvertisementData data;
|
||||
|
||||
data.SetRawSoilMoisture(soil_reading.raw);
|
||||
data.SetSoilMoistureRaw(soil_reading.raw);
|
||||
data.SetSoilMoisturePercent(soil_reading.parcent);
|
||||
data.SetBatteryVoltage(battery_voltage);
|
||||
|
||||
advertiser->SetData(data);
|
||||
|
||||
|
|
@ -51,8 +54,8 @@ void setup() {
|
|||
}
|
||||
|
||||
void loop() {
|
||||
double batt_voltage = batt_monitor.Read();
|
||||
Serial.printf("Batt voltage: %f\n", batt_voltage);
|
||||
double battery_voltage = batt_monitor.Read();
|
||||
Serial.printf("Batt voltage: %f\n", battery_voltage);
|
||||
|
||||
parasite::soil_reading_t soil_reading = soil_monitor.Read();
|
||||
Serial.printf("Moisture val: %d, %f%%\n", soil_reading.raw,
|
||||
|
|
@ -60,7 +63,7 @@ void loop() {
|
|||
|
||||
digitalToggle(kLED1Pin);
|
||||
|
||||
updateAdvertisingData(&advertiser, soil_reading);
|
||||
updateAdvertisingData(&advertiser, soil_reading, battery_voltage);
|
||||
|
||||
delay(500);
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue