- This demo forwards the message sent by the nRF Mesh app.
- The user enters the address of the destination node and use it to forwarded packet.
- The types of the forwarded message include:
ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET
,ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET
,ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK
.
- The destination node reports its Onoff state with the
ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS
message.
Example: The nRF Mesh app sends a ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET
message to the node that runs the ble_mesh_client_model
project. Then this node sends a ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET
message to the destination node that runs the ble_mesh_node
project. The address of the destination node is entered by the user via the serial port.
- 1 x Device that runs the
ble_mesh_client_model
project. - 1 x Device that runs the
ble_mesh_node
project. - 1 x Phone that installs the nRF Mesh app for controlling these two devices
This demo has only one element, in which the following three models are implemented:
- Configuration Server model is mainly to represent a mesh network configuration, such as its AppKey, Relay State, TTL State, Subscription List State, etc.
- Generic OnOff Client model controls a Generic OnOff Server via messages defined by the Generic OnOff Model, that is, turning on and off the lights.
- Generic OnOff Server model implements the nodes' Onoff state.
You can choose from the 4 message sequences described below:
- Acknowledged Get
- Acknowledged Set
- Unacknowledged Set
- Acknowledged Set with Periodic Publishing
//Allocating memory for publishing messages.
static esp_ble_mesh_model_pub_t onoff_srv_pub = {
.msg = NET_BUF_SIMPLE(2 + 1),
.update = NULL,
.dev_role = MSG_ROLE,
};
//Registering the minimum length of messages. For example, the minimum length of the ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET message is registered as 2 octets.
static esp_ble_mesh_model_op_t onoff_op[] = {
{ ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET, 0, 0},
{ ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET, 2, 0},
{ ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK, 2, 0},
ESP_BLE_MESH_MODEL_OP_END,
};
//Registering the Generic Onoff Server model.
static esp_ble_mesh_model_t root_models[] = {
//onoff server's onoff state init
ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV, onoff_op,
&onoff_srv_pub, &led_state[0]),
};
//Allocating memory for publishing messages.
static esp_ble_mesh_model_pub_t onoff_cli_pub = {
.msg = NET_BUF_SIMPLE(2 + 1),
.update = NULL,
.dev_role = MSG_ROLE,
};
//Registering the Generic Onoff Client model.
static esp_ble_mesh_model_t root_models[] = {
ESP_BLE_MESH_MODEL_GEN_ONOFF_CLI(&onoff_cli_pub, &onoff_client),
};
esp_ble_mesh_register_generic_client_callback(esp_ble_mesh_generic_cb);
-
The callback function will be triggered when the Client model:
- Receives a message that indicates the Onoff state of the Sever model; Or
- Calls any APIs that send messages.
-
The events that the callback function handle:
Event name | Opcode | Description |
---|---|---|
ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT | ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET | The event triggered when the Generic Onoff Client model receives acknowledgment after sending the ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET message |
ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT | ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET | The event triggered when the Generic Onoff Client model receives acknowledgment after sending the ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET message |
ESP_BLE_MESH_GENERIC_CLIENT_PUBLISH_EVT | NA | The event triggered when the Generic Onoff Client model receives publishing messages. |
ESP_BLE_MESH_GENERIC_CLIENT_TIMEOUT_EVT | ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET | The event triggered when API (that send messages) calling times out |
esp_ble_mesh_register_custom_model_callback(esp_ble_mesh_model_cb);
-
The callback function will be triggered when the Server model:
- Receives a message that indicates operating the Onoff state of the Server model from the Generic Onoff Client model; Or
- Calls any APIs that send messages.
-
The events that the callback function:
Event Name | Opcode | Description |
---|---|---|
ESP_BLE_MESH_MODEL_OPERATION_EVT | ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET | The event triggered when the Generic Onoff Server model receives the ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET message that gets the Onoff state of the server from the Client model |
ESP_BLE_MESH_MODEL_OPERATION_EVT | ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET | The event triggered when the Generic Onoff Server model receives the ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET message that sets the Onoff state of the server from the Client model |
ESP_BLE_MESH_MODEL_OPERATION_EVT | ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK | The event triggered when the Generic Onoff Server model receives the ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK message that sets the Onoff state of the server from the Client model |
ESP_BLE_MESH_MODEL_SEND_COMP_EVT | NA | The event triggered when the esp_ble_mesh_server_model_send_msg API calling completes |
ESP_BLE_MESH_MODEL_PUBLISH_COMP_EVT | NA | The event triggered when the esp_ble_mesh_model_publish API calling completes |
The esp_ble_mesh_set_msg_common
function is used to set the message controlling parameters.
Parameter Name | Description |
---|---|
opcode |
The message opcode |
model |
The pointer to the client model struct |
ctx.net_idx |
The NetKey Index of the subnet through which the message is sent |
ctx.app_idx |
The AppKey Index for message encryption |
ctx.addr |
The address of the destination nodes |
ctx.send_ttl |
The TTL State, which determines how many times a message will be relayed |
ctx.send_rel |
This parameter determines if the model will wait for an acknowledgement after sending a message |
msg_timeout |
The maximum time the model will wait for an acknowledgement |
msg_role |
The role of message (node/provisioner) |
Note:
Please check the
ESP_BLE_MESH_MODEL_SEND_COMP_EVT
event to see if the message is sent successfully.
The Generic Onoff Client model calls the esp_ble_mesh_generic_client_get_state
API to get the state of the server model, such as the Onoff state.
esp_ble_mesh_generic_client_get_state_t get_state = {0};
esp_ble_mesh_set_msg_common(&common, node, onoff_client.model, ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET);
err = esp_ble_mesh_generic_client_get_state(&common, &get_state);
if (err) {
ESP_LOGE(TAG, "%s: Generic OnOff Get failed", __func__);
return;
}
The Generic Onoff Client model calls the esp_ble_mesh_generic_client_set_state
API to set the state of the server model, such as the Onoff state.
esp_ble_mesh_generic_client_set_state_t set_state = {0};
esp_ble_mesh_set_msg_common(&common, &set_state, onoff_client.model,
ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET, remote_onoff);
err = esp_ble_mesh_generic_client_set_state(&common, &set_state);
if (err != ESP_OK) {
ESP_LOGE(TAG, "%s: Generic OnOff Set failed", __func__);
return;
}
The Generic Onoff Server model has to bind its Appkey before calling the esp_ble_mesh_server_model_send_msg
API to send a message.
err = esp_ble_mesh_server_model_send_msg(model, ctx, ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS,
sizeof(send_data), &send_data);
The Generic Onoff Server model calls the esp_ble_mesh_model_publish
API to publish messages. Only the models that have subscribed to this destination address receive the published messages.
err = esp_ble_mesh_model_publish(model, ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS,
sizeof(led->current), &led->current, ROLE_NODE);
Please connect your devices and enters the address of the destination node via the serial port.
Users can adjust the address of the destination node.
Note: Please connect the pin 16 and pin 17 of the device that runs the
ble_mesh_client_model
project to the USB-to-UART tool.
#define UART1_TX_PIN GPIO_NUM_16
#define UART1_RX_PIN GPIO_NUM_17
The board_uart_task
task is used to receive commands sent via the serial port, among which, theremote_addr
represents the address of destination node that the message is forwarded to. Please enters hexadecimal string, such as 5, for this parameter. The address will be converted to 0x05 automatically.
static void board_uart_task(void *p)
{
uint8_t *data = calloc(1, UART_BUF_SIZE);
uint32_t input;
while (1) {
int len = uart_read_bytes(MESH_UART_NUM, data, UART_BUF_SIZE, 100 / portTICK_RATE_MS);
if (len > 0) {
input = strtoul((const char *)data, NULL, 16);
remote_addr = input & 0xFFFF;
ESP_LOGI(TAG, "%s: input 0x%08x, remote_addr 0x%04x", __func__, input, remote_addr);
memset(data, 0, UART_BUF_SIZE);
}
}
vTaskDelete(NULL);
}
The steps for this demo:
- The nRF Mesh App provisionings the unprovisioned devices into nodes;
- The nRF Mesh App adds a Appkey to these nodes, and bind the models of these nodes to this Appkey.
- The nRF Mesh App sends a controlling message to the Generic Onoff Client model. Then the Client model forwards this message to the server model of the other node.
The timing sequence diagram of this demo is shown below:
Note:
The node only forwards the message after it receives the controlling message sent by the app. That is said, the node will not forwards messages to the other nodes every time the user enters the address of the destination node through the serial port.
- Scan the unprovisioned devices.
- Identity the the capability of the unprovisioned devices.
- Provisioning the unprovisioned devices.
- Check if the Mesh node has been configured successful.
- Configure the models of the nodes.
- Click on the Generic On Off Client option.
- Bind the Generic On Off Client model to the Appkey.
- Check if the binding is successfully.
- Bind the Generic On Off Server model to the Appkey.
- Send controlling messages.