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.
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
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.
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.
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 Configurationconst 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 instancesDIYables_ArduinoBLE bluetooth(DEVICE_NAME, SERVICE_UUID, TX_UUID, RX_UUID);DIYables_BluetoothServer bluetoothServer(bluetooth);// Create Monitor app instanceDIYables_BluetoothMonitor bluetoothMonitor;// Variables for demounsignedlong lastUpdate = 0;constunsignedlong UPDATE_INTERVAL = 3000; // Send update every 3 secondsint messageCount = 0;bool ledState = false;voidsetup() {Serial.begin(9600);while (!Serial);Serial.println("DIYables Bluetooth - Monitor Example");// Initialize built-in LEDpinMode(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"); }elseif (cmd == "LED_ON") {digitalWrite(LED_BUILTIN, HIGH); ledState = true; bluetoothMonitor.send("✓ LED turned ON"); }elseif (cmd == "LED_OFF") {digitalWrite(LED_BUILTIN, LOW); ledState = false; bluetoothMonitor.send("✓ LED turned OFF"); }elseif (cmd == "STATUS") { showStatus(); }elseif (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"));// Uptimeunsignedlong 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__externint __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 typesif (messageCount % 3 == 0) {// Send status update bluetoothMonitor.send("[INFO] Heartbeat #" + String(messageCount)); } elseif (messageCount % 5 == 0) {// Send simulated sensor readingint 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 wellSerial.print("Sent update #");Serial.println(messageCount);}voidloop() {// 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:
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 - Monitor Example
Waiting for Bluetooth connection...
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2
Ứ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.
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.
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
Bây giờ 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!
Sent update #1
Sent update #2
Sent update #3
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
Gửi Tin Nhắn
// Gửi tin nhắn văn bản đến ứng dụngbluetoothMonitor.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:
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 elseif. 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.