How a BLE Server can get the RSSI of received packet from Central Device

sideridispas
Posts: 5
Joined: Fri Oct 06, 2017 1:06 pm

How a BLE Server can get the RSSI of received packet from Central Device

Postby sideridispas » Fri Oct 06, 2017 1:40 pm

Hello everyone!
I am working on my first BLE project and hope to me it through using the awesome ESP-32 module! :D
I based my work/experiments on the example code of UART implementation by pcbreflux, which can be found in this github.

The basic information I think is important is given below.

Server: ESP-32 module (Huzzah32 board from Adafruit)
Client: Android smartphone with nRF Connect and Toolbox apps

The project requires two characteristics as the UART example:
  • RX characteristic: here writes the Client and reads the Server
  • TX characteristic: here writes the Server and reads the Client
Everything seems to work as planned but the RSSI. More specific, I need to get the RSSI value from the latest packet the client sent in order to check its proximity. But the code I used in the previous experiments for the ESP client role does not give me similar numbers that make sense (eg it gives 1073560042 instead of -80ish or something). The part of the code that is responsible is given below:

Code: Select all

void char1_write_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) {
	//ESP_LOGI(GATTS_TAG, "char1_write_handler %d\n", param->write.handle);

	if (gl_char[0].char_val!=NULL) {
		//ESP_LOGI(GATTS_TAG, "char1_write_handler char_val %d\n",param->write.len);
		gl_char[0].char_val->attr_len = param->write.len;
		for (uint32_t pos=0;pos<param->write.len;pos++) {
			gl_char[0].char_val->attr_value[pos]=param->write.value[pos];
		}
		ESP_LOGI(TAG, "[CRYPTO_PS]char1_write_handler %.*s", gl_char[0].char_val->attr_len, (char*)gl_char[0].char_val->attr_value);
	}
	//ESP_LOGI(GATTS_TAG, "char1_write_handler esp_gatt_rsp_t\n");
	notify_gatts_if = gatts_if;
	notify_conn_id = param->write.conn_id;
    esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);

    if (strncmp((const char *)gl_char[0].char_val->attr_value,"LED ON",6)==0) {
    	gpio_set_level(LED_PIN,HIGH);
    } else if (strncmp((const char *)gl_char[0].char_val->attr_value,"LED OFF",7)==0) {
    	gpio_set_level(LED_PIN,LOW);
    } else if(strncmp((const char *)gl_char[0].char_val->attr_value,(const char *)private_Key,4)==0){

    	//Check RSSI for proximity
    	esp_ble_gap_cb_param_t *p = (esp_ble_gap_cb_param_t *)param;
    	printf("scan-RSSI: %d\n", p->scan_rst.rssi); //test #1 with data from scan_rst
    	printf("read-RSSI: %d\n", p->read_rssi_cmpl.rssi); //test #2 with data from read_rssi_cmpl

    	char2_notify_handle(gatts_if, param->write.conn_id);
    }

}

Excuse me for anything really stupid that you might just saw or for any useful information that I forgot to write.
Thank you!! :D 8-)

halfro
Posts: 18
Joined: Sat Jul 15, 2017 11:13 am

Re: How a BLE Server can get the RSSI of received packet from Central Device

Postby halfro » Sun Oct 08, 2017 9:38 am

Probably 1073560042 is in raw milliWatts in decimal, because if you convert it to dBm ths gives 90.308263386 dBm. And since RSSI values vary from 0 to -120, probably this is -90.308263386 dBm.

But one question I have is that the value -120dBm would fall outside the int range described in esp_gap_ble_api.h so I may be wrong.

sideridispas
Posts: 5
Joined: Fri Oct 06, 2017 1:06 pm

Re: How a BLE Server can get the RSSI of received packet from Central Device

Postby sideridispas » Mon Oct 09, 2017 6:56 am

@halfro
Thank you for the reply! :)

Although the -90dBm value may make sense I am not sure about the raw milliWatts form because there are a few code examples with the same commands but with direct printing of the correct form (such as -minus something dBm).. :?

I also noticed that the value does not change, so maybe I am doing something wrong with the data that I take the RSSI from??

arapostol
Posts: 1
Joined: Sun Oct 15, 2017 11:40 am

Re: How a BLE Server can get the RSSI of received packet from Central Device

Postby arapostol » Sun Oct 15, 2017 11:56 am

Hello paschalis,

I've done a little research for your question and it turns out that you have to call the GAP API in order to get the RSSI.

So, if you modify the GATT server example from ESP-IDF by adding a call to esp_ble_gap_read_rssi(param->read.bda) in the gatts_profile_a_event_handler() function based on the needed case (ESP_GATTS_READ_EVT or ESP_GATTS_WRITE_EVT), you can ask the RSSI from the underlying GAP profile.
Then, you only have to add the ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT case in the gap_event_handler() function to print or save the RSSI.

Hope this can help you,

Aris :)

sideridispas
Posts: 5
Joined: Fri Oct 06, 2017 1:06 pm

Re: How a BLE Server can get the RSSI of received packet from Central Device

Postby sideridispas » Tue Oct 17, 2017 1:08 pm

@arapostol SOLVED!
Aris thank you!

That did the trick and works like a charm :D

Paschalis

Who is online

Users browsing this forum: Baidu [Spider], MicroController and 218 guests