How to print RSSI value of all the advertising packets from peripheral to central and print on PuTTy.

Hi,

I'm working on central_uart and peripheral_uart examples. I want to get the rssi value and tx_power of all advertising packets from peripheral to central via bluetooth connection. How can i get these values and print the same on the PuTTy terminal.

Can you please guide me with all the functions i need to involve in the peripheral or central main functions.

Regards

Karthik Kumar

Parents
  • Hi,

    static void set_tx_power(uint8_t handle_type, uint16_t handle, int8_t tx_pwr_lvl).
    but as seen on the terminal its prompting and error as follows,
    Set Tx power err: -5 reason 0x00 // for Tx_power.
    How can this errors be resolved and set tx power value to the maximum always both in central and peripheral.
    /******************************************* TX Power*********************************************************************/
    
    static void set_tx_power(uint8_t handle_type, uint16_t handle, int8_t tx_pwr_lvl)
    {
    	struct bt_hci_cp_vs_write_tx_power_level *cp;
    	struct bt_hci_rp_vs_write_tx_power_level *rp;
    	struct net_buf *buf, *rsp = NULL;
    	int err;
    
    	buf = bt_hci_cmd_create(BT_HCI_OP_VS_WRITE_TX_POWER_LEVEL,
    				sizeof(*cp));
    	if (!buf) {
    		printk("Unable to allocate command buffer\n");
    		return;
    	}
    
    	cp = net_buf_add(buf, sizeof(*cp));
    	cp->handle = sys_cpu_to_le16(handle);
    	cp->handle_type = handle_type;
    	cp->tx_power_level = tx_pwr_lvl;
    
    	err = bt_hci_cmd_send_sync(BT_HCI_OP_VS_WRITE_TX_POWER_LEVEL,
    				   buf, &rsp);
    	if (err) {
    		uint8_t reason = rsp ?
    			((struct bt_hci_rp_vs_write_tx_power_level *)
    			  rsp->data)->status : 0;
    		printk("Set Tx power err: %d reason 0x%02x\n", err, reason);
    		return;
    	}
    
    	rp = (void *)rsp->data;
    	printk("Actual Tx Power: %d\n", rp->selected_tx_power);
    
    	net_buf_unref(rsp);
    }
    
    /******************************************* Main *****************************************************/
    void main(void)
    {
    	int blink_status = 0;
    	int err = 0;
    	int8_t txp = 3;
    	
    	configure_gpio();
    
    	err = uart_init();
    	if (err) {
    		error();
    	}
    
    	bt_conn_cb_register(&conn_callbacks);
    
    	if (IS_ENABLED(CONFIG_BT_NUS_SECURITY_ENABLED)) {
    		bt_conn_auth_cb_register(&conn_auth_callbacks);
    	}
    
    	err = bt_enable(NULL);
    	if (err) {
    		error();
    	}
    	   set_tx_power(BT_HCI_VS_LL_HANDLE_TYPE_ADV,0,txp);
    
    	LOG_INF("Bluetooth initialized");
    	printk("Bluetooth initialized\n");
    
    	k_sem_give(&ble_init_ok);
    
    	if (IS_ENABLED(CONFIG_SETTINGS)) {
    		settings_load();
    	}
    
    	err = bt_nus_init(&nus_cb);
    	if (err) {
    		LOG_ERR("Failed to initialize UART service (err: %d)", err);
    		printk("Failed to initialize UART service (err: %d)\n", err);
    		return;
    	}
    
    	err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd,
    			      ARRAY_SIZE(sd));
    	if (err) {
    		LOG_ERR("Advertising failed to start (err %d)", err);
    		printk("Advertising failed to start (err %d)\n", err);
    		return;
    	}
    
    	for (;;) {
    		dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);
    		k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
    
    		/*ran_data();*/
    		const uint8_t sensor_data ='d';
    		uint16_t length = 1;
    		
    		if (bt_nus_send(NULL, &sensor_data, length)) {
    			
    			LOG_WRN("Failed to send data over BLE connection");
    			printk("Failed to send data over BLE connectionl\n");
    		}
    	}
    }
    
    void ble_write_thread(void)
    {
    	/* Don't go any further until BLE is initialized */
    	k_sem_take(&ble_init_ok, K_FOREVER);
    
    	for (;;) {
    		/* Wait indefinitely for data to be sent over bluetooth */
    		struct uart_data_t *buf = k_fifo_get(&fifo_uart_rx_data,
    						     K_FOREVER);
    
    		//struct uart_user_data *buf ; 
    
    		const uint8_t sensor_data ='d';
    		 uint16_t length = 1;
    		 
    
    		if (bt_nus_send(NULL, &sensor_data, length)) {
    			LOG_WRN("Failed to send data over BLE connection");
    			printk("Failed to send data over BLE connectionl\n");
    		}
    
    		k_free(buf);
    	}
    }
    
    K_THREAD_DEFINE(ble_write_thread_id, STACKSIZE, ble_write_thread, NULL, NULL,
    		NULL, PRIORITY, 0, 0);
    .prj.conf file,
    # Enable the UART driver
    CONFIG_UART_ASYNC_API=y
    CONFIG_NRFX_UARTE0=y
    CONFIG_SERIAL=y
    
    CONFIG_GPIO=y
    
    # Make sure printk is printing to the UART console
    CONFIG_CONSOLE=y
    CONFIG_UART_CONSOLE=y
    
    CONFIG_HEAP_MEM_POOL_SIZE=2048
    
    CONFIG_BT=y
    CONFIG_BT_PERIPHERAL=y
    CONFIG_BT_DEVICE_NAME="Nordic_UART_Service"
    CONFIG_BT_DEVICE_APPEARANCE=833
    CONFIG_BT_MAX_CONN=1
    CONFIG_BT_MAX_PAIRED=1
    
    # Enable the NUS service
    CONFIG_BT_NUS=y
    
    # Enable bonding
    CONFIG_BT_SETTINGS=y
    CONFIG_FLASH=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    CONFIG_FLASH_MAP=y
    CONFIG_NVS=y
    CONFIG_SETTINGS=y
    
    # Enable DK LED and Buttons library
    CONFIG_DK_LIBRARY=y
    
    # This example requires more workqueue stack
    CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
    
    # Config logger
    CONFIG_LOG=y
    CONFIG_USE_SEGGER_RTT=y
    CONFIG_LOG_BACKEND_RTT=y
    CONFIG_LOG_BACKEND_UART=y
    
    CONFIG_ASSERT=y
    CONFIG_BT_NUS_SECURITY_ENABLED=y
    
    #RSSI
    CONFIG_BT_CTLR_ADVANCED_FEATURES=y
    CONFIG_BT_CTLR_CONN_RSSI=y
    
    CONFIG_BT_LL_SOFTDEVICE=n
    CONFIG_BT_LL_SW_SPLIT=y
    CONFIG_BT_CTLR_ADVANCED_FEATURES=y
    CONFIG_BT_CTLR_CONN_RSSI=y
    CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL=y
    Regards
    Karthik Kumar
  • Hi

    It's not as straight forward to see what is wrong here.

    At least it looks like the types of your input variables are correct:

    set_tx_power(BT_HCI_VS_LL_HANDLE_TYPE_ADV,0,txp);

    But since it's giving the same error code as the other function, I would recommend double checking all your variable's types.

    I'm not really sure why the handle here is allowed to be just 0, have you tried giving it the same handle as the other function?

    Also, have you included the definition of BT_HCI_VS_LL_HANDLE_TYPE_ADV in your code?

    Best regards,

    Einar

Reply
  • Hi

    It's not as straight forward to see what is wrong here.

    At least it looks like the types of your input variables are correct:

    set_tx_power(BT_HCI_VS_LL_HANDLE_TYPE_ADV,0,txp);

    But since it's giving the same error code as the other function, I would recommend double checking all your variable's types.

    I'm not really sure why the handle here is allowed to be just 0, have you tried giving it the same handle as the other function?

    Also, have you included the definition of BT_HCI_VS_LL_HANDLE_TYPE_ADV in your code?

    Best regards,

    Einar

Children
No Data
Related