Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report battery percentage in BLE BTHOME_V2 protocol #188

Merged
merged 2 commits into from
Apr 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions code/nrf-connect/samples/ble/src/encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,33 +104,36 @@ int prst_ble_encode_service_data(const prst_sensors_t* sensors,
out[4] = temp_val & 0xff;
out[5] = temp_val >> 8;
// Humidity.
out[6] = 0x03;
// Value. Factor 0.01, over 100%.
uint16_t humi_val = 10000 * sensors->shtc3.rel_humi;
out[7] = humi_val & 0xff;
out[8] = humi_val >> 8;
out[6] = 0x2E;
// Value. Factor 1 over 100%.
uint8_t humi_val = 100 * sensors->shtc3.rel_humi + 0.5f;
out[7] = humi_val;
// Illuminance.
out[9] = 0x05;
out[8] = 0x05;
// Value. Factor of 0.01.
uint32_t lux_val = sensors->photo.brightness * 100;
out[10] = lux_val & 0xff;
out[11] = (lux_val >> 8) & 0xff;
out[12] = (lux_val >> 16) & 0xff;
out[9] = lux_val & 0xff;
out[10] = (lux_val >> 8) & 0xff;
out[11] = (lux_val >> 16) & 0xff;
// Battery voltage.
out[13] = 0x0c;
out[12] = 0x0c;
// Value. Factor of 0.001.
uint16_t batt_val = sensors->batt.adc_read.millivolts;
out[14] = batt_val & 0xff;
out[15] = batt_val >> 8;
out[13] = batt_val & 0xff;
out[14] = batt_val >> 8;
// Soil moisture.
out[16] = 0x14;
// Factor of 0.01, so we need to multiply our the value in 100% by 1/0.01 = 100.
uint16_t soil_val = 10000 * sensors->soil.percentage;
out[17] = soil_val & 0xff;
out[18] = soil_val >> 8;
out[15] = 0x2F;
// Factor of 1 over 100%
uint8_t soil_val = 100 * sensors->soil.percentage + 0.5f;
out[16] = soil_val;
// Battery percentage.
out[17] = 0x01;
// Value. Factor 1 over 100%
uint8_t batt_percentage_val = 100 * sensors->batt.percentage + 0.5f;
out[18] = batt_percentage_val;

#endif // Encoding protocols

LOG_HEXDUMP_DBG(out, out_len, "Encoded BLE adv: ");
return 0;
}
}
Loading