Arduino UNO R4 WiFi Bluetooth Monitor Hướng dẫn Wireless Serial Monitor qua BLE

Tổng Quan

Ví dụ Bluetooth Monitor cung cấp wireless serial monitor có thể truy cập qua ứng dụng DIYables Bluetooth STEM. Được thiết kế cho Arduino UNO R4 WiFi sử dụng BLE (Bluetooth Low Energy) để stream tin nhắn trạng thái thời gian thực, debug output và dữ liệu cảm biến không dây đến smartphone của bạn. Cũng có thể nhận lệnh văn bản từ ứng dụng. Hoàn hảo cho wireless debugging, remote monitoring và system logging.

Lưu ý: Arduino UNO R4 WiFi chỉ hỗ trợ BLE (Bluetooth Low Energy). Nó không hỗ trợ Classic Bluetooth. DIYables Bluetooth App 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 monitor example - wireless serial monitor via ble tutorial

Tính Năng

  • Wireless Serial Monitor: Stream tin nhắn văn bản đến điện thoại của bạn
  • Two-Way Communication: Gửi lệnh từ ứng dụng đến Arduino
  • Real-Time Streaming: Continuous output giống như Serial Monitor
  • Command Handling: Xử lý lệnh văn bản từ ứng dụng
  • Hoạt động trên Android & iOS: BLE được hỗ trợ trên cả hai nền tảng
  • Không Cần Pairing: BLE tự động kết nối mà không cần pairing thủ công
  • Tiết Kiệm Pin: BLE tiêu thụ ít năng lượng hơn Classic Bluetooth

Phần Cứng 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 Nhanh

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

  • Nếu đây là lần đầu 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 của bạn.
  • Chọn bo mạch Arduino UNO R4 WiFi và COM port thích 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ấ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ả các thư viện phụ thuộc.
Arduino UNO R4 diyables Bluetooth dependency

BLE Code

  • Trong Arduino IDE, đi đến File Examples DIYables Bluetooth ArduinoBLE_Monitor example, hoặc sao chép code trên và dán vào editor của Arduino IDE
/* * DIYables Bluetooth Library - Bluetooth Monitor Example * Works with DIYables Bluetooth STEM app on Android and iOS * * This example demonstrates the Bluetooth Monitor feature: * - Send real-time status messages to the mobile app * - Display system information and sensor readings * - Receive and process commands from the app * - Perfect for debugging and system monitoring * * 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 monitor output * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */ #include <DIYables_BluetoothServer.h> #include <DIYables_BluetoothMonitor.h> #include <platforms/DIYables_ArduinoBLE.h> // BLE Configuration const char* DEVICE_NAME = "Arduino_Monitor"; 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 Monitor app instance DIYables_BluetoothMonitor bluetoothMonitor; // Variables for demo unsigned long lastUpdate = 0; const unsigned long UPDATE_INTERVAL = 3000; // Send update every 3 seconds int messageCount = 0; bool ledState = false; void setup() { Serial.begin(9600); while (!Serial); Serial.println("DIYables Bluetooth - Monitor Example"); // Initialize built-in LED pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, LOW); // Initialize Bluetooth server with platform-specific implementation bluetoothServer.begin(); // Add monitor app to server bluetoothServer.addApp(&bluetoothMonitor); // Set up connection event callbacks bluetoothServer.setOnConnected([]() { Serial.println("Bluetooth connected!"); bluetoothMonitor.send("=== Arduino Monitor Connected ==="); bluetoothMonitor.send("System Ready"); bluetoothMonitor.send("Type HELP for available commands"); bluetoothMonitor.send(""); }); bluetoothServer.setOnDisconnected([]() { Serial.println("Bluetooth disconnected!"); }); // Set up message handler for incoming commands bluetoothMonitor.onMonitorMessage([](const String& message) { Serial.print("Received command: "); Serial.println(message); handleCommand(message); }); Serial.println("Waiting for Bluetooth connection..."); } void handleCommand(const String& cmd) { if (cmd == "HELP") { bluetoothMonitor.send("Available Commands:"); bluetoothMonitor.send(" LED_ON - Turn LED on"); bluetoothMonitor.send(" LED_OFF - Turn LED off"); bluetoothMonitor.send(" STATUS - Show system status"); bluetoothMonitor.send(" CLEAR - Clear monitor (if supported)"); bluetoothMonitor.send(" HELP - Show this help"); } else if (cmd == "LED_ON") { digitalWrite(LED_BUILTIN, HIGH); ledState = true; bluetoothMonitor.send("✓ LED turned ON"); } else if (cmd == "LED_OFF") { digitalWrite(LED_BUILTIN, LOW); ledState = false; bluetoothMonitor.send("✓ LED turned OFF"); } else if (cmd == "STATUS") { showStatus(); } else if (cmd == "CLEAR") { // App should handle clearing the display bluetoothMonitor.send(""); } else { bluetoothMonitor.send("✗ Unknown command: " + cmd); bluetoothMonitor.send("Type HELP for available commands"); } } void showStatus() { bluetoothMonitor.send("=== System Status ==="); // LED Status bluetoothMonitor.send("LED State: " + String(ledState ? "ON" : "OFF")); // Uptime unsigned long uptime = millis() / 1000; bluetoothMonitor.send("Uptime: " + String(uptime / 3600) + "h " + String((uptime % 3600) / 60) + "m " + String(uptime % 60) + "s"); // Free Memory (approximate for AVR) #ifdef __AVR__ extern int __heap_start, *__brkval; int freeMemory = (int) &freeMemory - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); bluetoothMonitor.send("Free Memory: " + String(freeMemory) + " bytes"); #endif // Messages sent bluetoothMonitor.send("Messages Sent: " + String(messageCount)); bluetoothMonitor.send("===================="); } void sendPeriodicUpdate() { messageCount++; // Example of different message types if (messageCount % 3 == 0) { // Send status update bluetoothMonitor.send("[INFO] Heartbeat #" + String(messageCount)); } else if (messageCount % 5 == 0) { // Send simulated sensor reading int sensorValue = random(0, 1024); bluetoothMonitor.send("[SENSOR] Reading: " + String(sensorValue) + " (random demo value)"); } else { // Send timestamp bluetoothMonitor.send("[TIME] Uptime: " + String(millis() / 1000) + "s"); } // Optionally log to Serial as well Serial.print("Sent update #"); Serial.println(messageCount); } void loop() { // Handle Bluetooth server communications bluetoothServer.loop(); // Send periodic updates (only when connected) if (bluetooth.isConnected() && millis() - lastUpdate >= UPDATE_INTERVAL) { lastUpdate = millis(); sendPeriodicUpdate(); } delay(10); }
  • Nhấn nút Upload trên Arduino IDE để upload code lên Arduino UNO R4 WiFi
  • Mở Serial Monitor
  • Kiểm tra kết quả trên Serial Monitor. Nó sẽ giống như sau:
COM6
Send
DIYables Bluetooth - Monitor Example Waiting for Bluetooth connection...
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Ứng Dụng Di Động

  • Cài đặt DIYables Bluetooth App trên smartphone: Android | iOS

Lưu ý: DIYables Bluetooth App 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 pairing thủ công cho BLE — chỉ cần scan và kết nối.

  • Mở DIYables Bluetooth App
  • 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 để scan và kết nối với thiết bị Bluetooth
    • Quyền Location (chỉ Android 11 trở xuống) - cần thiết cho các phiên bản Android cũ để scan 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ẽ scan các thiết bị BLE.
diyables Bluetooth app - home screen with scan nút nhấn
  • Tìm và nhấn "Arduino_Monitor" trong kết quả scan để 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 Monitor từ menu ứng dụng.
diyables Bluetooth app - home screen with monitor app

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 DIYables Bluetooth App User Manual.

  • Bạn sẽ thấy các tin nhắn trạng thái streaming trong màn hình monitor
  • Nhập LED_ON vào trường input và nhấn Send — LED built-in trên Arduino UNO R4 WiFi sẽ bật, và monitor sẽ hiển thị tin nhắn xác nhận
diyables Bluetooth app - monitor screen

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

COM6
Send
Bluetooth connected! Sent update #1 Sent update #2 Sent update #3
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

Gửi Tin Nhắn

// Gửi tin nhắn văn bản đến ứng dụng bluetoothMonitor.send("System started"); bluetoothMonitor.send("Temperature: " + String(temp, 1) + " °C"); bluetoothMonitor.send("[ERROR] Sensor disconnected!");

Xử Lý Lệnh Đến

Sử dụng callback onMonitorMessage() để nhận lệnh được nhập trong ứng dụng Monitor và phản ứng với chúng:

bluetoothMonitor.onMonitorMessage([](const String& message) { Serial.print("Received: "); Serial.println(message); if (message == "HELP") { bluetoothMonitor.send("Commands: LED_ON, LED_OFF, STATUS, HELP"); } else if (message == "LED_ON") { digitalWrite(LED_BUILTIN, HIGH); bluetoothMonitor.send("LED turned ON"); } else if (message == "LED_OFF") { digitalWrite(LED_BUILTIN, LOW); bluetoothMonitor.send("LED turned OFF"); } else if (message == "STATUS") { bluetoothMonitor.send("Uptime: " + String(millis() / 1000) + "s"); bluetoothMonitor.send("LED: " + String(digitalRead(LED_BUILTIN) ? "ON" : "OFF")); } else { bluetoothMonitor.send("Unknown command: " + message); } });

Bạn có thể thêm bao nhiêu lệnh tùy chỉnh tùy ý bằng cách thêm nhiều khối else if. Ví dụ, thêm RELAY_ON / RELAY_OFF để điều khiển relay, hoặc READ để kích hoạt đọc cảm biến — bất kỳ từ nào bạn nhập trong ứng dụng đều trở thành một lệnh.

Ví Dụ Lập Trình

Sensor Status Streaming

void loop() { bluetoothServer.loop(); static unsigned long lastUpdate = 0; if (bluetooth.isConnected() && millis() - lastUpdate >= 3000) { lastUpdate = millis(); int light = analogRead(A0); float voltage = analogRead(A1) * 5.0 / 1023.0; bluetoothMonitor.send("[SENSOR] Light: " + String(map(light, 0, 1023, 0, 100)) + "%"); bluetoothMonitor.send("[SENSOR] Voltage: " + String(voltage, 2) + "V"); bluetoothMonitor.send("[INFO] Uptime: " + String(millis() / 1000) + "s"); } delay(10); }

Event-Based Logging

const int BUTTON_PIN = 7; int lastButtonState = HIGH; void loop() { bluetoothServer.loop(); int buttonState = digitalRead(BUTTON_PIN); if (buttonState != lastButtonState) { lastButtonState = buttonState; if (buttonState == LOW) { bluetoothMonitor.send("[EVENT] Button pressed!"); } else { bluetoothMonitor.send("[EVENT] Button released"); } } delay(10); }

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 upload
  • Đảm bảo Bluetooth trên điện thoại của bạn đã được bật
  • Trên Android 11 trở xuống, cũng bật Location services

2. Không có tin nhắn xuất hiện trong ứng dụng

  • Xác minh bluetoothMonitor.send() đang được gọi
  • Kiểm tra rằng bluetoothServer.loop() có trong main loop
  • Xác nhận kết nối trong Serial Monitor

3. Tin nhắn bị trễ

  • Giảm update interval để có tin nhắn thường xuyên hơn
  • Tránh gửi quá nhiều tin nhắn quá nhanh (BLE có giới hạn bandwidth)

4. Lệnh từ ứng dụng không được nhận

  • Xác minh callback onMonitorMessage đã được thiết lập
  • Kiểm tra Serial Monitor cho output lệnh đã nhận

5. Kết nối thường xuyên bị mất

  • Di chuyển gần hơn Arduino (giảm khoảng cách)
  • Đảm bảo nguồn USB ổn định

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

  • Cài đặt Arduino UNO R4 board package mới nhất qua Board Manager
  • Thử cáp USB hoặc port khác

Ý Tưởng Dự Án

  • Wireless debug console
  • Sensor data logger
  • System health monitor
  • Event notification system
  • Remote command interface

Các Bước Tiếp Theo

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

  1. Bluetooth Chat - Cho tin nhắn hai chiều tương tác
  2. Bluetooth Table - Cho hiển thị dữ liệu có cấu trúc
  3. Bluetooth Plotter - Cho visualization dữ liệu
  4. Multiple Bluetooth Apps - Kết hợp monitor với các ứng dụng khác

Hỗ Trợ

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

  • Kiểm tra tài liệu API Reference
  • Diễn đàn cộng đồng Arduino