Arduino UNO R4 WiFi Bluetooth Temperature Gauge Example Hiển Thị Nhiệt Độ qua BLE Tutorial

Tổng Quan

Ví dụ Bluetooth Temperature cung cấp đồng hồ đo nhiệt độ trực quan 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 đọc số nhiệt độ với phạm vi và đơn vị có thể cấu hình trên smartphone của bạn. Hoàn hảo cho giám sát nhiệt độ, trạm thời tiết, dự án HVAC, và cảm biến môi trường.

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 temperature gauge example - hiển thị nhiệt Độ qua ble tutorial

Tính Năng

  • Đồng Hồ Đo Trực Quan: Hiển thị đồng hồ đo nhiệt độ đẹp mắt trên smartphone
  • Phạm Vi Có Thể Cấu Hình: Đặt giá trị nhiệt độ tối thiểu và tối đa
  • Đơn Vị Tùy Chỉnh: Hiển thị °C, °F, hoặc bất kỳ đơn vị tùy chỉnh nào
  • Cập Nhật Thời Gian Thực: Đẩy thay đổi nhiệt độ theo khoảng thời gian có thể cấu hình
  • Yêu Cầu Theo Nhu Cầu: Ứng dụng có thể yêu cầu nhiệt độ hiện tại
  • 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 Đôi: BLE tự động kết nối mà không cần ghép đô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×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×(Tùy chọn) DS18B20 Temperature Sensor
1×(Tùy chọn) DHT11 Temperature Humidity Sensor Module
1×(Tùy chọn) DHT22 Temperature Humidity Sensor Module
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 này từng bước một:

  • 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 bởi DIYables
  • Nhấp 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ấp 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_Temperature example, hoặc sao chép code trên và dán vào trình soạn thảo của Arduino IDE
/* * DIYables Bluetooth Library - Bluetooth Temperature Example * Works with DIYables Bluetooth STEM app on Android and iOS * * This example demonstrates the Bluetooth Temperature feature: * - Display temperature sensor readings * - Configurable temperature range and unit * - Real-time temperature updates * * 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 * * Optional: Temperature sensor (DHT22, DS18B20, or analog thermistor) * * 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 temperature * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */ #include <DIYables_BluetoothServer.h> #include <DIYables_BluetoothTemperature.h> #include <platforms/DIYables_ArduinoBLE.h> // BLE Configuration const char* DEVICE_NAME = "Arduino_Temp"; 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 Temperature app instance (min=-10°C, max=50°C, unit="°C") DIYables_BluetoothTemperature bluetoothTemperature(-10.0, 50.0, "°C"); // Variables for temperature simulation float currentTemperature = 25.0; unsigned long lastTempUpdate = 0; const unsigned long TEMP_UPDATE_INTERVAL = 2000; // Update every 2 seconds // Simulated temperature sensor reading float readTemperature() { // TODO: Replace with actual sensor reading // Examples: // - DHT22: dht.readTemperature() // - DS18B20: sensors.getTempCByIndex(0) // - Analog thermistor: analogToTemperature(analogRead(A0)) // Simulate temperature changes static float offset = 0; offset += random(-10, 11) / 10.0; // Random walk if (offset > 5.0) offset = 5.0; if (offset < -5.0) offset = -5.0; return 25.0 + offset; // Base temperature 25°C with variation } void setup() { Serial.begin(9600); while (!Serial); Serial.println("DIYables Bluetooth - Temperature Example"); // TODO: Initialize your temperature sensor here // Examples: // dht.begin(); // sensors.begin(); // Initialize Bluetooth server with platform-specific implementation bluetoothServer.begin(); // Add temperature app to server bluetoothServer.addApp(&bluetoothTemperature); // Set up connection event callbacks bluetoothServer.setOnConnected([]() { Serial.println("Bluetooth connected!"); // Send initial temperature reading float temp = readTemperature(); bluetoothTemperature.send(temp); Serial.print("Initial temperature sent: "); Serial.print(temp); Serial.println("°C"); }); bluetoothServer.setOnDisconnected([]() { Serial.println("Bluetooth disconnected!"); }); // Optional: Handle requests for temperature value bluetoothTemperature.onTemperatureRequest([]() { float temp = readTemperature(); bluetoothTemperature.send(temp); Serial.print("Temperature requested - Sent: "); Serial.print(temp); Serial.println("°C"); }); // You can change temperature configuration at runtime: // bluetoothTemperature.setRange(-40.0, 125.0); // Wide range for industrial sensors // bluetoothTemperature.setUnit("°F"); // Change to Fahrenheit Serial.println("Waiting for Bluetooth connection..."); } void loop() { // Handle Bluetooth server communications bluetoothServer.loop(); // Send temperature updates periodically (only when connected) if (bluetooth.isConnected() && millis() - lastTempUpdate >= TEMP_UPDATE_INTERVAL) { lastTempUpdate = millis(); // Read temperature from sensor currentTemperature = readTemperature(); // Send to Bluetooth app bluetoothTemperature.send(currentTemperature); // Print to Serial Monitor Serial.print("Temperature: "); Serial.print(currentTemperature); Serial.println("°C"); } 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ư sau:
COM6
Send
DIYables Bluetooth - Temperature 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 của bạn: 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 ghép đôi thủ công cho BLE — chỉ cần quét 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 để quét và kết nố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ũ để 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 các thiết bị BLE.
diyables Bluetooth app - home screen with scan nút nhấn
  • Tìm và nhấn "Arduino_Temp" 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 Temperature từ menu ứng dụng.
diyables Bluetooth app - home screen with temperature 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 đồng hồ đo nhiệt độ hiển thị đọc số hiện tại với phạm vi và đơn vị đã cấu hình
diyables Bluetooth app - temperature gauge 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! Temperature: 24.50 °C Temperature: 24.80 °C Temperature: 25.10 °C
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 Phạm Vi Nhiệt Độ và Đơn Vị

// Celsius: -10 đến 50 DIYables_BluetoothTemperature bluetoothTemp(bluetoothServer, -10.0, 50.0, "°C"); // Fahrenheit: 0 đến 120 DIYables_BluetoothTemperature bluetoothTemp(bluetoothServer, 0.0, 120.0, "°F"); // Phạm vi tùy chỉnh cho sử dụng công nghiệp DIYables_BluetoothTemperature bluetoothTemp(bluetoothServer, -40.0, 200.0, "°C");

Gửi Cập Nhật Nhiệt Độ

// Gửi giá trị nhiệt độ đến ứng dụng bluetoothTemp.send(25.5); // Đọc từ sensor thực và gửi float temp = readTemperatureSensor(); bluetoothTemp.send(temp);

Xử Lý Yêu Cầu Nhiệt Độ

bluetoothTemp.onTemperatureRequest([]() { float temp = readTemperatureSensor(); bluetoothTemp.send(temp); Serial.print("Requested: "); Serial.println(temp); });

Ví Dụ Lập Trình

DS18B20 Temperature Sensor

#include <OneWire.h> #include <DallasTemperature.h> OneWire oneWire(2); DallasTemperature sensors(&oneWire); DIYables_BluetoothTemperature bluetoothTemp(bluetoothServer, -10.0, 50.0, "°C"); void setup() { sensors.begin(); bluetoothTemp.onTemperatureRequest([]() { sensors.requestTemperatures(); float temp = sensors.getTempCByIndex(0); bluetoothTemp.send(temp); }); } void loop() { bluetoothServer.loop(); static unsigned long lastTime = 0; if (millis() - lastTime >= 2000) { lastTime = millis(); sensors.requestTemperatures(); float temp = sensors.getTempCByIndex(0); bluetoothTemp.send(temp); } }

DHT22 Sensor với Fahrenheit

#include <DHT.h> DHT dht(2, DHT22); DIYables_BluetoothTemperature bluetoothTemp(bluetoothServer, 0.0, 120.0, "°F"); void setup() { dht.begin(); bluetoothTemp.onTemperatureRequest([]() { float temp = dht.readTemperature(true); // Fahrenheit if (!isnan(temp)) { bluetoothTemp.send(temp); } }); } void loop() { bluetoothServer.loop(); static unsigned long lastTime = 0; if (millis() - lastTime >= 2000) { lastTime = millis(); float temp = dht.readTemperature(true); if (!isnan(temp)) { bluetoothTemp.send(temp); } } }

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 bật nguồn và sketch đã được upload
  • Đảm bảo Bluetooth điện thoại của bạn được bật
  • Trên Android 11 trở xuống, cũng bật dịch vụ vị trí

2. Nhiệt độ không cập nhật

  • Kiểm tra rằng send() đang được gọi trong loop
  • Xác minh thời gian khoảng cách cập nhật
  • Đảm bảo bluetoothServer.loop() được gọi

3. Nhiệt độ hiển thị sai phạm vi

  • Kiểm tra các giá trị min/max trong constructor
  • Đảm bảo chuỗi đơn vị đúng
  • Giá trị nhiệt độ phải nằm trong phạm vi đã cấu hình

4. Đọc sensor là NaN

  • Xác minh kết nối dây sensor
  • Kiểm tra nguồn cấp sensor
  • Thêm kiểm tra isnan() trước khi gửi

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 nhiệt độ phòng
  • Bộ điều khiển khí hậu nhà kính
  • Nhiệt kế tủ lạnh/tủ đông
  • Hiển thị trạm thời tiết
  • Giám sát nhiệt độ bể cá

Các Bước Tiếp Theo

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

  1. Bluetooth Analog Gauge - Cho hiển thị đồng hồ đo đa năng
  2. Bluetooth Plotter - Cho ghi nhật ký nhiệt độ theo thời gian
  3. Bluetooth Table - Cho hiển thị đa sensor
  4. Multiple Bluetooth Apps - Kết hợp nhiệt độ 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