Jeedella / Plantenna_2_Node

This repo contains smart sustainable sensor node
4 stars 2 forks source link

Clarify communication protocol #8

Open joepboumans opened 3 years ago

joepboumans commented 3 years ago

The communication protocol and how the sensors are stored in the payload is very unclear. I have added a test sensor which can be assigned a values, but it is still uncleary which configuration are necessary for this or how the total payload looks like in this. Started by looking at bt_mesh_model to get a better understanding of what is happening

joepboumans commented 3 years ago

In the bt_mesh_model_pub in the bt_mesh_model the msg can be stored. This is first init'ed by the bt_mesh_model_msg_init with the correct opcode. Then the extra values can be added to the buffer using net_buf_add_le16. \ Once the message is created and correct it needs to be published. This can be done by the bt_mesh_model_publish function and when a valid message is placed in the buffer it will send it. This is non period and to make this periodic the callback bt_mesh_model.update can be used.

joepboumans commented 3 years ago

The RX of the BT works by assigning functions to the different status models. These opcodes with their respected function get put into the BT mesh model.

// Opcode
static const struct bt_mesh_model_op sensor_cli_op[] = {
    {BT_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS, 0, sensor_descriptor_status_rx},
    {BT_MESH_MODEL_OP_SENSOR_DATA_STATUS,       0, sensor_data_status_rx},
    {BT_MESH_MODEL_OP_SENSOR_COLUMN_STATUS,     0, sensor_column_status_rx},
    {BT_MESH_MODEL_OP_SENSOR_SERIES_STATUS,     0, sensor_series_status_rx},
    {BT_MESH_MODEL_OP_SENSOR_CADENCE_STATUS,    0, sensor_cadence_status_rx},
    {BT_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS,   0, sensor_settings_status_rx},
    {BT_MESH_MODEL_OP_SENSOR_SETTING_STATUS,    0, sensor_setting_status_rx},
    BT_MESH_MODEL_OP_END,
};

Knowing this I'm going to try to add my own custom opcode and receiving function to test how this works

joepboumans commented 3 years ago

Okay so to add custom opcodes these can be added to the mesh_sensor_common. In the form of:

//Custom test
#define BT_MESH_MODEL_OP_SENSOR_TEST_GET            BT_MESH_MODEL_OP_2(0X82, 0X37)
#define BT_MESH_MODEL_OP_SENSOR_TEST_STATUS         BT_MESH_MODEL_OP_1(0x5C)

Next these need to be added to the server model and their rx function needs to be made. These are defined differently for the client and the sensor server (They are named the same, but behave differently). Below is the the rx function with the sensor model which were made.

void sensor_test_get_rx(struct bt_mesh_model *model,
                            struct bt_mesh_msg_ctx *ctx,
                            struct net_buf_simple *buf); 

// Opcode
static const struct bt_mesh_model_op sensor_srv_op[] = {
    {BT_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET, 0, sensor_descriptor_get_rx},
    {BT_MESH_MODEL_OP_SENSOR_DATA_GET,       0, sensor_data_get_rx},
    {BT_MESH_MODEL_OP_SENSOR_COLUMN_GET,     2, sensor_column_get_rx},
    {BT_MESH_MODEL_OP_SENSOR_SERIES_GET,     2, sensor_series_get_rx},
    {BT_MESH_MODEL_OP_SENSOR_TEST_GET,       0, sensor_test_get_rx},
    BT_MESH_MODEL_OP_END,
};

The tx function can be designed in without them needing to be include into the mesh model. For the tx function the bt_mesh_model_publish(model) (Link). This can be adapted to use the periodic sending with the bt_mesh_model_pub model with the update function in the same model.