Để thực hiện các công việc trên, chúng ta cần sử dụng module Bluetooth của ESP32 để tạo kết nối và giao tiếp với điện thoại qua ứng dụng LightBlue.
Đầu tiên, chúng ta cần khai báo tên thiết bị và khởi tạo module Bluetooth bằng hàm esp_bt_controller_init:
c Copy code
#include "esp_bt.h"
#include "esp_bt_main.h"
#define DEVICE_NAME "TenNhom"
void bluetooth_init() {
esp_bt_controller_config_t bt_cfg = ESP_BT_CONTROLLER_CONFIG_DEFAULT();
esp_bt_controller_init(&bt_cfg);
esp_bt_controller_enable(ESP_BT_MODE_BTDM);
esp_bluedroid_init();
esp_bluedroid_enable();
esp_bt_dev_set_device_name(DEVICE_NAME);
esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_NON_DISCOVERABLE);
}
Tiếp theo, chúng ta cần tạo một kết nối Bluetooth và lắng nghe nhận dữ liệu từ điện thoại. Khi nhận được dữ liệu, chúng ta sẽ hiển thị nó lên LCD:
c Copy code
#include "esp_gap_ble_api.h"
#include "esp_gatt_defs.h"
#include "esp_gatts_api.h"
#include "esp_log.h"
#include "esp_system.h"
#include "driver/gpio.h"
#include "driver/i2c.h"
#include "sdkconfig.h"
#define LCD_ADDR 0x27
// Khai báo các thông số LCD
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
switch (event) {
case ESP_GAP_BLE_SCAN_RESULT_EVT:
esp_ble_gap_stop_scanning();
esp_ble_gap_conn_param_t conn_params = {
.interval_min = 80,
.interval_max = 800,
.latency = 0,
.timeout = 4000
};
esp_ble_gap_connect(param->scan_rst.bda, param->scan_rst.ble_addr_type, &conn_params);
break;
case ESP_GAP_BLE_CONNECT_EVT:
esp_ble_gap_set_scan_mode(ESP_BT_NON_CONNECTABLE, ESP_BT_NON_DISCOVERABLE);
break;
case ESP_GAP_BLE_DISCONNECT_EVT:
esp_ble_gap_start_scanning(60);
break;
}
}
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) {
switch (event) {
case ESP_GATTS_REG_EVT:
break;
case ESP_GATTS_CONNECT_EVT:
break;
case ESP_GATTS_READ_EVT:
break;
case ESP_GATTS_WRITE_EVT:
if (param->write.is_prep == false && param->write.len > 0) {
char *str = (char*)malloc(param->write.len + 1);
memcpy(str, param->write.value, param->write.len);
str[param->write.len] = '\0';
lcd_clear();
lcd_printf("ID: %s", str);
free(str);
}
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
break;
}
}
void bluetooth_gap_init() {
esp_ble_gap_register_callback(gap_event_handler);
esp_ble_gatts_register_callback(gatts_event_handler);
esp_ble_gatts_app_register(0);
esp_ble_gap_set_device_name(DEVICE_NAME);
esp_ble_gap_start_scanning(60);
}
void app_main() {
i2c_init();
lcd_init(LCD_ADDR, 16, 2);
lcd_puts("Hello, ESP32!");
bluetooth_init();
bluetooth_gap_init();
}
Chúng ta cần bổ sung các hàm để thực hiện các công việc sau:
Thiết lập kết nối ESP32 và điện thoại.
c Copy code
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
switch (event) {
case ESP_GAP_BLE_SCAN_RESULT_EVT:
esp_ble_gap_stop_scanning();
esp_ble_gap_conn_param_t conn_params = {
.interval_min = 80,
.interval_max = 800,
.latency = 0,
.timeout = 4000
};
esp_ble_gap_connect(param->scan_rst.bda, param->scan_rst.ble_addr_type, &conn_params);
break;
case ESP_GAP_BLE_CONNECT_EVT:
esp_ble_gap_set_scan_mode(ESP_BT_NON_CONNECTABLE, ESP_BT_NON_DISCOVERABLE);
lcd_clear();
lcd_puts("Connected!");
break;
case ESP_GAP_BLE_DISCONNECT_EVT:
esp_ble_gap_start_scanning(60);
lcd_clear();
lcd_puts("Disconnected!");
break;
}
}
void bluetooth_gap_init() {
esp_ble_gap_register_callback(gap_event_handler);
esp_ble_gatts_register_callback(gatts_event_handler);
esp_ble_gatts_app_register(0);
esp_ble_gap_set_device_name(DEVICE_NAME);
esp_ble_gap_start_scanning(60);
}
Đọc và gửi dữ liệu từ điện thoại đến ESP32.
c Copy code
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) {
switch (event) {
case ESP_GATTS_REG_EVT:
break;
case ESP_GATTS_CONNECT_EVT:
break;
case ESP_GATTS_READ_EVT:
break;
case ESP_GATTS_WRITE_EVT:
if (param->write.is_prep == false && param->write.len > 0) {
char *str = (char*)malloc(param->write.len + 1);
memcpy(str, param->write.value, param->write.len);
str[param->write.len] = '\0';
lcd_clear();
lcd_printf("ID: %s", str);
free(str);
}
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
break;
}
}
Sau khi thêm các hàm đó, chúng ta cần chỉnh sửa lại hàm app_main để khởi tạo kết nối Bluetooth và LCD:
c Copy code
void app_main() {
i2c_init();
lcd_init(LCD_ADDR, 16, 2);
lcd_puts("Hello, ESP32!");
bluetooth_init();
bluetooth_gap_init();
}
Trên điện thoại, chúng ta cần tải ứng dụng LightBlue và tìm kiếm tên thiết bị của ESP32. Sau đó, chúng ta chọn dịch vụ BLE và viết dữ liệu vào thuộc tính tương ứng.
Ví dụ, để gửi ID của mỗi thành viên nhóm, chúng ta viết các dữ liệu sau lần lượt vào các thuộc tính:
code Copy code
Thuộc tính | Dữ liệu
-----------------|-------
ID Thành viên 1 | 18120000
ID Thành viên 2 | 18120001
ID Thành viên 3 | 18120002
Khi gửi xong, chúng ta sẽ thấy ID của từng thành viên hiển thị lên LCD của ESP32.