Send a semaphore every 20ms through the timer. After receiving the semaphore in the thread, push the data to the message queue
Then, the message queue is obtained through another thread, and finally through bt_nus_send() function send message to mobile phone
The procedure is as follows:
sensor data to msgq
K_SEM_DEFINE(sensor_sem, 0, 100); void sensors_timer_handler(struct k_timer *dummy) { k_sem_give(&sensor_sem); } K_TIMER_DEFINE(sensors_timer, sensors_timer_handler, NULL); void start_sensor_sample_timer(void) { k_timer_start(&sensors_timer, K_SECONDS(2), K_MSEC(20)); } void start_sensors_sample_send_thread(void){ while (true) { int ret = k_sem_take(&sensor_sem, K_MSEC(5000)); if ( ret != 0) { LOG_DBG("sensor sample send thread wait sem timeout"); } else { // add sensros data to msgq update_sensors_data(); } } } K_THREAD_DEFINE(sensor_sample_send_thread_id, STACKSIZE, start_sensors_sample_send_thread, NULL, NULL, NULL, PRIORITY, 0, 0);
get msgq data to ble send
void ble_send_sensors_sample_data_thread(void){ // get sensor data to msgq struct sensor_item_t sensor_data; while (true) { k_msgq_get(&sensor_data_msgq, &sensor_data, K_FOREVER); ret = bt_nus_send(NULL, sensor_data.array, sensor_data.length); if (ret) { LOG_ERR("Failed to send data over BLE connection %02x",ret); } } } K_THREAD_DEFINE(ble_send_sensors_sample_data_thread_id, STACKSIZE, ble_send_sensors_sample_data_thread, NULL, NULL, NULL, PRIORITY, 0, 0);
When the connection is unstable, the bt_nus_send() function will be blocked update_sensors_data();// add sensros data to msgq
I have adopted two threads
Why is update_sensors_data();// add sensros data to msgq blocked。
CONFIG_BT_DEBUG_CONN=y
CONFIG_BT_LOG_LEVEL_DBG=y
add to prj.conf
I got the following information: <wrn> bt_conn: Unable to get an immediate free conn_tx
