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
  • Hello ,

    Thank you very much. I've reviewed this example and it is supposed to connect with the first device with an strong signal. Is there a way to force the board to connect with the device with a given MAC address?

  • Unknown said:
    Can I use the NUS to send data to an ESP32 module?

    If the ESP32 has a properly configured NUS client, then yes. 

    Unknown said:
    I've been trying for a while but the Tx function always fails.
    1. What exactly have you done? 

    2. What did you expect to observe?

    3. What did you observe?

    As far as I can tell this project is your first BLE project, where all properties of the BLE protocol is completely new to you. I think you should set a lower goal than communicating with an ESP32 for your first project, because an insufficient understanding of core BLE concepts creates problems for you at every step. I suggest that you complete our Bluetooth Low Energy Fundamentals course before continuing any further development.

  • Hello,

    I have followed the course before answering. Below my answers:

    1. 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)

    2. What did you expect to observe? The data received by the ESP32.

    3. What did you observe? I see that the nRF52 can connect to the ESP32 device. Nevertheless, the 'bt_nus_client_send' returns an error value (-128)

    What I'm trying to do is to make the nRF52 work as a central while the ESP32 works as a peripheral using a 128-bit UUID (0000-FFFF-0000-1000-8000-00805f9b34fb). Once both devices are connected I want to send data from the nRF52 to the ESP32. I have tested the NUS example with two nRF52 boards and it works correctly, so I think there is something I'm missing in order to send the data to the ESP32.

  • -128 = ENOTCONN  "Socket is not connected". 

    From subsys/bluetooth/services/nus_client.c:86:

     

    int bt_nus_client_send(struct bt_nus_client *nus_c, const uint8_t *data,
    		       uint16_t len)
    {
    	int err;
    
    	if (!nus_c->conn) {
    		return -ENOTCONN;
    	}
        
        ...
    }

    The bt_nus_client struct provided to bt_nus_client_send does not contain a pointer to a valid connection object. 

    -Edit: 

    Have you implemented the NUS GATT server on the ESP32?

  • I have not implemented NUS GATT server on the ESP32. The ESP32 already has a 128-bit UUID (0000-FFFF-0000-1000-8000-00805f9b34fb) which allows sending/receiving data strings. This works with a mobile application and what I'm trying to do is to make work the nRF52 the same way the mobile application works on the smartphone.

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

Reply
  • 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.

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

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

Related