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,

  • Hello  ,

    In order to send data to the ESP32 the frame format explained in this link must be followed. I've modified the main function as follows to send 7 bytes of data within a frame 12 bytes of length:

    void main(void)
    {
    	int err;
    	struct uart_data_t buffer;
    	uint8_t byte_0 = 0x4D /* Type */, byte_1 = 0x0 /* Frame control */, byte_2 = 0x0/* Sequence number */, byte_3 = 0x7 /* Data length */, byte_4 = 'A' /* Data */, byte_5 = 2 /* Checksum */;
    	err = bt_conn_auth_cb_register(&conn_auth_callbacks);
    	if (err) {
    		LOG_ERR("Failed to register authorization callbacks.");
    		return;
    	}
    	printk("bt_conn_auth_cb_register\n");
    
    	err = bt_conn_auth_info_cb_register(&conn_auth_info_callbacks);
    	printk("bt_conn_auth_info_cb_register err: %i\n", err);
    	if (err) {
    		printk("Failed to register authorization info callbacks.\n");
    		return;
    	}
    
    	err = bt_enable(NULL);
    	printk("bt_enable err: %i\n", err);
    	if (err) {
    		LOG_ERR("Bluetooth init failed (err %d)", err);
    		printk("Bluetooth init failed (err %d)\n", err);
    		return;
    	}
    	printk("Bluetooth initialized\n");
    	LOG_INF("Bluetooth initialized");
    
    	if (IS_ENABLED(CONFIG_SETTINGS)) {
    		settings_load();
    	}
    
    	int (*module_init[])(void) = {uart_init, scan_init, nus_client_init};
    	for (size_t i = 0; i < ARRAY_SIZE(module_init); i++) {
    		err = (*module_init[i])();
    		//printk("i: %i\n", i);
    		if (err) {
    			return;
    		}
    	}
    	printk("\n");
    
    	printk("Starting Bluetooth Central UART example\n");
    
    	err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
    	if (err) {
    		LOG_ERR("Scanning failed to start (err %d)", err);
    		printk("Scanning failed to start (err %d)\n", err);
    		return;
    	}
    
    	LOG_INF("Scanning successfully started");
    	printk("Scanning successfully started\n");
    
    	for (;;) {
    		/* Wait indefinitely for data to be sent over Bluetooth */
    		buffer.data[0] = byte_0;
    		buffer.data[1] = byte_1;
    		buffer.data[2] = byte_2;
    		buffer.data[3] = byte_3;
    		buffer.data[4] = byte_4;
    		buffer.data[5] = 'B';
    		buffer.data[6] = 'C';
    		buffer.data[7] = 'D';
    		buffer.data[8] = 'E';
    		buffer.data[9] = 'F';
    		buffer.data[10] = byte_2;
    
    		buffer.data[11] = byte_5;
    		buffer.len = 12;
    
    		if (discovery_service_ok)
    		{
    			err = bt_nus_client_send(&nus_client, buffer.data, buffer.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);
    			}
    
    			if (err) {
    				LOG_WRN("NUS send timeout");
    				printk("NUS send timeout\n");
    			}
    			byte_2++; /* Increment sequence number. */
    		}
    		k_msleep(1000); 
    	}
    }

Related