ble communication client to multiple server

asmaadou
Posts: 8
Joined: Wed Apr 07, 2021 11:48 am

ble communication client to multiple server

Postby asmaadou » Thu Apr 15, 2021 10:50 am

hi all

I'm trying to develop a bluetooth network with one client and several servers, but they communicate with the client based on "notify". each server is connected to a sensor (DHT11).
i am able to connect to one server and get the temp characteristic.

i need a code for one esp32 client connecting to multiple ESP32 server .

I'm using the BLE examples

leogs01
Posts: 9
Joined: Wed Jun 24, 2020 6:53 pm

Re: ble communication client to multiple server

Postby leogs01 » Thu Apr 15, 2021 1:10 pm

There should be callbacks(EVT) that you can refer to in your client code in which you would choose to continue scanning or advertising based on whether the client is connecting to the servers or if the servers are connecting to your client and if you've hit your max number of connections. For example, if the number of connections I have currently on my client is 5 but my max connections is 7, I would continue scanning for additional servers to connect to.

Its one of the callbacks below.

Sorry, I can't remember which callback/evt is called after the connection setup is finished which is where you'd determine to continue scanning or stop.

Code: Select all

    
    case ESP_GATTC_CONNECT_EVT:{
        ESP_LOGI(GATTC_TAG, "ESP_GATTC_CONNECT_EVT conn_id %d, if %d", p_data->connect.conn_id, gattc_if);
        gl_profile_tab[PROFILE_A_APP_ID].conn_id = p_data->connect.conn_id;
        memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, p_data->connect.remote_bda, sizeof(esp_bd_addr_t));
        ESP_LOGI(GATTC_TAG, "REMOTE BDA:");
        esp_log_buffer_hex(GATTC_TAG, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, sizeof(esp_bd_addr_t));
        esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, p_data->connect.conn_id);
        if (mtu_ret){
            ESP_LOGE(GATTC_TAG, "config MTU error, error code = %x", mtu_ret);
        }
        break;
    }
    case ESP_GATTC_OPEN_EVT:
        if (param->open.status != ESP_GATT_OK){
            ESP_LOGE(GATTC_TAG, "open failed, status %d", p_data->open.status);
            break;
        }
        ESP_LOGI(GATTC_TAG, "open success");
        break;
     

asmaadou
Posts: 8
Joined: Wed Apr 07, 2021 11:48 am

Re: ble communication client to multiple server

Postby asmaadou » Thu Apr 15, 2021 4:10 pm

thank for your reply ,but to be honest i did not understand the code you mentioned , i am using the basic codes already included in arduino examples written by Neil Kolban and updated by @chegewara

leogs01
Posts: 9
Joined: Wed Jun 24, 2020 6:53 pm

Re: ble communication client to multiple server

Postby leogs01 » Fri Apr 16, 2021 12:54 pm

you would determine whether to continue scanning and connecting in your onConnect. It seems like in the example you would do that by setting/clearing flags they have in place instead of directly calling the scanning function from the onConnect function.

The onConnect callback should be called every time a new connection is made.

also, if you have multiple connections, it's important to keep track of them so you know who you're receiving what from.

Also, normally when it comes to ble, the max number of connections you can have is 7.

Code: Select all

class MyClientCallback : public BLEClientCallbacks {
  void onConnect(BLEClient* pclient) {
  }

  void onDisconnect(BLEClient* pclient) {
    connected = false;
    Serial.println("onDisconnect");
  }
};

Who is online

Users browsing this forum: No registered users and 57 guests