Arduino UNO R4 WiFi Bluetooth Plotter Example Hướng Dẫn Trực Quan Dữ Liệu Thời Gian Thực qua BLE

Tổng Quan

Ví dụ Bluetooth Plotter truyền dữ liệu thời gian thực đến ứng dụng DIYables Bluetooth STEM để trực quan hóa trực tiếp. Được thiết kế cho Arduino UNO R4 WiFi sử dụng BLE (Bluetooth Low Energy) để vẽ đồ thị các giá trị cảm biến, dạng sóng và dữ liệu đa kênh dưới dạng đồ thị thời gian thực trên smartphone. Lý tưởng cho việc ghi dữ liệu, phân tích tín hiệu, hiệu chỉnh cảm biến và thí nghiệm khoa học.

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. Vì 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 plotter example - hướng dẫn trực quan dữ liệu thời gian thực qua ble

Tính Năng

  • Vẽ Đồ Thị Đa Kênh: Lên đến 6 kênh dữ liệu đồng thời
  • Trục Có Thể Cấu Hình: Tiêu đề tùy chỉnh, nhãn và phạm vi trục Y
  • Nhãn Chú Thích: Đặt tên cho mỗi kênh dữ liệu để dễ hiểu
  • Giới Hạn Mẫu: Đặt số mẫu tối đa để hiển thị
  • Truyền Nhanh: Lên đến 10 cập nhật mỗi giây (khoảng thời gian 100ms)
  • 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

Linh Kiện Cần Thiết

1×Arduino UNO R4 WiFi
1×Alternatively, DIYables STEM V4 IoT
1×(Tùy chọn) DIYables STEM V4 IoT
1×Cáp USB Type-C
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 Thực Hiện

Thực hiện theo các hướng dẫn sau từng bước:

  • 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 bằng cáp USB.
  • Khởi động Arduino IDE trên máy tính.
  • Chọn bo mạch Arduino UNO R4 WiFi và cổng COM phù hợp.
  • Điều hướng đến biểu tượng Libraries ở 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ấp nút Install để cài đặt thư viện.
Arduino UNO R4 thư viện diyables Bluetooth
  • Bạn sẽ được hỏi về việc cài đặt một số thư viện phụ thuộc khác
  • Nhấp nút Install All để cài đặt tất cả các thư viện phụ thuộc.
Arduino UNO R4 thư viện phụ thuộc diyables Bluetooth

Code BLE

  • Trên Arduino IDE, đi đến File Examples DIYables Bluetooth ArduinoBLE_Plotter ví dụ, hoặc sao chép code trên và dán vào editor của Arduino IDE
/* * DIYables Bluetooth Library - Bluetooth Plotter Example * Works with DIYables Bluetooth STEM app on Android and iOS * * This example demonstrates the Bluetooth Plotter feature: * - Real-time data plotting via Bluetooth * - Plot multiple data series simultaneously * - Configurable plot settings * * 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 plot * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */ #include <DIYables_BluetoothServer.h> #include <DIYables_BluetoothPlotter.h> #include <platforms/DIYables_ArduinoBLE.h> // BLE Configuration const char* DEVICE_NAME = "Arduino_Plotter"; 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 Plotter app instance DIYables_BluetoothPlotter bluetoothPlotter; // Variables for generating sample data unsigned long lastPlotTime = 0; const unsigned long PLOT_INTERVAL = 100; // Plot every 100ms float phase = 0; void setup() { Serial.begin(9600); while (!Serial); Serial.println("DIYables Bluetooth - Plotter Example"); // Initialize Bluetooth server with platform-specific implementation bluetoothServer.begin(); // Add plotter app to server bluetoothServer.addApp(&bluetoothPlotter); // Configure plotter settings bluetoothPlotter.setPlotTitle("Sensor Data"); bluetoothPlotter.setAxisLabels("Time", "Value"); bluetoothPlotter.setYAxisRange(-15, 30); // Fixed range bluetoothPlotter.setMaxSamples(100); // Show last 100 samples bluetoothPlotter.setLegendLabels("Sine", "Cosine", "Tangent"); // Custom legend labels // Uncomment to enable auto-scaling instead of fixed range: // bluetoothPlotter.enableAutoScale(true); // Set up connection event callbacks bluetoothServer.setOnConnected([]() { Serial.println("Bluetooth connected!"); }); bluetoothServer.setOnDisconnected([]() { Serial.println("Bluetooth disconnected!"); }); // Optional: Handle requests for current plot data (when app loads) bluetoothPlotter.onDataRequest([]() { // Send initial data when app requests it Serial.println("App requested plot data"); // Could send recent historical data here if needed }); Serial.println("Waiting for Bluetooth connection..."); } void loop() { // Handle Bluetooth server communications bluetoothServer.loop(); // Send plot data at regular intervals if (millis() - lastPlotTime >= PLOT_INTERVAL) { lastPlotTime = millis(); // Generate sample data (sine and cosine waves) float sine = sin(phase); float cosine = cos(phase); float tangent = tan(phase) * 0.3; // Scaled down // Send data to Bluetooth plotter (multiple series) // You can send 1 to 4 values, or use array for more bluetoothPlotter.send(sine, cosine, tangent); // Also print to Serial for Arduino IDE Serial Plotter comparison // Format: value1 value2 value3 (space-separated for Serial Plotter) Serial.print(sine, 2); Serial.print(" "); Serial.print(cosine, 2); Serial.print(" "); Serial.println(tangent, 2); // Alternative methods: // bluetoothPlotter.send(sine); // Single value // bluetoothPlotter.send(sine, cosine); // Two values // float values[] = {sine, cosine, tangent}; // bluetoothPlotter.send(values, 3); // Array of values phase += 0.1; if (phase > 2 * PI) { phase = 0; } // TODO: Replace sample data with your actual sensor readings // Examples: // - Temperature sensor: bluetoothPlotter.send(temperature); // - Accelerometer: bluetoothPlotter.send(accelX, accelY, accelZ); // - Multiple sensors: bluetoothPlotter.send(sensor1, sensor2, sensor3); } delay(10); }
  • Nhấp 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ó trông như bên dưới:
COM6
Send
DIYables Bluetooth - Plotter 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 smartphone: Android | iOS

Lưu ý: Ứng dụng DIYables Bluetooth hỗ trợ cả BLE và Classic Bluetooth trên Android, và BLE trên iOS. Vì 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 cho 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 các quyền sau:
    • Quyền Nearby Devices (Android 12+) / quyền Bluetooth (iOS) - cần thiết để quét và kết nối các thiết bị Bluetooth
    • Quyền Location (chỉ Android 11 trở xuống) - được yêu cầu bởi 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
  • Trên màn hình chính, nhấn nút Connect. Ứng dụng sẽ quét các thiết bị BLE.
diyables Bluetooth app - màn hình chính với nút scan
  • Tìm và nhấn "Arduino_Plotter" trong kết quả quét để kết nối.
  • Khi đã kết nối, ứng dụng tự động quay về màn hình chính. Chọn ứng dụng Plotter từ menu ứng dụng.
diyables Bluetooth app - màn hình chính với Ứng dụng plotter

Lưu ý: Bạn có thể nhấn biểu tượng cài đặt trên màn hình chính để ẩn/hiện ứ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 DIYables Bluetooth App.

  • Bạn sẽ thấy đồ thị thời gian thực của các dạng sóng Sine, Cosine và Tangent
diyables Bluetooth app - màn hình plotter

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

COM6
Send
Bluetooth connected! Sending plot data... Sine: 0.00, Cosine: 10.00, Tangent: 0.00
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

Cấu Hình Giao Diện Đồ Thị

// Đặt tiêu đề đồ thị bluetoothPlotter.setPlotTitle("My Sensor Data"); // Đặt nhãn trục bluetoothPlotter.setAxisLabels("Time", "Value"); // Đặt phạm vi trục Y bluetoothPlotter.setYAxisRange(-100, 100); // Đặt số mẫu tối đa để hiển thị bluetoothPlotter.setMaxSamples(200); // Đặt nhãn chú thích cho mỗi kênh bluetoothPlotter.setLegendLabels("Temperature", "Humidity", "Pressure");

Gửi Điểm Dữ Liệu

// Gửi một giá trị đơn bluetoothPlotter.send(sensorValue); // Gửi hai giá trị bluetoothPlotter.send(temperature, humidity); // Gửi ba giá trị bluetoothPlotter.send(value1, value2, value3); // Gửi lên đến sáu giá trị bluetoothPlotter.send(ch1, ch2, ch3, ch4, ch5, ch6);

Xử Lý Yêu Cầu Dữ Liệu

bluetoothPlotter.onDataRequest([]() { Serial.println("App requested data"); // Ứng dụng vừa kết nối hoặc cần cấu hình ban đầu });

Ví Dụ Lập Trình

Bộ Ghi Nhiệt Độ và Độ Ẩm

DIYables_BluetoothPlotter bluetoothPlotter(bluetoothServer); void setup() { bluetoothPlotter.setPlotTitle("Environment Monitor"); bluetoothPlotter.setAxisLabels("Time", "Value"); bluetoothPlotter.setYAxisRange(0, 100); bluetoothPlotter.setMaxSamples(100); bluetoothPlotter.setLegendLabels("Temp (°C)", "Humidity (%)"); } void loop() { bluetoothServer.loop(); static unsigned long lastTime = 0; if (millis() - lastTime >= 1000) { lastTime = millis(); float temp = readTemperature(); float humidity = readHumidity(); bluetoothPlotter.send(temp, humidity); } }

Đồ Thị Đầu Vào Analog

void loop() { bluetoothServer.loop(); static unsigned long lastTime = 0; if (millis() - lastTime >= 100) { lastTime = millis(); int a0 = analogRead(A0); int a1 = analogRead(A1); int a2 = analogRead(A2); bluetoothPlotter.send(a0, a1, a2); } }

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 trở xuống, cũng cần bật dịch vụ Vị trí

2. Đồ thị không cập nhật

  • Xác minh dữ liệu đang được gửi trong hàm loop()
  • Kiểm tra rằng bluetoothServer.loop() được gọi
  • Đảm bảo ứng dụng đang ở màn hình Plotter

3. Dữ liệu trông sai hoặc nhiễu

  • Kiểm tra kết nối cảm biến và các giá trị đọc được
  • Xác minh phạm vi trục Y khớp với phạm vi dữ liệu của bạn
  • Cân nhắc thêm làm mượn/lọc dữ liệu

4. Tiêu đề đồ thị hoặc nhãn không xuất hiện

  • Đặt cấu hình đồ thị trong setup() trước khi kết nối
  • Đảm bảo callback onDataRequest gửi lại cấu hình nếu cần

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

  • 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

  • Giám sát môi trường (nhiệt độ, độ ẩm, áp suất)
  • Trực quan hóa dữ liệu gia tốc kế/con quay hồi chuyển
  • Công cụ hiệu chỉnh cảm biến analog
  • Phân tích và so sánh tín hiệu
  • Bộ ghi dữ liệu thí nghiệm khoa học

Bước Tiếp Theo

Sau khi thành thạo ví dụ Bluetooth Plotter, hãy thử:

  1. Bluetooth Table - Để hiển thị dữ liệu có cấu trúc
  2. Bluetooth Monitor - Để hiển thị dữ liệu dạng văn bản
  3. Bluetooth Temperature - Để hiển thị nhiệt độ kiểu đồng hồ đo
  4. Nhiều Ứng Dụng Bluetooth - Kết hợp plotter với các ứng dụng khác

Hỗ Trợ

Để được trợ giúp thêm: