ble mesh setting publication address and subcription address

ask1432
Posts: 5
Joined: Mon Apr 19, 2021 11:39 am
Contact:

ble mesh setting publication address and subcription address

Postby ask1432 » Thu Jun 24, 2021 6:50 am

I am working on a sensor model and want to receive data from multiple nodes with periodic intervals is it possible.?? also how to set publication and subscription addresses for that. in the sensor example, the client receives data from only one node which is lastly provisioned .

kskd1804
Posts: 5
Joined: Fri Jul 23, 2021 4:28 am

Re: ble mesh setting publication address and subcription address

Postby kskd1804 » Fri Jul 23, 2021 9:04 am

I had the same problem earlier. You can solve it by following below steps.

1. In sensor client code, place the below code within switch block of example_ble_mesh_provisioning_cb in the case block of ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT.

Code: Select all

case ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT:
        ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT, err_code %d", param->provisioner_bind_app_key_to_model_comp.err_code);
		esp_err_t err = esp_ble_mesh_model_subscribe_group_addr(PROV_OWN_ADDR, ESP_BLE_MESH_CID_NVAL, ESP_BLE_MESH_MODEL_ID_SENSOR_CLI, SUBSCRIPTION_ADDR);
		if (err != ESP_OK) {
			ESP_LOGE(TAG, "Failed to subscribe to group 0x%04x", SUBSCRIPTION_ADDR);
		}
        break;
This will make the local model subscribe to SUBSCRIPTION_ADDR. SUBSCRIPTION_ADDR must be a value between 0xC000 - 0xFFEF.

2. In sensor client code, add another case block for ESP_BLE_MESH_MODEL_SUBSCRIBE_GROUP_ADDR_COMP_EVT in example_ble_mesh_provisioning_cb as follows:

Code: Select all

case ESP_BLE_MESH_MODEL_SUBSCRIBE_GROUP_ADDR_COMP_EVT:
		ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_SUBSCRIBE_GROUP_ADDR_COMP_EVT, err_code %d", param->model_sub_group_addr_comp.err_code);
		break;
3. Next, set the publication address for Sensor Server model using Configuration Client model in the sensor client code. This can be done using below code. Place it before ESP_LOGW(TAG, "Provision and config successfully"); statement.

Code: Select all

param->status_cb.model_app_status.company_id == ESP_BLE_MESH_CID_NVAL) {
				example_ble_mesh_set_msg_common(&common, node, config_client.model, ESP_BLE_MESH_MODEL_OP_MODEL_PUB_SET);
				set.model_pub_set.element_addr = node->unicast_addr;
				set.model_pub_set.publish_addr = SUBSCRIPTION_ADDR;
				set.model_pub_set.publish_app_idx = prov_key.app_idx;
				set.model_pub_set.publish_ttl = 7;
				set.model_pub_set.model_id = ESP_BLE_MESH_MODEL_ID_SENSOR_SRV;
				set.model_pub_set.company_id = ESP_BLE_MESH_CID_NVAL;
				err = esp_ble_mesh_config_client_set_state(&common, &set);
                if (err) {
                    ESP_LOGE(TAG, "Failed to send Config Model Pub Set");
                    return;
                }
                wait_model_id = ESP_BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV;
                wait_cid = ESP_BLE_MESH_CID_NVAL;
                ESP_LOGW(TAG, "Provision and config successfully");
With this the code changes in sensor_client file are done. Now we can proceed to the sensor_server code and make the necessary changes.

4. In the example_ble_mesh_config_server_cb of sensor_server code, handle the ESP_BLE_MESH_MODEL_OP_MODEL_PUB_SET event, which will set the Publication address on the Senor Server model (sent by Configuration Client model in sensor_client code).

Code: Select all

case ESP_BLE_MESH_MODEL_OP_MODEL_PUB_SET:
            ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_MODEL_PUB_SET");
            ESP_LOGI(TAG, "elem_addr 0x%04x, pub_addr 0x%04x, cid 0x%04x, mod_id 0x%04x",
                param->value.state_change.mod_pub_set.element_addr,
                param->value.state_change.mod_pub_set.pub_addr,
                param->value.state_change.mod_pub_set.company_id,
                param->value.state_change.mod_pub_set.model_id);
            break;
5. Finally, call the esp_ble_mesh_model_publish to publish the data.

Hope this works for you as well. Cheers!

Who is online

Users browsing this forum: No registered users and 149 guests