Messaging in ESP_BLE_MESH

ixtreme3
Posts: 7
Joined: Sat May 13, 2023 10:00 am

Messaging in ESP_BLE_MESH

Postby ixtreme3 » Sun May 14, 2023 2:22 pm

Hello everyone!

I would like to ask for help with my BLE Mesh project.

My task is to create a Mesh network with the following topology:

* There are about 15 devices that read information from some sensor and once every N seconds send data (just a number from 0 to 300) to the main device. These devices should consume little power.
* There is ONE master device/gateway that needs to receive data from all other devices on the network and forward those messages to my android app. The master device also has an unlimited power supply.
* There are Relay devices that help to deliver messages from devices with sensors to the main device. These devices are in fixed locations and have an unlimited power supply.

I decided to build on the Vendor Model from the repository https://github.com/espressif/esp-idf/tr ... ndor_model, because it also distinguishes between the main device - Client and slave devices - Server, and also allows you to automate the provisioning process.

The problem is that the Servers in the Vendor Model example send messages to the Client only in response to a message from that very master inside the example_ble_mesh_custom_model_cb(esp_ble_mesh_model_cb_event_t event, esp_ble_mesh_model_cb_param_t *param) callback:

Code: Select all

static void example_ble_mesh_custom_model_cb(esp_ble_mesh_model_cb_event_t event,
                                             esp_ble_mesh_model_cb_param_t *param)
{
    switch (event) {
    case ESP_BLE_MESH_MODEL_OPERATION_EVT:
        if (param->model_operation.opcode == ESP_BLE_MESH_VND_MODEL_OP_SEND) {
            uint16_t tid = *(uint16_t *)param->model_operation.msg;
            ESP_LOGI(TAG, "Recv 0x%06" PRIx32 ", tid 0x%04x", param->model_operation.opcode, tid);
            esp_err_t err = esp_ble_mesh_server_model_send_msg(&vnd_models[0],
                    param->model_operation.ctx, ESP_BLE_MESH_VND_MODEL_OP_STATUS,
                    sizeof(tid), (uint8_t *)&tid);
            if (err) {
                ESP_LOGE(TAG, "Failed to send message 0x%06x", ESP_BLE_MESH_VND_MODEL_OP_STATUS);
            }
        }
        break;
    case ESP_BLE_MESH_MODEL_SEND_COMP_EVT:
        if (param->model_send_comp.err_code) {
            ESP_LOGE(TAG, "Failed to send message 0x%06" PRIx32, param->model_send_comp.opcode);
            break;
        }
        ESP_LOGI(TAG, "Send 0x%06" PRIx32, param->model_send_comp.opcode);
        break;
    default:
        break;
    }
}
while I need Servers to be able to send messages to Clients whenever they want.

To do this, I tried to move esp_ble_mesh_server_model_send_msg() into a separate function that would be called every N seconds:

Code: Select all

static void send_pulse_data(void* arg) {
    // create message
    esp_err_t err = esp_ble_mesh_server_model_send_msg(&vnd_models[0],
            param->model_operation.ctx, ESP_BLE_MESH_VND_MODEL_OP_STATUS,
            sizeof(message), (uint8_t *)&message);
    if (err) {
        ESP_LOGE(TAG, "Failed to send message 0x%06x", ESP_BLE_MESH_VND_MODEL_OP_STATUS);
    }
}
, but I ran into the fact that in order to send a message to the Client, parameters such as param->model_operation.ctx are needed, which I can’t get anywhere except inside the example_ble_mesh_custom_model_cb callback.

I would like to ask for advice on choosing the right model, on how to send messages from the Servers to the Client for my requirements, on how to receive messages on the Client side and on what functions to use for this. Basically any implementation tips, code snippets or comments would be very helpful to me.

I would also be very grateful if you could answer my questions below:

How to make devices with sensors consume little power? Do I need to explicitly do something for this, or is it enough to disable Relay and send messages once in a limited amount of time?
Is it enough to change ESP_BLE_MESH_RELAY_DISABLED to ESP_BLE_MESH_RELAY_ENABLED to enable Relay?
Do I need to do provisioning every time I use the devices network?

lzc56_Win
Posts: 1
Joined: Thu Oct 24, 2024 5:09 am

Re: Messaging in ESP_BLE_MESH

Postby lzc56_Win » Thu Oct 24, 2024 5:12 am

do this
```
case ESP_BLE_MESH_MODEL_OPERATION_EVT:
if (param->model_operation.opcode == ESP_BLE_MESH_VND_MODEL_OP_SEND)
{
uint16_t tid = *(uint16_t *)param->model_operation.msg;
ESP_LOGI(TAG, "Recv 0x%06" PRIx32 ", tid 0x%04x", param->model_operation.opcode, tid);
memcpy(&my_ctx, param->model_operation.ctx, sizeof(esp_ble_mesh_msg_ctx_t));
esp_err_t err = esp_ble_mesh_server_model_send_msg(&vnd_models[0],
param->model_operation.ctx, ESP_BLE_MESH_VND_MODEL_OP_STATUS,
sizeof(tid), (uint8_t *)&tid);
if (err)
{
ESP_LOGE(TAG, "Failed to send message 0x%06x", ESP_BLE_MESH_VND_MODEL_OP_STATUS);
}
}
break;
```

Who is online

Users browsing this forum: No registered users and 161 guests