Sending MPU6050 sensor data through BLE (5340DK)

 while Sending MPU6050 sensor data through BLE (5340DK) .In the nrf connect for mobile I am getting following data,sensor reading is not transferred propoerly In the vscode log it is showing correct reading when transferred though BLE and viewed in app below ouput comes.

  

MAIN.C 

#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/sys/byteorder.h>

/* UUIDs for the custom BLE service and characteristics */
#define CUSTOM_SERVICE_UUID BT_UUID_DECLARE_128(BT_UUID_128_ENCODE(0x12345678, 0x1234, 0x5678, 0x1234, 0x123456789abc))
#define ACCEL_CHAR_UUID BT_UUID_DECLARE_128(BT_UUID_128_ENCODE(0x12345678, 0x1234, 0x5678, 0x1234, 0xabcdefabcdef))
#define GYRO_CHAR_UUID BT_UUID_DECLARE_128(BT_UUID_128_ENCODE(0x12345678, 0x1234, 0x5678, 0x1234, 0x123456abcdef))

static struct bt_conn *current_conn;

/* GATT Characteristic values */
static uint8_t accel_data[12]; /* 3 axes, each 4 bytes (int32) */
static uint8_t gyro_data[12];

/* GATT Service Declaration */
BT_GATT_SERVICE_DEFINE(custom_svc,
    BT_GATT_PRIMARY_SERVICE(CUSTOM_SERVICE_UUID),
    BT_GATT_CHARACTERISTIC(ACCEL_CHAR_UUID,
        BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
        BT_GATT_PERM_READ, NULL, NULL, accel_data),
    BT_GATT_CCC(NULL, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
    BT_GATT_CHARACTERISTIC(GYRO_CHAR_UUID,
        BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
        BT_GATT_PERM_READ, NULL, NULL, gyro_data),
    BT_GATT_CCC(NULL, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE)
);

static const struct bt_data ad[] = {
    BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
    BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_128_ENCODE(0x12345678, 0x1234, 0x5678, 0x1234, 0x123456789abc)),
};

void main(void) {
    const struct device *mpu6050;
    struct sensor_value accel[3], gyro[3];

    /* Initialize BLE */
    int err = bt_enable(NULL);
    if (err) {
        printk("Bluetooth init failed (err %d)\n", err);
        return;
    }

    printk("Bluetooth initialized\n");

    err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
    if (err) {
        printk("Advertising failed (err %d)\n", err);
        return;
    }

    printk("Advertising started\n");

    /* Initialize MPU6050 */
    mpu6050 = device_get_binding("MPU6050");
    if (!mpu6050) {
        printk("Error: Could not get MPU6050 device binding\n");
        return;
    }

    printk("MPU6050 initialized\n");

    while (1) {
        /* Fetch and send accelerometer and gyroscope data */
        if (sensor_sample_fetch(mpu6050) < 0) {
            printk("Error: Failed to fetch data from MPU6050\n");
            k_sleep(K_MSEC(1000));
            continue;
        }

        if (sensor_channel_get(mpu6050, SENSOR_CHAN_ACCEL_XYZ, accel) == 0) {
            sys_put_le32(accel[0].val1, &accel_data[0]);
            sys_put_le32(accel[1].val1, &accel_data[4]);
            sys_put_le32(accel[2].val1, &accel_data[8]);
        }

        if (sensor_channel_get(mpu6050, SENSOR_CHAN_GYRO_XYZ, gyro) == 0) {
            sys_put_le32(gyro[0].val1, &gyro_data[0]);
            sys_put_le32(gyro[1].val1, &gyro_data[4]);
            sys_put_le32(gyro[2].val1, &gyro_data[8]);
        }

        /* Notify connected devices */
        if (current_conn) {
            bt_gatt_notify(current_conn, &custom_svc.attrs[2], accel_data, sizeof(accel_data));
            bt_gatt_notify(current_conn, &custom_svc.attrs[5], gyro_data, sizeof(gyro_data));
        }

        /* Wait before next read */
        k_sleep(K_MSEC(1000));
    }
}

/* Connection callback */
static void connected(struct bt_conn *conn, uint8_t err) {
    if (err) {
        printk("Connection failed (err %u)\n", err);
    } else {
        printk("Connected\n");
        current_conn = conn;
    }
}

static void disconnected(struct bt_conn *conn, uint8_t reason) {
    printk("Disconnected (reason %u)\n", reason);
    current_conn = NULL;
}

BT_CONN_CB_DEFINE(conn_callbacks) = {
    .connected = connected,
    .disconnected = disconnected,
};
PRJ.CONF
# Bluetooth Configuration
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y


# Enable I2C for sensor communication
CONFIG_I2C=y
CONFIG_I2C_LOG_LEVEL_DBG=y  # I2C logging at debug level

# Enable Sensor
CONFIG_SENSOR=y
CONFIG_MPU6050=y

# Enable logging (Log Level: 3 for info, 4 for debug)
CONFIG_LOG=y
CONFIG_LOG_DEFAULT_LEVEL=4  # Set logging level to debug

# Enable the Console for logging output via UART
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y

# Enable memory settings and stack size
CONFIG_MAIN_STACK_SIZE=1024
CONFIG_HEAP_MEM_POOL_SIZE=1024
# Bluetooth Configuration
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y


# Enable I2C for sensor communication
CONFIG_I2C=y
CONFIG_I2C_LOG_LEVEL_DBG=y  # I2C logging at debug level

# Enable Sensor
CONFIG_SENSOR=y
CONFIG_MPU6050=y

# Enable logging (Log Level: 3 for info, 4 for debug)
CONFIG_LOG=y
CONFIG_LOG_DEFAULT_LEVEL=4  # Set logging level to debug

# Enable the Console for logging output via UART
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y

# Enable memory settings and stack size
CONFIG_MAIN_STACK_SIZE=1024
CONFIG_HEAP_MEM_POOL_SIZE=1024


# Bluetooth Configuration
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y


# Enable I2C for sensor communication
CONFIG_I2C=y
CONFIG_I2C_LOG_LEVEL_DBG=y  # I2C logging at debug level

# Enable Sensor
CONFIG_SENSOR=y
CONFIG_MPU6050=y

# Enable logging (Log Level: 3 for info, 4 for debug)
CONFIG_LOG=y
CONFIG_LOG_DEFAULT_LEVEL=4  # Set logging level to debug

# Enable the Console for logging output via UART
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y

# Enable memory settings and stack size
CONFIG_MAIN_STACK_SIZE=1024
CONFIG_HEAP_MEM_POOL_SIZE=1024

LOG MESSAGES IN VSCODE
--- 19 messages dropped ---
[00:12:54.814,086] <dbg> bt_conn: bt_conn_tx_processor: no buf returned
[00:12:54.814,117] <dbg> bt_conn: bt_conn_unref: handle 47 ref 2 -> 1
[00:12:54.814,117] <dbg> bt_hci_core: tx_processor: TX process start
[00:12:54.814,147] <dbg> bt_conn: bt_conn_tx_processor: start
[00:12:54.814,178] <dbg> bt_conn: bt_conn_tx_processor: no connection wants to do stuff
--- 9 messages dropped ---
[00:12:54.844,726] <dbg> bt_conn: tx_notify_process: conn 0x200011c0
[00:12:54.844,757] <dbg> bt_conn: tx_notify_process: tx 0x200018d0 cb 0 user_data 0
[00:12:54.844,757] <dbg> bt_conn: tx_free: 0x200018d0
[00:12:54.844,787] <dbg> bt_conn: tx_notify_process: raise TX IRQ
[00:12:54.844,818] <dbg> bt_hci_core: bt_tx_irq_raise: kick TX
[00:12:54.844,848] <dbg> bt_hci_core: tx_processor: TX process start
[00:12:54.844,848] <dbg> bt_conn: bt_conn_tx_processor: start
[00:12:54.844,879] <dbg> bt_conn: bt_conn_tx_processor: no connection wants to do stuff
[00:12:54.844,909] <dbg> os: k_sched_unlock: scheduler unlocked (0x200008f8:0)
[00:12:54.844,940] <dbg> bt_hci_driver: bt_ipc_rx: RX buf payload:
33 68 83 01 00 |3h...
[00:12:54.892,791] <dbg> bt_hci_driver: bt_ipc_rx: ipc data:
04 13 05 01 2f 00 01 00 |..../...
[00:12:54.892,822] <dbg> bt_hci_driver: bt_ipc_evt_recv: len 5
[00:12:54.892,852] <dbg> bt_hci_driver: bt_ipc_rx: Calling bt_recv(0x200056ec)
[00:12:54.892,852] <dbg> bt_hci_core: bt_recv_unsafe: buf 0x200056ec len 7
[00:12:54.892,883] <dbg> bt_hci_core: hci_num_completed_packets: num_handles 1
[00:12:54.892,913] <dbg> bt_hci_core: hci_num_completed_packets: handle 47 count 1
[00:12:54.892,944] <dbg> bt_conn: bt_conn_ref: handle 47 ref 1 -> 2
[00:12:54.892,974] <dbg> bt_conn: bt_conn_unref: handle 47 ref 2 -> 1
[00:12:54.893,005] <dbg> os: k_sched_unlock: scheduler unlocked (0x200008f8:0)
[00:12:54.893,035] <dbg> bt_conn: tx_notify_process: conn 0x200011c0
[00:12:54.893,066] <dbg> bt_conn: tx_notify_process: tx 0x200018dc cb 0 user_data 0
[00:12:54.893,066] <dbg> bt_conn: tx_free: 0x200018dc
[00:12:54.893,096] <dbg> bt_conn: tx_notify_process: raise TX IRQ
[00:12:54.893,096] <dbg> bt_hci_core: bt_tx_irq_raise: kick TX
[00:12:54.893,157] <dbg> bt_hci_core: tx_processor: TX process start
[00:12:54.893,157] <dbg> bt_conn: bt_conn_tx_processor: start
[00:12:54.893,188] <dbg> bt_conn: bt_conn_tx_processor: no connection wants to do stuff
[00:12:54.893,218] <dbg> bt_hci_driver: bt_ipc_rx: RX buf payload:
94 5b 02 00 60 |.[..`
[00:12:55.818,176] <dbg> bt_hci_core: bt_send: buf 0x200057d0 len 23 type 2
--- 107 messages dropped ---
[00:12:55.818,695] <dbg> bt_hci_core: bt_tx_irq_raise: kick TX
[00:12:55.818,725] <dbg> bt_conn: bt_conn_tx_processor: no buf returned
[00:12:55.818,725] <dbg> bt_conn: bt_conn_unref: handle 47 ref 2 -> 1
[00:12:55.818,756] <dbg> bt_hci_core: tx_processor: TX process start
[00:12:55.818,786] <dbg> bt_conn: bt_conn_tx_processor: start
[00:12:55.818,817] <dbg> bt_conn: bt_conn_tx_processor: no connection wants to do stuff
--- 12 messages dropped ---
[00:12:55.819,793] <dbg> bt_conn: tx_notify_process: raise TX IRQ
[00:12:55.819,824] <dbg> bt_hci_core: bt_tx_irq_raise: kick TX
[00:12:55.819,854] <dbg> bt_hci_core: tx_processor: TX process start
[00:12:55.819,885] <dbg> bt_conn: bt_conn_tx_processor: start
[00:12:55.819,915] <dbg> bt_conn: bt_conn_tx_processor: no connection wants to do stuff
[00:12:55.819,946] <dbg> os: k_sched_unlock: scheduler unlocked (0x200008f8:0)
[00:12:55.819,976] <dbg> bt_hci_driver: bt_ipc_rx: RX buf payload:
ca e8 83 01 58 |....X
[00:12:55.867,828] <dbg> bt_hci_driver: bt_ipc_rx: ipc data:
04 13 05 01 2f 00 01 00 |..../...
[00:12:55.867,828] <dbg> bt_hci_driver: bt_ipc_evt_recv: len 5
[00:12:55.867,858] <dbg> bt_hci_driver: bt_ipc_rx: Calling bt_recv(0x200056ec)
[00:12:55.867,889] <dbg> bt_hci_core: bt_recv_unsafe: buf 0x200056ec len 7
[00:12:55.867,889] <dbg> bt_hci_core: hci_num_completed_packets: num_handles 1
[00:12:55.867,919] <dbg> bt_hci_core: hci_num_completed_packets: handle 47 count 1
[00:12:55.867,950] <dbg> bt_conn: bt_conn_ref: handle 47 ref 1 -> 2
[00:12:55.867,980] <dbg> bt_conn: bt_conn_unref: handle 47 ref 2 -> 1
[00:12:55.868,041] <dbg> bt_conn: tx_notify_process: conn 0x200011c0
[00:12:55.868,041] <dbg> bt_conn: tx_notify_process: tx 0x200018d0 cb 0 user_data 0
[00:12:55.868,072] <dbg> bt_conn: tx_free: 0x200018d0
[00:12:55.868,103] <dbg> bt_conn: tx_notify_process: raise TX IRQ
[00:12:55.868,103] <dbg> bt_hci_core: bt_tx_irq_raise: kick TX
[00:12:55.868,133] <dbg> bt_hci_core: tx_processor: TX process start
[00:12:55.868,164] <dbg> bt_conn: bt_conn_tx_processor: start
[00:12:55.868,164] <dbg> bt_conn: bt_conn_tx_processor: no connection wants to do stuff
[00:12:55.868,225] <dbg> os: k_sched_unlock: scheduler unlocked (0x200008f8:0)
[00:12:55.868,255] <dbg> bt_hci_driver: bt_ipc_rx: RX buf payload:
00 21 00 00 20 |.!..
[00:12:56.822,814] <dbg> bt_hci_core: bt_send: buf 0x200057d0 len 23 type 2
--- 87 messages dropped ---
[00:12:56.822,845] <dbg> bt_hci_driver: bt_ipc_send: buf 0x200057d0 type 2 len 23
[00:12:56.822,875] <dbg> bt_hci_driver: bt_ipc_send: Final HCI buffer:
02 2f 00 13 00 0f 00 04 00 1b 15 00 00 00 00 00 |./...... ........
00 00 00 00 00 00 00 00 |........
--- 19 messages dropped ---
[00:12:56.823,364] <dbg> bt_conn: bt_conn_tx_processor: no buf returned
[00:12:56.823,394] <dbg> bt_conn: bt_conn_unref: handle 47 ref 2 -> 1
[00:12:56.823,425] <dbg> bt_hci_core: tx_processor: TX process start
--- 14 messages dropped ---
[00:12:56.843,566] <dbg> bt_conn: tx_notify_process: raise TX IRQ
[00:12:56.843,566] <dbg> bt_hci_core: bt_tx_irq_raise: kick TX
[00:12:56.843,597] <dbg> bt_hci_core: tx_processor: TX process start
[00:12:56.843,627] <dbg> bt_conn: bt_conn_tx_processor: start
[00:12:56.843,658] <dbg> bt_conn: bt_conn_tx_processor: no connection wants to do stuff
[00:12:56.843,688] <dbg> os: k_sched_unlock: scheduler unlocked (0x200008f8:0)
[00:12:56.843,719] <dbg> bt_hci_driver: bt_ipc_rx: RX buf payload:
00 19 00 00 38 |....8
[00:12:56.891,571] <dbg> bt_hci_driver: bt_ipc_rx: ipc data:
04 13 05 01 2f 00 01 00 |..../...
[00:12:56.891,601] <dbg> bt_hci_driver: bt_ipc_evt_recv: len 5
[00:12:56.891,632] <dbg> bt_hci_driver: bt_ipc_rx: Calling bt_recv(0x200056ec)
[00:12:56.891,662] <dbg> bt_hci_core: bt_recv_unsafe: buf 0x200056ec len 7
[00:12:56.891,693] <dbg> bt_hci_core: hci_num_completed_packets: num_handles 1
[00:12:56.891,693] <dbg> bt_hci_core: hci_num_completed_packets: handle 47 count 1
[00:12:56.891,723] <dbg> bt_conn:

   
How to rectify this error  rectify this and get proper reading App
Parents
  • Hello,

    You need to enable notifications (subscribe) from the characteristics you want to receive data from (see three inbound arrow icons) when you are connected to a device in nRF Connect for mobile app:
    https://nordicsemiconductor.github.io/IOS-nRF-Connect/assets/files/UserManual.pdf 

    For debugging you might want to try to send a fixed array data for accel_data and gyro_data.

    Kenneth

  • I had enabled the notifications (3 inbound arrows)   but I received same output

      /* Notify connected devices with the updated data */
            if (current_conn) {
                if (bt_gatt_is_subscribed(&custom_svc.attrs[1], current_conn, BT_GATT_CCC_NOTIFY))
                 {
                 
                      bt_gatt_notify(current_conn, &custom_svc.attrs[1], accel_data, sizeof(accel_data));
                }
                if (bt_gatt_is_subscribed(&custom_svc.attrs[3], current_conn, BT_GATT_CCC_NOTIFY)) {
                    bt_gatt_notify(current_conn, &custom_svc.attrs[3], gyro_data, sizeof(gyro_data));

                   
                }
            }                   should I do anything in this part of code as it is notifying part    or sensor reading format part
  • the data being notified through BLE is not as expected.

  • Any update...[00:00:01.535,858] <err> bt_hci_driver: Endpoint binding failed with -11
    [00:00:01.543,182] <err> bt_hci_core: HCI driver open failed (-11)
    Bluetooth init failed (err -11)              what is suitable headerfiles and prj.conf files                        

    #include <zephyr/kernel.h>
    #include <zephyr/device.h>
    #include <zephyr/drivers/sensor.h>
    #include <zephyr/bluetooth/bluetooth.h>
    #include <zephyr/bluetooth/conn.h>
    #include <zephyr/bluetooth/gatt.h>
    #include <zephyr/sys/byteorder.h>
    #include <zephyr/bluetooth/hci.h>                             
    #include <zephyr/bluetooth/conn.h>
    #include <zephyr/bluetooth/uuid.h>
    #include <zephyr/bluetooth/gatt.h>
    #include <zephyr/sys/printk.h>
    # Bluetooth stack and HCI
    CONFIG_BT=y
    CONFIG_BT_PERIPHERAL=y
    CONFIG_BT_DEVICE_NAME="MPU6050 Sensor"
    CONFIG_BT_HCI_DRIVER_LOG_LEVEL_DBG=y
    CONFIG_BT_HCI=y
      # Optional, for central role support

    # Logging
    CONFIG_LOG=y
    CONFIG_LOG_MODE_IMMEDIATE=y
    CONFIG_LOG_DEFAULT_LEVEL=3
    CONFIG_LOG_DEFAULT_LEVEL=4

    # I2C and Sensor support
    CONFIG_I2C=y
    CONFIG_SENSOR=y
    CONFIG_MPU6050=y

    # Main thread and system workqueue stack size
    CONFIG_MAIN_STACK_SIZE=4096
    CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096                    this what curently used ....why Host and Controller couldn.t able to bind
  • Think last time I saw a -11 error the network core was not programmed. Suggest you test with a standard BLE example and check if it works.

    Kenneth

  • using BLE example also gicing me the -11 error

  • Then it does sound as the network core is not programmed. Are you building and programming from VS code or command line? If you are using VS code, can you show me a screenshot of the VS code window when you press the Flash button. Which ncs version are you using?

    Kenneth

Reply Children
No Data
Related