Page 1 of 1

Need help using BLE with esp-idf

Posted: Sat Apr 28, 2018 8:51 pm
by MickPF
Hello,

My goal: An "Adafruit ESP32 Feather" should send some sensor data (several different values) in short messages (one message per sensor value) directly behind each other to a BLE server and then go into sleep mode for a certain time.
I use both examples "esp-idf/examples/bluetooth/gatt_server" and "esp-idf/examples/bluetooth/ gatt_client" as a starting point for my goal. I use the latest version of esp-idf (v 3.0).
It (message transfer) works as desired as long as I do not insert the code for the sleep mode at the end of the client's "app_main". As soon as I activate the code for sleeping, no data will be sent.

The reason: The callback function "gattc_profile_event_handler" is executed completely asynchronously to the rest of the code and the CPU changes into sleep mode before the messages are sent. After running the code for the event "ESP_GATTC_WRITE_CHAR_EVT" ( one event call per message), no further events are generated.

I've tried to call "esp_ble_gattc_write_char" in "gattc_profile_event_handler" executing code for the event "ESP_GATTC_WRITE_DESCR_EVT" (as in the original example "gatt_client") and to call the same function ("esp_ble_gattc_write_char") from "app_main", but both variants lead to the same result!

Calling "esp_ble_gattc_close" before changing into the sleep mode cause the following error messages and my messages are not sent:

Code: Select all

E (1634) BT: bta_gattc_cmpl_cback unknown conn_id =  3, ignore data
E (1635) BT: bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x0016
The code line leading to these error messages is

Code: Select all

        esp_ble_gattc_close(gattc_if, conn_id);
Interestingly, the field "gattc_if" has the value "3" and "conn_id" has the value "0" and are valid before calling the function "esp_ble_gattc_close". Somewhere in the code of "esp-idf" both arguments would be swapped!

I have read various documents about BLE on the internet, but can not find any solution to my problem.

Can somebody help me to find a solution please?

Thanks in advance,
Michael

Re: Need help using BLE with esp-idf

Posted: Sat Apr 28, 2018 9:01 pm
by chegewara
As far as i know ble is not working with sleep mode, not yet implemented.

This line looks like error but its not an error, its just debug message which is saying that device got disconnected (reason=0x0016), its only reason code i know :(

Code: Select all

E (1635) BT: bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x0016

Re: Need help using BLE with esp-idf

Posted: Sun Apr 29, 2018 7:49 am
by MickPF
Hello chegewara,

The problem is not that the CPU goes into sleep mode, but that I do not know how to correctly close/finish communication before anything else can happen.
As I described before, calling "esp_ble_gattc_close" closes the connection abrupt before the messages are really sent by the event handler.
I've tried to synchronize both processes ("app_main" and the event handler) by using the xQueue, but also without success.

Kind Regards,
Michael

Re: Need help using BLE with esp-idf

Posted: Mon Apr 30, 2018 8:33 am
by MickPF
Hello chegewara,

1. I do not understand why you think the message

Code: Select all

E (1634) BT: bta_gattc_cmpl_cback unknown conn_id =  3, ignore data
E (1635) BT: bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x0016
is not an error message. The statement is very clear: "unknown conn_id = 3".

2. I found a solution to synchronize the BLE communication myself:
In "gattc_event_handler"

Code: Select all

        case ESP_GATTC_WRITE_DESCR_EVT :
            ready = true;
            xQueueSend(msg_queue, &ready, 0);
            break;
        case ESP_GATTC_EXEC_EVT :
            ready = true;
            xQueueSend(msg_queue, &ready, 0);
            break;
In "app_main"

Code: Select all

    xQueueReceive(msg_queue, &ready, 10 * portTICK_PERIOD_MS);
    /* Send message(s) calling function 'esp_ble_gattc_write_char' to the server here */
    esp_ble_gattc_execute_write(gattc_profile.gattc_if, gattc_profile.conn_id, true);
    xQueueReceive(msg_queue, &ready, 10 * portTICK_PERIOD_MS);
    
The event "ESP_GATTC_WRITE_DESCR_EVT" occurs only one time before any data can be sent.
The call to 'esp_ble_gattc_execute_write' cause the event "ESP_GATTC_EXEC_EVT" and it makes sense in my opinion...

Kind Regards,
Michael

Re: Need help using BLE with esp-idf

Posted: Mon Apr 30, 2018 9:57 am
by chegewara
I said this is not error:

Code: Select all

E (1635) BT: bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x0016
But you solved the issue, thats good.
Have a nice day.

Re: Need help using BLE with esp-idf

Posted: Mon Apr 30, 2018 12:05 pm
by MickPF
Hello chegewara,

where can I find your solution?

Re: Need help using BLE with esp-idf

Posted: Mon Apr 30, 2018 7:16 pm
by chegewara
Im having this "error" message in server application. It is complaining about gattc connect callback:

Code: Select all

E (76370) BT: bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x0015
Because it cant be changed in arduino-ide, but ive checked gatt_server example and this should be done in menuconfig:
- in gatt_server example turn off GATTC and SMP if you dont use it
- in gatt_client example turn off GATTS and SMP if you dont use it

Check also this:

https://github.com/espressif/esp-idf/bl ... _act.c#L49