Communicate two microcontrollers via BLE

Hello,

I'm using a nRF52 DK to test BLE communication with another microcontroller. What I need to test is the nRF52 pairing with another brand microcontroller, send a data stream and close the connection.

Could you please tell me which example from the Bluetooth samples list I can use for starting?

Thanks for your attention.

Regards,

Parents Reply Children
  • I have modified the "central_uart" project as follows:

    -In the function 'scan_init' (main.c file) the filtering is done by MAC (ESP32's MAC is 60:55:F9:F5:29:D2):

    static int scan_init(void)
    {
    	int err;
    	struct bt_scan_init_param scan_init = {
    		.connect_if_match = 1,
    	};
    
    	uint8_t esp_add[BT_ADDR_SIZE] = {0x60, 0x55, 0xF9, 0xF5, 0x29, 0xD2};
    	bt_addr_le_t esp32_address = {
    		.type = BT_ADDR_LE_PUBLIC,
    		.a = esp_add,
    	};
    
    	printk("scan_init\n");
    	bt_scan_init(&scan_init);
    	bt_scan_cb_register(&scan_cb);
    
    	//err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_UUID, BT_UUID_NUS_SERVICE);
    	//err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_ADDR, esp32_address);
    	err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_ADDR, /*(const void*)*/esp_add);
    	if (err) {
    		LOG_ERR("Scanning filters cannot be set (err %d)", err);
    		printk("Scanning filters cannot be set (err %d)\n", err);
    		return err;
    	}
    
    	//err = bt_scan_filter_enable(BT_SCAN_UUID_FILTER, false);
    	err = bt_scan_filter_enable(BT_SCAN_ADDR_FILTER, true);
    	
    	if (err) {
    		LOG_ERR("Filters cannot be turned on (err %d)", err);
    		printk("Filters cannot be turned on (err %d)\n", err);
    		return err;
    	}
    
    	LOG_INF("Scan module initialized");
    	printk("Scan module initialized\n");
    	return err;
    }

    -In the 'main' function (main.c) I have modified the loop as follows:

    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_data_t *buf;
    		buf->data[0] = 0;
    		buf->data[1] = 1;
    		buf->data[2] = 2;
    		buf->len = 3;
    
    		err = bt_nus_client_send(&nus_client, buf->data, buf->len);
    		if (err) {
    			LOG_WRN("Failed to send data over BLE connection"
    				"(err %d)", err);
    			//printk("Failed to send data over BLE connection"
    			//	"(err %d)\n", err);
    		}
    
    		err = k_sem_take(&nus_write_sem, NUS_WRITE_TIMEOUT);
    		if (err) {
    			LOG_WRN("NUS send timeout");
    			//printk("NUS send timeout\n");
    		}
    	}

    This time the 'bt_nus_client_send' function returns the -128 error as the nRF52 cannot connect to the ESP32. is there something missing in how I have configured the filtering function?

    -In the nus.h file I have modified the UUID as follows:

    /** @brief UUID of the NUS Service. **/
    #define BT_UUID_NUS_VAL \
    	BT_UUID_128_ENCODE(0x0000FFFF, 0x0000, 0x1000, 0x8000, 0x00805F9B34FB)
    
    /** @brief UUID of the TX Characteristic. **/
    #define BT_UUID_NUS_TX_VAL \
    	BT_UUID_128_ENCODE(0x0000FF01, 0x0000, 0x1000, 0x8000, 0x00805F9B34FB)
    
    /** @brief UUID of the RX Characteristic. **/
    #define BT_UUID_NUS_RX_VAL \
    	BT_UUID_128_ENCODE(0x0000FF02, 0x0000, 0x1000, 0x8000, 0x00805F9B34FB)

  • Where do you set 'nus_client.conn' to point to the connection object of the ESP32 connection?

  • I am not sure which function should set 'nus_client.conn'. Where is this set in the "central_uart" example? I am using this example as template to modify.

  • Excuse me but this is not clear to me. There is no use of NUS library in lesson 3.

    Where do you set 'nus_client.conn' to point to the connection object of the ESP32 connection?

    Do you mean implementing the callback functions?

    /* STEP 2.2 - Implement the callback functions */
    void on_connected(struct bt_conn *conn, uint8_t err)
    {
        if (err) {
            LOG_ERR("Connection error %d", err);
            return;
        }
        LOG_INF("Connected");
        my_conn = bt_conn_ref(conn);
    
    	/* STEP 3.2  Turn the connection status LED on */
        dk_set_led(CONNECTION_STATUS_LED, 1);
    }
    
    void on_disconnected(struct bt_conn *conn, uint8_t reason)
    {
        LOG_INF("Disconnected. Reason %d", reason);
        bt_conn_unref(my_conn);
    
    	/* STEP 3.3  Turn the connection status LED off */
        dk_set_led(CONNECTION_STATUS_LED, 0);
    }
    
    /* STEP 2.1 - Declare the connection_callback structure */
    struct bt_conn_cb connection_callbacks = {
        .connected              = on_connected,
        .disconnected           = on_disconnected,
    };
    

Related