BLE Communication between two nrf5340

Hello,

I have on nrf5340 an application, that meassures Temperature every second. I need to send the data to another nrf5340. I am going throw the bluetooth samples, but can't find, how can I astabilish the connection between two nrf5340s. Could you please recommend me some tutorial, that handles this issue?

Parents
  • Hi Amanda

    thank you very much for the tips.

    While testing those samples, I can't get any output, that the Kits are connected. I only get a message "Starting Bluetooth Central UART example" in NRF TERMINAL, but nothing else. On the Kit with the Peripheral sample on it indicates the LED 2 that the Kit is connected, but in NRF TERMINAL is also only the "Starting Nordic UART service example" message. I also connected PuTTy to the Central, but I don!'t get any message here. I did not change anything in the Samples so far.

    Could you please give me an advice, what should I do?

    Regards, Jirik

  • Hi, 

    In the UART samples, the UART console is used to send and read data over the NUS service. Debug messages are not displayed in this UART console. Instead, they are printed by the RTT logger. If you want to view the debug messages, follow the procedure in Connecting using RTT. Once you connect via RTT, you can the event logs. 

    -Amanda

  • Hi Amanda

    Thank you very much for tips, I think I understood the connecting and the communication between the Central and Peripheral. Now I have problems with getting the data from the temperature sensors to the UART sending buffer. I get the temperature data with SPI interface as uint32_t.

    Would you have some advice for me?

    Jiri

  • Hi Amanda

    thank you for advice. I have still problems with the received data. 

    This is how is changed the ble_write_thread, I just added array number and variable length, and I'm increasing the number everz second and send it to the central.

    uint8_t number[1];
    uint16_t length;
    
    void ble_write_thread(void)
    {
    	/* Don't go any further until BLE is initialized */
    	k_sem_take(&ble_init_ok, K_FOREVER);
    	length = sizeof(number);
    	number[0] = 0b0000001;
    	for (;;) {
    		if (bt_nus_send(NULL, number, length)) {
    			LOG_WRN("Failed to send data over BLE connection");
    		}
    		number[0] ++;
    		length = sizeof(number);
    		k_msleep(1000);
    		printk("number %x\t length %x\n", number[0], length);
    	}
    }

    The output in PuTTy of the peripheral looks like this: 

    Starting Nordic UART service example
    
    number 2         length 1
    
    number 3         length 1
    
    number 4         length 1
    
    number 5         length 1
    

    When I do no changes to the central, the UART outputs in PuTTy are characters, which I do not need, I need the numbers, so I made following change.

    static uint8_t ble_data_received(struct bt_nus_client *nus,
    						uint8_t *data, uint16_t len)
    {
    	ARG_UNUSED(nus);
    
    	printk("%x\n", &data);
    	
    	return BT_GATT_ITER_CONTINUE;
    }

    In the central I just let the received data print to console like this (without sending it to UART).

    But now I don't get the data I send, I get something like this: 

    20003b94
    
    20003b94
    
    20003b94
    
    20003b94
    
    20003b94
    
    20003b94
    
    20003b94
    
    20003b94
    

    Some data, I don't understand, and every time same. How is it possible?

    I think in the central I work with wrong data, but I can't find the right.

    I also tried another sample, peripheral_hr and central_hr, I think this programm does the same thing as I tried, the peripheral sends every second increased number, and the central should receive it and print it, but it prints every time the same number.

    PuTTy - Peripheral

    Heartrate 141
    
    Heartrate 142
    
    Heartrate 143
    
    Heartrate 144
    
    Heartrate 145
    
    Heartrate 146
    
    Heartrate 147
    
    Heartrate 148
    
    Heartrate 149
    

    PuTTy - Central

    [NOTIFICATION] data 20005dc0 length 2
    
    [NOTIFICATION] data 20005dc0 length 2
    
    [NOTIFICATION] data 20005dc0 length 2
    
    [NOTIFICATION] data 20005dc0 length 2
    
    [NOTIFICATION] data 20005dc0 length 2
    
    [NOTIFICATION] data 20005dc0 length 2
    
    [NOTIFICATION] data 20005dc0 length 2
    
    [NOTIFICATION] data 20005dc0 length 2
    
    [NOTIFICATION] data 20005dc0 length 2
    
    

    I think it must be some other parameter read.

    Could you please give me an advice, how do I get to the data, I actually wanted to send?

    Jiri

  • I just solved the issue with the output, I completely forgot that data is an array, so i need to print it like

    printk("%x\n", data[0]);

    Just a beginners mistake :D

Reply Children
  • Hello Amanda

    I'm now trying to set the directed advertising on the peripheral, so it accepts only connections request from my peer, but I can't find, in which format should i set the address of the central. I tried to change it this way in peripheral main, but it did not connect.

    err = bt_le_adv_start(((struct bt_le_adv_param[]) { 
    { .id = BT_ID_DEFAULT, .sid = 0, .secondary_max_skip = 0, 
    .options = (BT_LE_ADV_OPT_CONNECTABLE), .interval_min = (0x00a0), 
    .interval_max = (0x00f0), .peer = {0xF0,0x33,0x4D,0x3A,0x9F,0x6B}, } }),
    ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));

    I am also a little bit confused of the Bluetooth LE Device Adress in addr.h library.

    /** Bluetooth Device Address */
    typedef struct {
    	uint8_t  val[BT_ADDR_SIZE];
    } bt_addr_t;
    /**/
    
    /** Length in bytes of an LE Bluetooth address. Not packed, so no sizeof() */
    #define BT_ADDR_LE_SIZE 7
    /** Bluetooth LE Device Address */
    typedef struct {
    	uint8_t      type;
    	bt_addr_t a;
    } bt_addr_le_t;

    What does the variable type mean and in which way should I write the bt_addr_t?

    Here is the log from J-Link RTT Viewer

    00> [00:00:00.272,583] <inf> fs_nvs: 2 Sectors of 4096 bytes
    00> [00:00:00.272,583] <inf> fs_nvs: alloc wra: 0, fc8
    00> [00:00:00.272,583] <inf> fs_nvs: data wra: 0, 2c
    00> [00:00:00.300,354] <inf> bt_hci_core: No ID address. App must call settings_load()
    00> [00:00:00.300,354] <inf> peripheral_uart: Bluetooth initialized
    00> [00:00:00.949,981] <inf> peripheral_uart: Connected F0:33:4D:3A:9F:6B (random)
    00> [00:00:01.664,978] <wrn> peripheral_uart: Failed to send data over BLE connection

    Sorry for maybe stupid questions, I am just beginner and did not find it anywhere.

  • Hi, 

    Please create a new support case since this issue is away from the original scope. 

    -Amanda

Related