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,

  • Unknown said:
    The ESP32 already has a 128-bit UUID (0000-FFFF-0000-1000-8000-00805f9b34fb) which allows sending/receiving data strings.

    The Nordic Uart Service uses two characteristics though, rx and tx. If you're only going to use one characteristic for both sending and receiving data then the NUS is not really suited for this task. You should rather implement the service you're already using on the ESP32 in on the nRF52, or modify the service running on the ESP32 to fit the NUS. You can change the UUID's of the NUS to whatever you want btw.

  • What exactly have you done? I configured the NUS service on the 'central' example and added a call to the 'bt_nus_client_send' function to permanently send data after the device has been connected to the ESP32. I have also modified the NUS 128-bit UUID to the UUID already set in the ESP32 device (0000-FFFF-0000-1000-8000-00805f9b34fb)

    I have already modified the UUID of the NUS but I get the error message:

    -128 = ENOTCONN  "Socket is not connected".

    Could it be that there is something else that needs to be configured in the 'prj.conf' file?

  • Unknown said:
    Could it be that there is something else that needs to be configured in the 'prj.conf' file?

    I don't know. 

    Can you share your code, relevant to the bt_nus_client_send error?

  • 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?

Related