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.

Arduino UNO R4 WiFi Bluetooth table example - hiển thị dữ liệu có cấu trúc qua ble tutorial

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

Hardware Yêu Cầu

1×Arduino UNO R4 WiFi
1×Alternatively, DIYables STEM V4 IoT
1×(Tùy chọn) DIYables STEM V4 IoT
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4
1×(Khuyến nghị) Breadboard Shield for Arduino UNO R4
1×(Khuyến nghị) Enclosure for Arduino UNO R4
1×(Khuyến nghị) Power Splitter for Arduino UNO R4
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V4 IoT Starter Kit (Arduino included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Code Arduino UNO R4 WiFi

Các Bước Nhanh

Làm theo các hướng dẫn từng bước sau:

  • Nếu đây là lần đầu tiên bạn sử dụng Arduino UNO R4 WiFi, hãy tham khảo Arduino UNO R4 - Cài Đặt Phần Mềm.
  • 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.
Arduino UNO R4 diyables Bluetooth 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.
Arduino UNO R4 diyables Bluetooth dependency

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 Configuration const 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 instances DIYables_ArduinoBLE bluetooth(DEVICE_NAME, SERVICE_UUID, TX_UUID, RX_UUID); DIYables_BluetoothServer bluetoothServer(bluetooth); // Create Table app instance DIYables_BluetoothTable bluetoothTable; // Variables for demo data unsigned long lastUpdate = 0; const unsigned long UPDATE_INTERVAL = 1000; // Update every second int counter = 0; void setup() { 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) unsigned long 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"); } void loop() { // 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:
COM6
Send
DIYables Bluetooth - Table Example Waiting for Bluetooth connection...
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Ứ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.
diyables Bluetooth app - home screen with scan nút nhấn
  • 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.
diyables Bluetooth app - home screen with table app

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
diyables Bluetooth app - table screen

Bây giờ hãy nhìn lại Serial Monitor trên Arduino IDE. Bạn sẽ thấy:

COM6
Send
Bluetooth connected! Sending table updates... Temperature: 25.30 °C Humidity: 55.70 %
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Tùy Chỉnh Sáng Tạo - Điều Chỉnh Code Cho Dự Án Của Bạn

Định Nghĩa Cấu Trúc Bảng

// Add rows during setup bluetoothTable.addRow("Temperature", "-- °C"); bluetoothTable.addRow("Humidity", "-- %"); bluetoothTable.addRow("Pressure", "-- hPa"); bluetoothTable.addRow("Status", "Initializing...");

Cập Nhật Giá Trị

// Update by row name bluetoothTable.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 app bluetoothTable.sendTableStructure(); // Handle data request from app bluetoothTable.onDataRequest([]() { bluetoothTable.sendTableStructure(); });

Ví Dụ Lập Trình

Dashboard Trạm Thời Tiết

DIYables_BluetoothTable bluetoothTable(bluetoothServer); void setup() { bluetoothTable.addRow("Temperature", "-- °C"); bluetoothTable.addRow("Humidity", "-- %"); bluetoothTable.addRow("Pressure", "-- hPa"); bluetoothTable.addRow("Wind Speed", "-- m/s"); bluetoothTable.addRow("Rain", "None"); bluetoothTable.onDataRequest([]() { bluetoothTable.sendTableStructure(); }); } void loop() { bluetoothServer.loop(); static unsigned long lastTime = 0; if (millis() - lastTime >= 2000) { lastTime = millis(); bluetoothTable.sendValueUpdate("Temperature", String(readTemp(), 1) + " °C"); bluetoothTable.sendValueUpdate("Humidity", String(readHumidity(), 1) + " %"); bluetoothTable.sendValueUpdate("Pressure", String(readPressure(), 0) + " hPa"); } }

Màn Hình Giám Sát Trạng Thái Hệ Thống

void setup() { bluetoothTable.addRow("Uptime", "0s"); bluetoothTable.addRow("Free RAM", "-- bytes"); bluetoothTable.addRow("WiFi RSSI", "-- dBm"); bluetoothTable.addRow("IP Address", "N/A"); bluetoothTable.addRow("Status", "Starting..."); } void loop() { bluetoothServer.loop(); static unsigned long lastTime = 0; if (millis() - lastTime >= 1000) { lastTime = millis(); unsigned long uptime = millis() / 1000; String uptimeStr = String(uptime / 3600) + "h " + String((uptime % 3600) / 60) + "m " + String(uptime % 60) + "s"; bluetoothTable.sendValueUpdate("Uptime", uptimeStr); bluetoothTable.sendValueUpdate("Status", "Running"); } }

Khắc Phục Sự Cố

Các Vấn Đề Thường Gặp

1. Không thể tìm thấy thiết bị trong ứng dụng

  • Đảm bảo Arduino UNO R4 WiFi được cấp nguồn và sketch đã được tải lên
  • Đảm bảo Bluetooth của điện thoại được bật
  • Trên Android 11 và thấp hơn, cũng bật dịch vụ Location

2. Bảng trống hoặc các hàng không hiển thị

  • Đảm bảo addRow() được gọi trong setup() trước khi kết nối
  • Thực hiện callback onDataRequest để gửi lại cấu trúc
  • Xác minh sendTableStructure() được gọi

3. Giá trị không cập nhật

  • Kiểm tra rằng sendValueUpdate() được gọi trong vòng lặp
  • Xác minh tên hàng khớp chính xác (phân biệt chữ hoa chữ thường)
  • Đảm bảo bluetoothServer.loop() được gọi trong mỗi vòng lặp

4. Tên hàng không khớp

  • Tên hàng phân biệt chữ hoa chữ thường — "Temperature" ≠ "temperature"
  • Sử dụng chỉ số hàng (bắt đầu từ 0) thay thế cho tên hàng

5. Upload thất bại hoặc bo mạch không được nhận diện

  • Cài đặt gói bo mạch Arduino UNO R4 mới nhất qua Board Manager
  • Thử cáp USB hoặc cổng khác

Ý Tưởng Dự Án

  • Dashboard trạm thời tiết
  • Màn hình giám sát tài nguyên hệ thống
  • Hiển thị dữ liệu đa cảm biến
  • Bảng cấu hình thiết bị
  • Bảng trạng thái thiết bị IoT

Các Bước Tiếp Theo

Sau khi nắm vững ví dụ Bluetooth Table, hãy thử:

  1. Bluetooth Plotter - Để hiển thị dữ liệu dạng đồ thị
  2. Bluetooth Monitor - Để tương tác dựa trên văn bản
  3. Bluetooth Temperature - Để hiển thị dạng đồng hồ đo
  4. Multiple Bluetooth Apps - Kết hợp bảng với các ứng dụng khác

Hỗ Trợ

Để được hỗ trợ thêm: