Arduino UNO R4 WiFi Bluetooth Table Example Hiển Thị Dữ Liệu Có Cấu Trúc qua BLE Tutorial
Tổng Quan
Ví dụ Bluetooth Table cung cấp hiển thị dữ liệu có cấu trúc thông qua ứng dụng DIYables Bluetooth STEM. Được thiết kế cho Arduino UNO R4 WiFi sử dụng BLE (Bluetooth Low Energy) để hiển thị các giá trị cảm biến, trạng thái hệ thống và bất kỳ dữ liệu nào dưới định dạng bảng gọn gàng trên điện thoại thông minh của bạn. Hỗ trợ các hàng có tên với cập nhật giá trị thời gian thực. Hoàn hảo cho dashboard, giám sát hệ thống và hiển thị đa cảm biến.
Lưu ý: Arduino UNO R4 WiFi chỉ hỗ trợ BLE (Bluetooth Low Energy). Nó không hỗ trợ Classic Bluetooth. Ứng dụng DIYables Bluetooth hỗ trợ cả BLE và Classic Bluetooth trên Android, và BLE trên iOS. Do bo mạch này sử dụng BLE, ứng dụng hoạt động trên cả Android và iOS.
Tính Năng
Hàng Có Tên: Định nghĩa các hàng với nhãn (ví dụ: "Temperature", "Humidity")
Cập Nhật Thời Gian Thực: Gửi cập nhật giá trị từng mục một cách hiệu quả
Đồng Bộ Cấu Trúc: Tự động gửi cấu trúc bảng khi kết nối
Dữ Liệu Linh Hoạt: Hiển thị bất kỳ dữ liệu chuỗi hoặc số nào
Lên Đến 20 Hàng: Hỗ trợ nhiều trường dữ liệu
Hoạt Động trên Android & iOS: BLE được hỗ trợ trên cả hai nền tảng
Không Cần Ghép Nối: BLE tự động kết nối mà không cần ghép nối thủ công
Kết nối bo mạch Arduino UNO R4 WiFi với máy tính của bạn bằng cáp USB.
Khởi động Arduino IDE trên máy tính của bạn.
Chọn bo mạch Arduino UNO R4 WiFi và cổng COM thích hợp.
Điều hướng đến biểu tượng Libraries trên thanh bên trái của Arduino IDE.
Tìm kiếm "DIYables Bluetooth", sau đó tìm thư viện DIYables Bluetooth của DIYables
Nhấn nút Install để cài đặt thư viện.
Bạn sẽ được yêu cầu cài đặt một số thư viện phụ thuộc khác
Nhấn nút Install All để cài đặt tất cả thư viện phụ thuộc.
Code BLE
Trên Arduino IDE, Đi đến File Examples DIYables Bluetooth ArduinoBLE_Table example, hoặc sao chép code trên và dán vào editor của Arduino IDE
/* * DIYables Bluetooth Library - Bluetooth Table Example * Works with DIYables Bluetooth STEM app on Android and iOS * * This example demonstrates the Bluetooth Table feature: * - Display structured data in a two-column table * - Real-time value updates for each row * - Perfect for sensor dashboards and status displays * * Compatible Boards: * - Arduino UNO R4 WiFi * - Arduino Nano 33 BLE / BLE Sense * - Arduino Nano 33 IoT * - Arduino MKR WiFi 1010 * - Arduino Nano RP2040 Connect * - Any board supporting the ArduinoBLE library * * Setup: * 1. Upload the sketch to your Arduino * 2. Open Serial Monitor to see connection status * 3. Use DIYables Bluetooth App to connect and view the table * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */#include <DIYables_BluetoothServer.h>#include <DIYables_BluetoothTable.h>#include <platforms/DIYables_ArduinoBLE.h>// BLE Configurationconst char* DEVICE_NAME = "Arduino_Table";const char* SERVICE_UUID = "19B10000-E8F2-537E-4F6C-D104768A1214";const char* TX_UUID = "19B10001-E8F2-537E-4F6C-D104768A1214";const char* RX_UUID = "19B10002-E8F2-537E-4F6C-D104768A1214";// Create Bluetooth instancesDIYables_ArduinoBLE bluetooth(DEVICE_NAME, SERVICE_UUID, TX_UUID, RX_UUID);DIYables_BluetoothServer bluetoothServer(bluetooth);// Create Table app instanceDIYables_BluetoothTable bluetoothTable;// Variables for demo dataunsignedlong lastUpdate = 0;constunsignedlong UPDATE_INTERVAL = 1000; // Update every secondint counter = 0;voidsetup() {Serial.begin(9600);while (!Serial);Serial.println("DIYables Bluetooth - Table Example");// Initialize Bluetooth server with platform-specific implementation bluetoothServer.begin();// Add table app to server bluetoothServer.addApp(&bluetoothTable);// Define table structure (add rows with attribute names) bluetoothTable.addRow("Temperature"); bluetoothTable.addRow("Humidity"); bluetoothTable.addRow("Pressure"); bluetoothTable.addRow("Counter"); bluetoothTable.addRow("Uptime"); bluetoothTable.addRow("Free Memory"); bluetoothTable.addRow("Status");Serial.print("Table rows defined: ");Serial.println(bluetoothTable.getRowCount());// Set up connection event callbacks bluetoothServer.setOnConnected([]() {Serial.println("Bluetooth connected!");// Send table structure bluetoothTable.sendTableStructure();// Send initial values updateTableValues(); }); bluetoothServer.setOnDisconnected([]() {Serial.println("Bluetooth disconnected!"); });// Optional: Handle requests for table data bluetoothTable.onDataRequest([]() {Serial.println("App requested table data"); bluetoothTable.sendTableStructure(); updateTableValues(); });Serial.println("Waiting for Bluetooth connection...");}void updateTableValues() {// TODO: Replace with actual sensor readings// Simulated temperature (20-30°C)float temperature = 20.0 + random(0, 100) / 10.0; bluetoothTable.sendValueUpdate("Temperature", String(temperature, 1) + " °C");// Simulated humidity (40-60%)int humidity = 40 + random(0, 21); bluetoothTable.sendValueUpdate("Humidity", String(humidity) + " %");// Simulated pressure (1000-1020 hPa)int pressure = 1000 + random(0, 21); bluetoothTable.sendValueUpdate("Pressure", String(pressure) + " hPa");// Counter value bluetoothTable.sendValueUpdate("Counter", String(counter)); counter++;// Uptime (in seconds)unsignedlong uptime = millis() / 1000;String uptimeStr = String(uptime / 3600) + "h " + String((uptime % 3600) / 60) + "m " + String(uptime % 60) + "s"; bluetoothTable.sendValueUpdate("Uptime", uptimeStr);// Free memory (simulated)int freeMemory = 2048 - random(0, 512); bluetoothTable.sendValueUpdate("Free Memory", String(freeMemory) + " bytes");// Status bluetoothTable.sendValueUpdate("Status", counter % 2 == 0 ? "Running" : "Active");// Alternative: Update by index instead of attribute name// bluetoothTable.sendValueUpdate(0, String(temperature, 1) + " °C");// bluetoothTable.sendValueUpdate(1, String(humidity) + " %");Serial.println("Table values updated");}voidloop() {// Handle Bluetooth server communications bluetoothServer.loop();// Update table values periodically (only when connected)if (bluetooth.isConnected() && millis() - lastUpdate >= UPDATE_INTERVAL) { lastUpdate = millis(); updateTableValues(); }delay(10);}
Nhấn nút Upload trên Arduino IDE để tải code lên Arduino UNO R4 WiFi
Mở Serial Monitor
Kiểm tra kết quả trên Serial Monitor. Nó sẽ trông như thế này:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
DIYables Bluetooth - Table Example
Waiting for Bluetooth connection...
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2
Ứng Dụng Di Động
Cài đặt ứng dụng DIYables Bluetooth trên điện thoại thông minh của bạn: Android | iOS
Lưu ý: Ứng dụng DIYables Bluetooth hỗ trợ cả BLE và Classic Bluetooth trên Android, và BLE trên iOS. Do Arduino UNO R4 WiFi sử dụng BLE, ứng dụng hoạt động trên cả Android và iOS. Không cần ghép nối thủ công với BLE — chỉ cần quét và kết nối.
Mở ứng dụng DIYables Bluetooth
Khi mở ứng dụng lần đầu tiên, nó sẽ yêu cầu quyền. Vui lòng cấp những quyền sau:
Quyền Nearby Devices (Android 12+) / quyền Bluetooth (iOS) - cần thiết để quét và kết nối với thiết bị Bluetooth
Quyền Location (chỉ Android 11 và thấp hơn) - cần thiết cho các phiên bản Android cũ để quét thiết bị BLE
Đảm bảo Bluetooth được bật trên điện thoại của bạn
Trên màn hình chính, nhấn nút Connect. Ứng dụng sẽ quét tìm thiết bị BLE.
Tìm và nhấn "Arduino_Table" trong kết quả quét để kết nối.
Sau khi kết nối, ứng dụng tự động quay về màn hình chính. Chọn ứng dụng Table từ menu ứng dụng.
Lưu ý: Bạn có thể nhấn biểu tượng settings trên màn hình chính để ẩn/hiện các ứng dụng trên màn hình chính. Để biết thêm chi tiết, xem Hướng dẫn sử dụng ứng dụng DIYables Bluetooth.
Bạn sẽ thấy một bảng với các hàng hiển thị Temperature, Humidity, Pressure, Counter, Uptime, Free Memory và Status — tất cả đều cập nhật theo thời gian thực
Bây giờ hãy nhìn lại Serial Monitor trên Arduino IDE. Bạn sẽ thấy:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
Bluetooth connected!
Sending table updates...
Temperature: 25.30 °C
Humidity: 55.70 %
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2
Tùy Chỉnh Sáng Tạo - Điều Chỉnh Code Cho Dự Án Của Bạn
// Update by row namebluetoothTable.sendValueUpdate("Temperature", "25.5 °C");bluetoothTable.sendValueUpdate("Status", "Running");// Update by row index (0-based)bluetoothTable.sendValueUpdate(0, "25.5 °C");bluetoothTable.sendValueUpdate(3, "Running");
Gửi Cấu Trúc Bảng
// Re-send the full table structure to the appbluetoothTable.sendTableStructure();// Handle data request from appbluetoothTable.onDataRequest([]() { bluetoothTable.sendTableStructure();});