Skip to content

Commit

Permalink
Merge pull request #188 from jrhbcn/main
Browse files Browse the repository at this point in the history
Report battery percentage in BLE BTHOME_V2 protocol
  • Loading branch information
rbaron authored Apr 20, 2024
2 parents e62521d + dc02480 commit e7e4b77
Showing 1 changed file with 21 additions and 18 deletions.
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;
}
}

0 comments on commit e7e4b77

Please sign in to comment.