Page 1 of 2

BLE Scan and obtain RSSI

Posted: Tue Mar 07, 2017 10:05 am
by JordC1995
Hi all,

I'm working on my master's thesis, and using the ESP32 as one of the main hardware components of my research project.

I've been trying to program the ESP to scan for nearby BLE devices check the MAC address and if it matches another address in a list,
return the RSSI value of the BLE device.

What API can be used to complete this particular task?
Also, how can this be implemented in code, I've been searching for examples and haven't had much luck with my understanding of it all.

Best wishes,

Jordan

Re: BLE Scan and obtain RSSI

Posted: Tue Mar 07, 2017 11:41 am
by rene007
Start by looking here.

https://github.com/espressif/esp-idf/bl ... ttc_demo.c

Use the espressif SDK to flash the code in the link above into the chip.

The key thing is to set your ESP32 as a GATT client in order to scan for nearby BLE devices.

Re: BLE Scan and obtain RSSI

Posted: Wed Mar 08, 2017 7:37 am
by torntrousers
Some (ESP32/Arduino) code is here showing a scan and getting the mac address and RSSI. Described in the article here.

Re: BLE Scan and obtain RSSI

Posted: Wed Mar 08, 2017 4:42 pm
by f.h-f.s.
in the "esp_gap_cb" function, case: "ESP_GAP_BLE_SCAN_RESULT_EVT" the param argument is cast to scan_result(in the example). This struct contains all the information you need.

From esp_gap_ble_api.h:

Code: Select all

    struct ble_scan_result_evt_param {
        esp_gap_search_evt_t search_evt;			/*!< Search event type */
        esp_bd_addr_t bda;							/*!< Bluetooth device address which has been searched */
        esp_bt_dev_type_t dev_type;					/*!< Device type */
        esp_ble_addr_type_t ble_addr_type;			/*!< Ble device address type */
		esp_ble_evt_type_t ble_evt_type;			/*!< Ble scan result event type */
        int rssi;									/*!< Searched device's RSSI */
        uint8_t  ble_adv[ESP_BLE_ADV_DATA_LEN_MAX]; /*!< Received EIR */
        int flag;									/*!< Advertising data flag bit */
        int num_resps;								/*!< Scan result number */
    } scan_rst;										/*!< Event parameter of ESP_GAP_BLE_SCAN_RESULT_EVT */
scan_result->scan_rst.bda is the address (uint8_t array, length 6)
scan_result->scan_rst.ble_adv is the advertisement (uint8_t array, length 31)
scan_result->scan_rst.rssi would be the rssi (int)

The RSSI is not very stable though, i've measured -19 ~ -59 for a distance less than a meter. http://pastebin.com/fHrya27z

Re: BLE Scan and obtain RSSI

Posted: Wed Mar 08, 2017 5:18 pm
by JordC1995
Hi,

Thanks for all the very useful responses from rene007, torntrousers and f.h.f.s.
You've all been of great help to me.

A little update on what I did:

I read the article posted by torntrousers, which was very useful indeed. Step by step guide detailing how to obtain the RSSI and MAC address. The code provided by f.h.f.s has aided my understanding of how this process works and furthermore, I installed the Espressif SDK as mentioned by rene007.

I now have the ESP32 running and it's sampling RSSI values of nearby BLE advertising nodes successfully.

However, one slight problem as highlighted by f.h.f.s, the values are very fluctuant, even when the advertising node is very close to the ESP32. I'm unsure about why this is, but I assume it's a hardware limitation :geek: .

Once again, thanks for all your help & advice.

Best wishes,
Jordan

Re: BLE Scan and obtain RSSI

Posted: Wed Mar 08, 2017 8:00 pm
by madscientist_42
Much of the GATT Client code is not needed if you're just doing a Scanner.

The pieces are all there, and you only need to keep it running (Restart the scan with the timeout on the completion event on the GAP callback) and clear out the stop and GATT search, etc. code out of the GAP scan result event code. As mentioned, RSSI is in the event result structure.

Re: BLE Scan and obtain RSSI

Posted: Fri Apr 21, 2017 7:35 pm
by neilyoung
Don't trust the RSSI values, it is mostly complete nonsense

Re: BLE Scan and obtain RSSI

Posted: Mon May 08, 2017 6:30 pm
by petcovici
Hello! I work in a similar project, so I need to collect MAC data from multiple readers along a road corridor.
In some point I don't have WI-fi signal, so I need to write data to a SD Card.
Do you think it's possible to add a time stamp to my ESP32 Mac addresses?
Do you think this project it's feasible with ESP 32 and Arduino IDE ?
Where should I start searching for more information?

Thank you

Re: BLE Scan and obtain RSSI

Posted: Mon May 08, 2017 7:12 pm
by madscientist_42
neilyoung wrote:Don't trust the RSSI values, it is mostly complete nonsense
It is? What basis do you have for this? Did you do the requisite analysis with precision equipment and arrived at this? If not...how can you claim it's nonsense?

Re: BLE Scan and obtain RSSI

Posted: Mon May 08, 2017 7:48 pm
by WiFive
madscientist_42 wrote:
neilyoung wrote:Don't trust the RSSI values, it is mostly complete nonsense
It is? What basis do you have for this? Did you do the requisite analysis with precision equipment and arrived at this? If not...how can you claim it's nonsense?
https://github.com/espressif/esp-idf/is ... -287391770