I am trying to sending data from custom bluetooth mesh sensor server to sensor client, but the sensor_cli_data_cb() function cannot get called.

I am trying to send data from icm20948 sensor from the sensor node to the observer node using ble mesh. Due to the data from icm20948 is too large (9 uint16_t, 18bytes) for the given sensor types from nordic sample, so I created a custom sensor server to send them. The sensor server structure looks like this:

static const struct bt_mesh_sensor_channel people_count_sensor_channels[] = { {
        .format = &bt_mesh_sensor_format_count_16,
        .name = "channel_1",
    }, {
        .format = &bt_mesh_sensor_format_count_16,
        .name = "channel_2",
    }, {
        .format = &bt_mesh_sensor_format_count_16,
        .name = "channel_3",
    },
};

static const struct bt_mesh_sensor_type icm20948_sensor_type_accel = {
    .id = BT_MESH_PROP_ID_CUSTOM_ICM20948_ACCEL,
    .channel_count = 3,
    .channels = people_count_sensor_channels,
};

static const struct bt_mesh_sensor_type icm20948_sensor_type_gyro = {
    .id = BT_MESH_PROP_ID_CUSTOM_ICM20948_GYRO,
    .channel_count = 3,
    .channels = people_count_sensor_channels,
};

static const struct bt_mesh_sensor_type icm20948_sensor_type_mag = {
    .id = BT_MESH_PROP_ID_CUSTOM_ICM20948_MAG,
    .channel_count = 3,
    .channels = people_count_sensor_channels,
};

/* ... ... other codes ... ... */

static struct bt_mesh_sensor icm20948_sensor_accel = { // 人数传感器定义
    .type = &icm20948_sensor_type_accel, // 自定义sensor_type类型
    .get = icm20948_data_get,
};

static struct bt_mesh_sensor icm20948_sensor_gyro = { // 人数传感器定义
    .type = &icm20948_sensor_type_gyro, // 自定义sensor_type类型
    .get = icm20948_data_get,
};

static struct bt_mesh_sensor icm20948_sensor_mag = { // 人数传感器定义
    .type = &icm20948_sensor_type_mag, // 自定义sensor_type类型
    .get = icm20948_data_get,
};

static struct bt_mesh_sensor *const people_count_sensor_data[] = {
    &icm20948_sensor_accel,
    &icm20948_sensor_gyro,
    &icm20948_sensor_mag,
}; // icm20948传感器传输数据指针定义,包含了三个自定义的传感器实例

static struct bt_mesh_sensor_srv people_count_sensor_srv =
    BT_MESH_SENSOR_SRV_INIT(people_count_sensor_data, ARRAY_SIZE(people_count_sensor_data)); // 人数传感器服务器定义与初始化
    
    static struct bt_mesh_elem elements[] = {
    BT_MESH_ELEM(1,
            BT_MESH_MODEL_LIST(BT_MESH_MODEL_CFG_SRV,
                    BT_MESH_MODEL_HEALTH_SRV(&health_srv,
                    			 &health_pub),
                    BT_MESH_MODEL_SENSOR_SRV(&people_count_sensor_srv)),
            BT_MESH_MODEL_NONE),
    };

and it successfully sent 18 bytes data by putting them into 3 custom sensor instances each of which contains 3 sensor channels. I could then see data getting sent on nrf mesh mobile app by configuring the sensor server.

After that, I tried to get those data by sensor client sample on another board. I changed the sensor_client sample to read the PROP_ID I set for my custom sensor types, like this:

/* ... ... */
	case (5): {
		err = bt_mesh_sensor_cli_get(&sensor_cli, NULL, &icm20948_sensor_type_accel, NULL);
		if (err) {
			printk("Error getting people count (%d)\n", err);
		} else {
			printk("Success getting people count data!\n");
		}
		break;
	}
/* ... ... */

and the sensor_cli_data_cb() function like this:

static void sensor_cli_data_cb(struct bt_mesh_sensor_cli *cli,
			       struct bt_mesh_msg_ctx *ctx,
			       const struct bt_mesh_sensor_type *sensor,
			       const struct bt_mesh_sensor_value *value)
{
	enum bt_mesh_sensor_value_status status;

	if  (sensor->id == icm20948_sensor_type_accel.id) {
		printk("ICM20948 accel data is %s\n", bt_mesh_sensor_ch_str(value));
	}
}

Then I built and flashed the board, configured the sensor server and sensor client on app, then I found that I can see data being sent from sensor server on the sensor node board, and get_data function of my sensor client run correctly, but the sensor_cli_data_cb() function cannot get called.

I tried to use the given sensor type from the sample (like people count sensor), the sensor_cli_data_cb() function can get called correctly after receiving message; but once I changed to my custom sensor server and sensor client, I cannot see the sensor_cli_data_cb() function getting called.

Parents
  • Add: I then tried to add an unknown type callback function to see if the sensor client received the information:

    static void sensor_cli_unknown_type_cb(struct bt_mesh_sensor_cli *cli, struct bt_mesh_msg_ctx *ctx, uint16_t id, uint32_t opcode) {
    	LOG_ERR("Unknown sensor ID: 0x%04x", id);
    	LOG_ERR("Opcode of unknown sensor: %x", opcode);
    }

    then I built and flashed to the board and get:

    [00:04:31.136,077] <err> sensor_client: Unknown sensor ID: 0x4a01
    [00:04:31.136,108] <err> sensor_client: Opcode of unknown sensor: 52

    I have defined my custom sensor type both in sensor server and sensor client, binding with the same property ID. what else do i need to do to solve this problem?

  • Hi,

    Was this not an issue before changing the sensor model? Did it work before?

    , and get_data function of my sensor client run correctly, but the sensor_cli_data_cb() function cannot get called.

    Could you expand a bit on this? Is this get_data function showing that the data is infact recieved, but the callback isn't being triggered?

    There are also a few steps here worth going through in case you've missed anything.

    Regards,

    ELfving

  • Hello, 

    Thank you for your reply! It's not an issue when I used the given sensor type from the sensor_server&sensor_client samples (for example bt_mesh_sensor_type_people_count). But when I used my custom sensor type the problem occurred.

    Could you expand a bit on this?

    As the code I shown above, when get_data function did not receive any messages, err would return -125 and print "error getting people count". and when received message, normally err would be 0 and print "Success getting people count data" and call the data callback function to process the sensor data. However, when I use my custom sensor type, I can see sensor client printing "Success getting people count data" but the data callback function has no response.

    I then add an unknown type callback function to sensor_cli initialisation, then I found that unknown type callback function would get called when client received message.

  • Hello

    The problem have been solved! I found out that custom sensor type/channel/format can only be defined and initialised in sensor_type.h and sensor_type.c files using SENSOR_TYPE macro.

Reply Children
Related