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?

  • The application must always decide whether to connect to a device or not. In this case a filter for 'a strong signal' is used, but you can filter for whatever metric you want, including the BLE address (MAC). We have an in-house Scanning module, but you can also use the standard blutetooth sub-system APIs as demonstrated in the Bluetooth: Central / Heart-rate Monitor sample. 

    Unknown said:
    Thank you very much. I've reviewed this example and it is supposed to connect with the first device with an strong signal.

    That is true for the zephyr/samples/bluetooth/central sample. The central_hr sample does use a scan filter however. See /samples/bluetooth/central_hr/src/main.c line 163:

    static void start_scan(void)
    {
    	int err;
    
    	/* Use active scanning and disable duplicate filtering to handle any
    	 * devices that might update their advertising data at runtime. */
    	struct bt_le_scan_param scan_param = {
    		.type       = BT_LE_SCAN_TYPE_ACTIVE,
    		.options    = BT_LE_SCAN_OPT_NONE,
    		.interval   = BT_GAP_SCAN_FAST_INTERVAL,
    		.window     = BT_GAP_SCAN_FAST_WINDOW,
    	};
    
    	err = bt_le_scan_start(&scan_param, device_found);
    	if (err) {
    		printk("Scanning failed to start (err %d)\n", err);
    		return;
    	}
    
    	printk("Scanning successfully started\n");
    }

Reply
  • The application must always decide whether to connect to a device or not. In this case a filter for 'a strong signal' is used, but you can filter for whatever metric you want, including the BLE address (MAC). We have an in-house Scanning module, but you can also use the standard blutetooth sub-system APIs as demonstrated in the Bluetooth: Central / Heart-rate Monitor sample. 

    Unknown said:
    Thank you very much. I've reviewed this example and it is supposed to connect with the first device with an strong signal.

    That is true for the zephyr/samples/bluetooth/central sample. The central_hr sample does use a scan filter however. See /samples/bluetooth/central_hr/src/main.c line 163:

    static void start_scan(void)
    {
    	int err;
    
    	/* Use active scanning and disable duplicate filtering to handle any
    	 * devices that might update their advertising data at runtime. */
    	struct bt_le_scan_param scan_param = {
    		.type       = BT_LE_SCAN_TYPE_ACTIVE,
    		.options    = BT_LE_SCAN_OPT_NONE,
    		.interval   = BT_GAP_SCAN_FAST_INTERVAL,
    		.window     = BT_GAP_SCAN_FAST_WINDOW,
    	};
    
    	err = bt_le_scan_start(&scan_param, device_found);
    	if (err) {
    		printk("Scanning failed to start (err %d)\n", err);
    		return;
    	}
    
    	printk("Scanning successfully started\n");
    }

Children
  • Thank you. Now I can use the MAC to decide to which device connect. Which function should be used to send a string of data to the peer device?

  • First you will need a Characteristic to communicate the string through. We recommend the Nordic UART Service (NUS) because it is a simple Service with one Rx Characteristic and one Tx Characteristic, where both transmits data in the form of strings. 

    You can also test the NUS service by connecting the nRF52 to a smartphone that runs the nRF Toolbox app. Then you don't need to implement the NUS service on your peer device before you can verify that the NUS service runs as intended on your nRF52.

  • Hello ,

    Can I use the NUS to send data to an ESP32 module? I've been trying for a while but the Tx function always fails.

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

Related