It didn't feel like I was getting anywhere, so I switched to using the vendor_client and vendor_server examples, as I was able to send a string using that example - still only in response to a message from the provisioner though. I tried pulling out the code from the callback that responds, but it still doesn't seem to be doing anything. Here's the working code that sends a string in response to a button push from the provisioner (note the line where I copy the details to test_ctx for future use):
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;
test_ctx = *param->model_operation.ctx;
ESP_LOGI(TAG, "Recv 0x%06x, tid 0x%04x", param->model_operation.opcode, tid);
char *mydata = "TEST STRING PASSING";
ESP_LOGI(TAG, "NetKey Index: 0x%06x", param->model_operation.ctx->net_idx);
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,
strlen(mydata)+1, (uint8_t *)mydata);
if (err) {
ESP_LOGE(TAG, "Failed to send message 0x%06x", ESP_BLE_MESH_VND_MODEL_OP_STATUS);
}
}
break;
And here's the code on the provisioner side of things that triggers the ESP_BLE_MESH_MODEL_OPERATION_EVT (runs when the button is pushed):
Code: Select all
void example_ble_mesh_send_vendor_message(bool resend)
{
esp_ble_mesh_msg_ctx_t ctx = {0};
uint32_t opcode;
esp_err_t err;
ctx.net_idx = prov_key.net_idx;
ctx.app_idx = prov_key.app_idx;
ctx.addr = store.server_addr;
ctx.send_ttl = MSG_SEND_TTL;
ctx.send_rel = MSG_SEND_REL;
opcode = ESP_BLE_MESH_VND_MODEL_OP_SEND;
if (resend == false) {
store.vnd_tid++;
}
err = esp_ble_mesh_client_model_send_msg(vendor_client.model, &ctx, opcode,
sizeof(store.vnd_tid), (uint8_t *)&store.vnd_tid, MSG_TIMEOUT, true, MSG_ROLE);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to send vendor message 0x%06x", opcode);
return;
}
mesh_example_info_store(); /* Store proper mesh example info */
}
I tried to make my own function that would run on a button push instead of a mesh callback:
Code: Select all
void button_cb_msg(){
char *mydata = "TEST STRING PASSING";
esp_err_t err = esp_ble_mesh_server_model_send_msg(&vnd_models[0],
&test_ctx, ESP_BLE_MESH_VND_MODEL_OP_STATUS,
strlen(mydata)+1, (uint8_t *)mydata);
if (err) {
ESP_LOGE(TAG, "Failed to send message 0x%06x", ESP_BLE_MESH_VND_MODEL_OP_STATUS);
}
}
And this runs without error, but I don't see the message appear on the provisioner's side of things like I do when the first callback runs. Am I copying the ctx wrong maybe?