This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Update advertising packet with battery level in nrf52832

Dear Sir,

I am using nrf52832.

SDK 15

soft device s132.

I need to add the battery level also to the advertising packet.

Currently I have done code to read the battery level using SAADC.

/**@brief Function for handling the ADC interrupt.
 *
 * @details  This function will fetch the conversion result from the ADC, convert the value into
 *           percentage and send it to peer.
 */
void saadc_event_handler(nrf_drv_saadc_evt_t const * p_event)
{
    if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
    {
        nrf_saadc_value_t adc_result;
        uint16_t          batt_lvl_in_milli_volts;
        uint8_t           percentage_batt_lvl;
        uint32_t          err_code;

        adc_result = p_event->data.done.p_buffer[0];

        err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, 1);
        APP_ERROR_CHECK(err_code);

        batt_lvl_in_milli_volts = ADC_RESULT_IN_MILLI_VOLTS(adc_result) +
                                  DIODE_FWD_VOLT_DROP_MILLIVOLTS;
NRF_LOG_INFO("saadc_event_handler = %d  \n",batt_lvl_in_milli_volts);
        percentage_batt_lvl = battery_level_in_percent(batt_lvl_in_milli_volts);
NRF_LOG_INFO("saadc_event_handler = %d  \n",percentage_batt_lvl);
        err_code = ble_bas_battery_level_update(&m_bas, percentage_batt_lvl, BLE_CONN_HANDLE_ALL);
        NRF_LOG_INFO("\n err_code = %d  \n",err_code);
        if ((err_code != NRF_SUCCESS) &&
            (err_code != NRF_ERROR_INVALID_STATE) &&
            (err_code != NRF_ERROR_RESOURCES) &&
            (err_code != NRF_ERROR_BUSY) &&
            (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
           )
        {
            APP_ERROR_HANDLER(err_code);
        }
    }
}




/**@brief Function for configuring ADC to do battery level conversion.
 */
static void adc_configure(void)
{
    

    nrf_saadc_channel_config_t config =
        NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD);

        ret_code_t err_code = nrf_drv_saadc_init(NULL, saadc_event_handler);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_channel_init(0, &config);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_buffer_convert(&adc_buf[0], 1);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_buffer_convert(&adc_buf[1], 1);
    APP_ERROR_CHECK(err_code);
}

And I  have used a timer to update the battery level each interval.The timeout handler is implemnted as follows


/**@brief Function for handling the Battery measurement timer timeout.
 *
 * @details This function will be called each time the battery level measurement timer expires.
 *          This function will start the ADC.
 *
 * @param[in] p_context   Pointer used for passing some arbitrary information (context) from the
 *                        app_start_timer() call to the timeout handler.
 */
static void battery_level_meas_timeout_handler(void * p_context)
{
    UNUSED_PARAMETER(p_context);

    ret_code_t err_code;
    NRF_LOG_INFO("Battery \n");
    err_code = nrf_drv_saadc_sample();
    APP_ERROR_CHECK(err_code);
}



Now I need to add this information in to advertising packet.

Can anybody help me and tell me how to do the same?

How can I see this battery value (which is sent from the device ) using some App ( like  nRFConnect/LE Scanner) in the phone?

Parents
  • In my application I add so called "Manufacturer Specific Data" to the advertising packet to store status information like the battery level. You can add this data during initialization of the advertising:

    ble_advertising_init_t init;
    memset(&init, 0, sizeof(init));
    
    ble_advdata_manuf_data_t manuf_data;    //Variable to hold manufacturer specific data
    manuf_data.company_identifier = 0xFFFF; // See https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers
    manuf_data.data.p_data        = &m_adv_data[APP_ADV_STATUS_IDX];
    manuf_data.data.size          = 6; // Or whatever size your data has.
    init.advdata.p_manuf_specific_data = &manuf_data;

    You can then update the advertising data by manipulating the manufacturing data:

    ret_code_t err_code = sd_ble_gap_adv_data_set( m_adv_data, sizeof(m_adv_data), NULL, 0 );
    

  • Hi, Thank you for your response.

    But is this function"sd_ble_gap_adv_data_set()"  available in SDK15?..

    I am a little confused about the locatin at which the functions to be called..

    If possible please provide the main.c file for the same.

    Thanking you in advance.

    with regards,

    Geetha

Reply Children
Related