How to send and receive data using bluetooth with esp32

How to send and receive data using bluetooth with esp32

Communicating using Bluetooth with embedded system hardware is very useful where no wifi available and it also saves the cost of products. here is a short example of how esp32 which has inbuilt Bluetooth and BLE can communication with Android mobile applications to exchange data via Bluetooth.

#include  "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_bt_api.h"
#include "esp_bt_device.h"
#include "esp_spp_api.h"

static bool connected = false;
static esp_spp_cb_param_t spp_param;
static esp_spp_cb_t spp_cb = {
? ? .connect_cb = spp_connect_callback,
? ? .disconnect_cb = spp_disconnect_callback,
? ? .data_ind_cb = spp_data_callback,
};


// callback function for when a device is connected
void spp_connect_callback(esp_spp_cb_param_t *param) {
? ? connected = true;
? ? spp_param = *param;
? ? printf("Connected to %s\n", param->connect.rem_bda);
}


// callback function for when a device is disconnected
void spp_disconnect_callback(esp_spp_cb_param_t *param) {
? ? connected = false;
? ? printf("Disconnected from %s\n", param->disconnect.rem_bda);
}


// callback function for when data is received
void spp_data_callback(esp_spp_cb_param_t *param) {
? ? printf("Received data: %s\n", param->data_ind.data);
}


void send_data(char *data) {
? ? if (connected) {
? ? ? ? esp_spp_write(spp_param.connect.handle, strlen(data), (uint8_t*)data);
? ? ? ? printf("Sent data: %s\n", data);
? ? } else {
? ? ? ? printf("Not connected, can't send data\n");
? ? }
}


void app_main() {
? ? // initialize the Bluetooth stack
? ? esp_bt_controller_init();
? ? esp_bt_controller_enable(ESP_BT_MODE_BTDM);
? ? // set the device name
? ? esp_bt_dev_set_device_name("ESP32_Device");
? ? // start scanning for devices
? ? esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 5, 0);
? ? // register the callback function
? ? esp_bt_gap_register_callback(ESP_BT_GAP_DISC_RES_EVT, &device_found_callback);
? ? esp_spp_register_callback(&spp_cb);
? ? esp_spp_init(ESP_SPP_MODE_CB);
}

"        

This code is written in embedded C for the ESP32 microcontroller using the ESP-IDF framework. The code is used to establish a Bluetooth connection between the ESP32 and an Android mobile application, and to send and receive data over this connection.

The code starts by including several header files for the ESP-IDF Bluetooth stack and the Serial Port Profile (SPP) API.

Next, the code defines several global variables and callback functions. The connected variable is used to keep track of whether the ESP32 is currently connected to a device. The spp_param variable is used to store the parameters passed to the callback functions. The spp_cb variable is an instance of the esp_spp_cb_t struct, which contains pointers to the callback functions.

The spp_connect_callback() function is called when a device is connected to the ESP32. It sets the connected variable to true, stores the parameters passed to the function in the spp_param variable, and prints a message indicating that a connection has been established.

The spp_disconnect_callback() function is called when a device is disconnected from the ESP32. It sets the connected variable to false and prints a message indicating that the connection has been lost.

The spp_data_callback() function is called when data is received from a connected device. It prints the received data to the console.

The send_data() function is used to send data to a connected device. It checks whether a connection is currently established, and if so, it uses the esp_spp_write() function to send the data and prints a message indicating that the data has been sent.

In the app_main() function, the code initializes the Bluetooth stack.


Thanks.

Like and subscribe for more updates like this.

要查看或添加评论,请登录

Pawan Meena的更多文章

社区洞察

其他会员也浏览了