ESP32 Bluetooth Analog Gauge Example Hướng Dẫn Hiển Thị Đồng Hồ Analog

Tổng Quan

Ví dụ Bluetooth Analog Gauge cung cấp màn hình hiển thị kiểu đồng hồ analog cổ điển có thể truy cập thông qua ứng dụng DIYables Bluetooth STEM. Được thiết kế cho bo mạch ESP32 với hỗ trợ cho cả kết nối BLE (Bluetooth Low Energy)Classic Bluetooth. Gửi các giá trị số với phạm vi và nhãn đơn vị có thể cấu hình — hoàn hảo cho đồng hồ tốc độ, đồng hồ áp suất, đồng hồ điện áp và bất kỳ ứng dụng nào yêu cầu hiển thị mặt đồng hồ analog.

Ví dụ này hỗ trợ hai chế độ Bluetooth:

  • ESP32 BLE (Bluetooth Low Energy): Hoạt động trên cả Android và iOS
  • ESP32 Classic Bluetooth: Chỉ hoạt động trên Android. iOS không hỗ trợ Classic Bluetooth. Sử dụng BLE nếu bạn cần hỗ trợ iOS.
ESP32 Bluetooth analog gauge example - hướng dẫn hiển thị Đồng hồ analog

Tính Năng

  • Hiển Thị Đồng Hồ Analog: Hình ảnh hóa kiểu mặt đồng hồ/đồng hồ đo cổ điển
  • Phạm Vi Có Thể Cấu Hình: Đặt giá trị tối thiểu và tối đa cho đồng hồ
  • Đơn Vị Tùy Chỉnh: Hiển thị km/h, PSI, V, %, hoặc bất kỳ chuỗi đơn vị tùy chỉnh nào
  • Cập Nhật Thời Gian Thực: Gửi các số đọc trực tiếp với chuyển động kim mượt mà
  • Request Callback: Ứng dụng có thể yêu cầu giá trị hiện tại theo nhu cầu
  • BLE & Classic Bluetooth: Chọn chế độ Bluetooth phù hợp với dự án của bạn
  • Đa Nền Tảng: Chế độ BLE hoạt động trên cả Android và iOS; Classic Bluetooth hoạt động trên Android
  • Tùy Chọn Tiết Kiệm Điện: Chế độ BLE tiêu thụ ít điện năng hơn Classic Bluetooth

Linh Kiện Cần Thiết

1×mô-đun phát triển ESP-WROOM-32
1×Alternatively, ESP32 Uno-form board
1×Alternatively, ESP32 S3 Uno-form board
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×breadboard
1×dây jumper
1×(Khuyến nghị) Screw Terminal Expansion Board for ESP32
1×(Khuyến nghị) Breakout Expansion Board for ESP32
1×(Khuyến nghị) Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Code ESP32

Các Bước Nhanh

Hãy thực hiện theo các hướng dẫn này từng bước:

  • Mới sử dụng ESP32? Hãy bắt đầu với hướng dẫn Raspberry Pi Getting Started để học các kiến thức cơ bản trước.
  • Kết nối bo mạch ESP32 với máy tính của bạn bằng cáp USB.
  • Khởi chạy Arduino IDE trên máy tính của bạn.
  • Chọn bo mạch ESP32 và cổng COM phù 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ấp nút Install để cài đặt thư viện.
ESP32 diyables Bluetooth thư viện
  • 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.
ESP32 diyables Bluetooth dependency

Chọn một trong hai chế độ Bluetooth dưới đây tùy thuộc vào nhu cầu của bạn:

Code ESP32 Classic Bluetooth (chỉ hoạt động với ứng dụng trên Android)

Lưu ý: Classic Bluetooth KHÔNG được hỗ trợ trên iOS. Nếu bạn cần hỗ trợ iOS, hãy sử dụng code BLE bên dưới.

  • Trên Arduino IDE, vào File Examples DIYables Bluetooth Esp32Bluetooth_AnalogGauge example, hoặc sao chép code trên và dán vào trình chỉnh sửa của Arduino IDE
/* * DIYables Bluetooth Library - ESP32 Classic Bluetooth Analog Gauge Example * Works with DIYables Bluetooth STEM app on Android * Note: Classic Bluetooth is NOT supported on iOS. Use BLE examples for iOS support. * * This example demonstrates the Bluetooth Analog Gauge feature: * - Display values on an analog meter/gauge * - Configurable range and unit * - Perfect for sensor monitoring (speed, pressure, voltage, etc.) * * Compatible Boards: * - ESP32 (all variants with Classic Bluetooth) * - ESP32-WROOM-32 * - ESP32-DevKitC * - ESP32-WROVER * * Note: Select "Huge APP (3MB No OTA/1MB SPIFFS)" partition scheme * in Arduino IDE: Tools > Partition Scheme * * Optional: Analog sensor (potentiometer, pressure sensor, etc.) * * Setup: * 1. Upload the sketch to your ESP32 * 2. Open Serial Monitor (115200 baud) to see connection status * 3. Use DIYables Bluetooth App to connect and view the gauge * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */ #include <DIYables_BluetoothServer.h> #include <DIYables_BluetoothAnalogGauge.h> #include <platforms/DIYables_Esp32Bluetooth.h> // Create Bluetooth instances DIYables_Esp32Bluetooth bluetooth("ESP32_Gauge"); DIYables_BluetoothServer bluetoothServer(bluetooth); // Create Analog Gauge app instance (min=0, max=100, unit="km/h") DIYables_BluetoothAnalogGauge bluetoothGauge(0.0, 100.0, "km/h"); // Variables for gauge value float currentValue = 0.0; unsigned long lastUpdate = 0; const unsigned long UPDATE_INTERVAL = 200; // Update every 200ms // Optional: Analog input pin for sensor const int ANALOG_PIN = 34; // ESP32 ADC pin // Function to read sensor value float readSensorValue() { // TODO: Replace with actual sensor reading // Examples: // - Pressure sensor: readPressure() // - Voltage sensor: analogRead(34) * (3.3 / 4095.0) // - Speed sensor: calculateSpeed() // Option 1: Read from analog pin and map to gauge range // int rawValue = analogRead(ANALOG_PIN); // return map(rawValue, 0, 4095, 0, 100); // Option 2: Simulated data (sine wave) static float phase = 0; phase += 0.05; if (phase > 2 * PI) phase = 0; return 50 + 50 * sin(phase); // Oscillates between 0-100 } void setup() { Serial.begin(115200); delay(1000); Serial.println("DIYables Bluetooth - ESP32 Analog Gauge Example"); // Optional: Initialize analog pin // pinMode(ANALOG_PIN, INPUT); // Initialize Bluetooth server with platform-specific implementation bluetoothServer.begin(); // Add gauge app to server bluetoothServer.addApp(&bluetoothGauge); // Set up connection event callbacks bluetoothServer.setOnConnected([]() { Serial.println("Bluetooth connected!"); // Send initial value currentValue = readSensorValue(); bluetoothGauge.send(currentValue); Serial.print("Initial value sent: "); Serial.print(currentValue); Serial.print(" "); Serial.println(bluetoothGauge.getUnit()); }); bluetoothServer.setOnDisconnected([]() { Serial.println("Bluetooth disconnected!"); }); // Optional: Handle requests for current value bluetoothGauge.onValueRequest([]() { currentValue = readSensorValue(); bluetoothGauge.send(currentValue); Serial.print("Value requested - Sent: "); Serial.print(currentValue); Serial.print(" "); Serial.println(bluetoothGauge.getUnit()); }); // You can change gauge configuration at runtime: // bluetoothGauge.setRange(0.0, 200.0); // Change range to 0-200 // bluetoothGauge.setUnit("mph"); // Change unit to mph // bluetoothGauge.setRange(0.0, 3.3); // For voltage (0-3.3V on ESP32) // bluetoothGauge.setUnit("V"); Serial.println("Waiting for Bluetooth connection..."); Serial.print("Gauge range: "); Serial.print(bluetoothGauge.getMin()); Serial.print(" - "); Serial.print(bluetoothGauge.getMax()); Serial.print(" "); Serial.println(bluetoothGauge.getUnit()); } void loop() { // Handle Bluetooth server communications bluetoothServer.loop(); // Send gauge updates periodically (only when connected) if (bluetooth.isConnected() && millis() - lastUpdate >= UPDATE_INTERVAL) { lastUpdate = millis(); // Read sensor value currentValue = readSensorValue(); // Send to Bluetooth app bluetoothGauge.send(currentValue); // Print to Serial Monitor Serial.print("Gauge: "); Serial.print(currentValue, 1); Serial.print(" "); Serial.println(bluetoothGauge.getUnit()); } delay(10); }
  • Nhấp nút Upload trên Arduino IDE để upload code lên ESP32
  • 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 - ESP32 Analog Gauge Example Waiting for Bluetooth connection...
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code ESP32 BLE (hoạt động với ứng dụng trên cả Android và iOS)

  • Trên Arduino IDE, vào File Examples DIYables Bluetooth Esp32BLE_AnalogGauge example, hoặc sao chép code trên và dán vào trình chỉnh sửa của Arduino IDE
/* * DIYables Bluetooth Library - ESP32 BLE Analog Gauge Example * Works with DIYables Bluetooth STEM app on Android and iOS * * This example demonstrates the Bluetooth Analog Gauge feature: * - Display values on an analog meter/gauge * - Configurable range and unit * - Perfect for sensor monitoring (speed, pressure, voltage, etc.) * * Compatible Boards: * - ESP32-WROOM-32 * - ESP32-DevKitC * - ESP32-WROVER * - ESP32-S3 * - ESP32-C3 * - Any ESP32 board supporting BLE * * Note: Select "Huge APP (3MB No OTA/1MB SPIFFS)" partition scheme * in Arduino IDE: Tools > Partition Scheme * * Optional: Analog sensor (potentiometer, pressure sensor, etc.) * * Setup: * 1. Upload the sketch to your ESP32 * 2. Open Serial Monitor (115200 baud) to see connection status * 3. Use DIYables Bluetooth App to connect and view the gauge * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */ #include <DIYables_BluetoothServer.h> #include <DIYables_BluetoothAnalogGauge.h> #include <platforms/DIYables_Esp32BLE.h> // BLE Configuration const char* DEVICE_NAME = "ESP32BLE_Gauge"; 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_Esp32BLE bluetooth(DEVICE_NAME, SERVICE_UUID, TX_UUID, RX_UUID); DIYables_BluetoothServer bluetoothServer(bluetooth); // Create Analog Gauge app instance (min=0, max=100, unit="km/h") DIYables_BluetoothAnalogGauge bluetoothGauge(0.0, 100.0, "km/h"); // Variables for gauge value float currentValue = 0.0; unsigned long lastUpdate = 0; const unsigned long UPDATE_INTERVAL = 200; // Update every 200ms // Optional: Analog input pin for sensor const int ANALOG_PIN = 34; // ESP32 ADC pin // Function to read sensor value float readSensorValue() { // TODO: Replace with actual sensor reading // Option 1: Read from analog pin and map to gauge range // int rawValue = analogRead(ANALOG_PIN); // return map(rawValue, 0, 4095, 0, 100); // ESP32 has 12-bit ADC // Option 2: Simulated data (sine wave) static float phase = 0; phase += 0.05; if (phase > 2 * PI) phase = 0; return 50 + 50 * sin(phase); // Oscillates between 0-100 } void setup() { Serial.begin(115200); delay(1000); Serial.println("DIYables Bluetooth - ESP32 BLE Analog Gauge Example"); // Initialize Bluetooth server with platform-specific implementation bluetoothServer.begin(); // Add gauge app to server bluetoothServer.addApp(&bluetoothGauge); // Set up connection event callbacks bluetoothServer.setOnConnected([]() { Serial.println("Bluetooth connected!"); currentValue = readSensorValue(); bluetoothGauge.send(currentValue); Serial.print("Initial value sent: "); Serial.print(currentValue); Serial.print(" "); Serial.println(bluetoothGauge.getUnit()); }); bluetoothServer.setOnDisconnected([]() { Serial.println("Bluetooth disconnected!"); }); bluetoothGauge.onValueRequest([]() { currentValue = readSensorValue(); bluetoothGauge.send(currentValue); Serial.print("Value requested - Sent: "); Serial.print(currentValue); Serial.print(" "); Serial.println(bluetoothGauge.getUnit()); }); Serial.println("Waiting for Bluetooth connection..."); Serial.print("Gauge range: "); Serial.print(bluetoothGauge.getMin()); Serial.print(" - "); Serial.print(bluetoothGauge.getMax()); Serial.print(" "); Serial.println(bluetoothGauge.getUnit()); } void loop() { bluetoothServer.loop(); if (bluetooth.isConnected() && millis() - lastUpdate >= UPDATE_INTERVAL) { lastUpdate = millis(); currentValue = readSensorValue(); bluetoothGauge.send(currentValue); Serial.print("Gauge: "); Serial.print(currentValue, 1); Serial.print(" "); Serial.println(bluetoothGauge.getUnit()); } delay(10); }
  • Nhấp nút Upload trên Arduino IDE để upload code lên ESP32
  • 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 - ESP32 BLE Analog Gauge 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 điện thoại của bạn: Android | iOS
  • Nếu bạn đang sử dụng code ESP32 Classic Bluetooth, bạn cần ghép nối ESP32 với điện thoại Android của bạn trước khi mở ứng dụng:
    • Vào Settings > Bluetooth của điện thoại
    • Đảm bảo Bluetooth được bật
    • Điện thoại của bạn sẽ quét các thiết bị có sẵn
    • Tìm và nhấn "ESP32_Gauge" trong danh sách các thiết bị có sẵn
    • Xác nhận yêu cầu ghép nối (không cần PIN)
    • Đợi cho đến khi hiển thị "Paired" dưới tên thiết bị
  • Nếu bạn đang sử dụng code ESP32 BLE, không cần ghép nối. Chỉ cần tiến hành bước tiếp theo.
  • 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 vớ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ũ hơn để 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ả thiết bị BLE và Classic Bluetooth.
diyables Bluetooth app - home screen with scan nút nhấn
  • Tìm và nhấn thiết bị của bạn trong kết quả quét để kết nối:
    • Đối với Classic Bluetooth: nhấn "ESP32_Gauge"
    • Đối với BLE: nhấn "ESP32BLE_Gauge"
  • Sau khi kết nối, ứng dụng tự động quay về màn hình chính. Chọn ứng dụng Analog Gauge từ menu ứng dụng.
diyables Bluetooth app - home screen with analog gauge 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 các ứng dụng trên màn hình chính. Để biết thêm chi tiết, hãy xem DIYables Bluetooth App User Manual.

  • Đồng hồ analog sẽ hiển thị giá trị hiện tại với kim di chuyển
diyables Bluetooth app - analog 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! Gauge value: 50.0 km/h Gauge value: 75.0 km/h Gauge value: 93.3 km/h Gauge value: 100.0 km/h
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Theo dõi kim đồng hồ di chuyển theo thời gian thực trong ứng dụng

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

Cấu Hình Phạm Vi và Đơn Vị Đồng Hồ

Đặt phạm vi hiển thị và đơn vị:

// Constructor: DIYables_BluetoothAnalogGauge(min, max, unit) DIYables_BluetoothAnalogGauge bluetoothGauge(0.0, 100.0, "km/h"); // Change range at runtime bluetoothGauge.setRange(0.0, 200.0); // Change unit bluetoothGauge.setUnit("MPH"); // Read current configuration float minVal = bluetoothGauge.getMin(); // Returns 0.0 float maxVal = bluetoothGauge.getMax(); // Returns 100.0 String unit = bluetoothGauge.getUnit(); // Returns "km/h"

Gửi Giá Trị Đồng Hồ

// Send current reading float speed = 72.5; bluetoothGauge.send(speed); // Send text message bluetoothGauge.send("Calibrating...");

Xử Lý Yêu Cầu Giá Trị Từ Ứng Dụng

bluetoothGauge.onValueRequest([]() { float currentValue = readSensor(); bluetoothGauge.send(currentValue); Serial.println("App requested value: " + String(currentValue)); });

Xử Lý Sự Kiện Kết Nối

bluetoothServer.setOnConnected([]() { Serial.println("Bluetooth connected!"); bluetoothGauge.send(currentValue); // Send current reading }); bluetoothServer.setOnDisconnected([]() { Serial.println("Bluetooth disconnected!"); });

Cách Sử Dụng Analog Gauge

Giao Diện Ứng Dụng

Giao diện analog gauge trong DIYables Bluetooth App hiển thị:

  • Mặt Đồng Hồ/Kim: Đồng hồ đo analog cổ điển với kim di chuyển
  • Hiển Thị Số: Hiển thị giá trị hiện tại chính xác
  • Nhãn Đơn Vị: Hiển thị chuỗi đơn vị đã cấu hình
  • Vạch Chia Độ: Hiển thị thang chia độ từ min đến max

Cấu Hình Đơn Vị Phổ Biến

  • Tốc Độ: DIYables_BluetoothAnalogGauge(0.0, 200.0, "km/h")
  • Áp Suất: DIYables_BluetoothAnalogGauge(0.0, 100.0, "PSI")
  • Điện Áp: DIYables_BluetoothAnalogGauge(0.0, 5.0, "V")
  • Phần Trăm: DIYables_BluetoothAnalogGauge(0.0, 100.0, "%")
  • RPM: DIYables_BluetoothAnalogGauge(0.0, 8000.0, "RPM")

Ví Dụ Lập Trình

Đồng Hồ Điện Áp

const int VOLTAGE_PIN = 34; const float VREF = 3.3; const float DIVIDER_RATIO = 5.0; // Voltage divider ratio // Gauge configured for 0-16V range DIYables_BluetoothAnalogGauge bluetoothGauge(0.0, 16.0, "V"); void loop() { bluetoothServer.loop(); static unsigned long lastUpdate = 0; if (millis() - lastUpdate >= 500) { lastUpdate = millis(); int raw = analogRead(VOLTAGE_PIN); float voltage = (raw / 4095.0) * VREF * DIVIDER_RATIO; bluetoothGauge.send(voltage); Serial.println("Voltage: " + String(voltage, 2) + " V"); } delay(10); }

Đồng Hồ Áp Suất (BMP280)

#include <Adafruit_BMP280.h> Adafruit_BMP280 bmp; // Gauge configured for atmospheric pressure range DIYables_BluetoothAnalogGauge bluetoothGauge(900.0, 1100.0, "hPa"); void setup() { Serial.begin(115200); bmp.begin(0x76); // ... Bluetooth setup ... bluetoothGauge.onValueRequest([]() { float pressure = bmp.readPressure() / 100.0; // Pa to hPa bluetoothGauge.send(pressure); }); } void loop() { bluetoothServer.loop(); static unsigned long lastUpdate = 0; if (millis() - lastUpdate >= 1000) { lastUpdate = millis(); float pressure = bmp.readPressure() / 100.0; bluetoothGauge.send(pressure); Serial.println("Pressure: " + String(pressure, 1) + " hPa"); } delay(10); }

Cảm Biến Dòng Điện (ACS712)

const int CURRENT_PIN = 34; const float SENSITIVITY = 0.185; // ACS712-05B: 185mV/A const float ZERO_CURRENT_VOLTAGE = 2.5; // Gauge configured for 0-5A range DIYables_BluetoothAnalogGauge bluetoothGauge(0.0, 5.0, "A"); void loop() { bluetoothServer.loop(); static unsigned long lastUpdate = 0; if (millis() - lastUpdate >= 200) { lastUpdate = millis(); int raw = analogRead(CURRENT_PIN); float voltage = (raw / 4095.0) * 3.3; float current = abs((voltage - ZERO_CURRENT_VOLTAGE) / SENSITIVITY); bluetoothGauge.send(current); Serial.println("Current: " + String(current, 2) + " A"); } delay(10); }

Tốc Độ Từ Encoder

volatile unsigned long pulseCount = 0; const int ENCODER_PIN = 2; const float PULSES_PER_REV = 20.0; const float WHEEL_CIRCUMFERENCE = 0.628; // meters (20cm diameter) DIYables_BluetoothAnalogGauge bluetoothGauge(0.0, 50.0, "km/h"); void IRAM_ATTR countPulse() { pulseCount++; } void setup() { Serial.begin(115200); pinMode(ENCODER_PIN, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(ENCODER_PIN), countPulse, RISING); // ... Bluetooth setup ... } void loop() { bluetoothServer.loop(); static unsigned long lastCalc = 0; if (millis() - lastCalc >= 500) { unsigned long elapsed = millis() - lastCalc; lastCalc = millis(); noInterrupts(); unsigned long count = pulseCount; pulseCount = 0; interrupts(); float revPerSec = (count / PULSES_PER_REV) / (elapsed / 1000.0); float speedKmh = revPerSec * WHEEL_CIRCUMFERENCE * 3.6; bluetoothGauge.send(speedKmh); Serial.println("Speed: " + String(speedKmh, 1) + " km/h"); } delay(10); }

Kỹ Thuật Lập Trình Nâng Cao

Chuyển Động Kim Mượt Mà

float displayValue = 0; float targetValue = 0; const float SMOOTHING = 0.1; // 0.0-1.0, lower = smoother void loop() { bluetoothServer.loop(); static unsigned long lastUpdate = 0; if (millis() - lastUpdate >= 50) { // 20 FPS lastUpdate = millis(); targetValue = readSensor(); displayValue += (targetValue - displayValue) * SMOOTHING; bluetoothGauge.send(displayValue); } delay(5); }

Giữ Giá Trị Đỉnh

float peakValue = 0; unsigned long peakTime = 0; const unsigned long PEAK_HOLD_MS = 3000; // Hold peak for 3 seconds void updateWithPeakHold(float value) { if (value > peakValue) { peakValue = value; peakTime = millis(); } if (millis() - peakTime > PEAK_HOLD_MS) { peakValue = value; // Reset peak } // Send the higher of current or peak bluetoothGauge.send(max(value, peakValue)); }

Tự Động Chia Tỷ Lệ Phạm Vi

float autoMin = 0; float autoMax = 100; void autoScale(float value) { bool changed = false; if (value < autoMin) { autoMin = value - 10; changed = true; } if (value > autoMax) { autoMax = value + 10; changed = true; } if (changed) { bluetoothGauge.setRange(autoMin, autoMax); } bluetoothGauge.send(value); }

Ý Tưởng Tích Hợp Phần Cứng

Cảm Biến Analog

Bất kỳ cảm biến nào có đầu ra analog đều có thể điều khiển đồng hồ: chiết áp, cảm biến lực, cảm biến uốn, và nhiều hơn nữa.

Cảm Biến I2C/SPI

Sử dụng cảm biến I2C như BMP280 (áp suất), INA219 (dòng điện/công suất), hoặc cảm biến SPI để đo tốc độ cao.

Cảm Biến Xung/Tần Số

Encoder tốc độ, đồng hồ lưu lượng, và cảm biến RPM sử dụng đếm dựa trên ngắt.

Load Cell (HX711)

Đo trọng lượng với bộ khuếch đại HX711 cho cân bếp hoặc cân công nghiệp.

BLE vs Classic Bluetooth - Nên Chọn Loại Nào?

Tính NăngBLE (Esp32BLE_AnalogGauge)Classic Bluetooth (Esp32Bluetooth_AnalogGauge)
Hỗ Trợ iOS? Có? Không
Hỗ Trợ Android? Có? Có
Tiêu Thụ ĐiệnThấpCao hơn
Phạm Vi~30-100m~10-100m
Tốc Độ Dữ LiệuThấp hơnCao hơn
Yêu Cầu Ghép NốiKhông (tự động kết nối)Có (ghép nối thủ công)
Tốt Nhất ChoTiết kiệm pin, đa nền tảngThông lượng cao, chỉ Android

Khắc Phục Sự 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 ESP32 được cấp nguồn và sketch đã được upload
  • Đối với BLE: Đảm bảo Bluetooth và Location của điện thoại được bật
  • Đối với Classic Bluetooth: Ghép nối thiết bị trước trong cài đặt Bluetooth của điện thoại
  • Kiểm tra rằng partition scheme đúng đã được chọn (Huge APP)

2. Đồng hồ hiển thị 0 hoặc giá trị không chính xác

  • Xác minh kết nối dây và cảm biến
  • Kiểm tra việc đọc cảm biến với Serial Monitor trước
  • Đảm bảo giá trị nằm trong phạm vi đã cấu hình
  • Xác minh cấu hình đơn vị và phạm vi trong constructor

3. Kim đồng hồ không chuyển động mượt mà

  • Tăng tần suất cập nhật (khoảng thời gian nhỏ hơn trong kiểm tra millis())
  • Áp dụng làm mượt/lọc cho việc đọc cảm biến
  • Kiểm tra nhiễu cảm biến hoặc việc đọc không ổn định

4. Giá trị ngoài phạm vi đồng hồ

  • Điều chỉnh phạm vi với setRange(min, max) để phù hợp với đầu ra cảm biến
  • Giá trị ngoài phạm vi sẽ hiển thị nhưng có thể bị cắt ở min/max
  • Xem xét tự động chia tỷ lệ cho các phạm vi không biết

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

  • Di chuyển gần hơn với ESP32 (giảm khoảng cách)
  • Đối với BLE: Kiểm tra nhiễu từ các thiết bị BLE khác
  • Đối với Classic Bluetooth: Đảm bảo nguồn điện ổn định cho ESP32

6. Sketch quá lớn / không đủ không gian

  • Trong Arduino IDE, vào Tools > Partition Scheme và chọn "Huge APP (3MB No OTA/1MB SPIFFS)" hoặc "No OTA (Large APP)"
  • Partition scheme mặc định chỉ cung cấp ~1.2MB cho code ứng dụng, không đủ cho thư viện Bluetooth
  • Cài đặt này cung cấp ~3MB bằng cách hy sinh phân vùng OTA (over-the-air update)

Mẹo Debug

Thêm debug toàn diện:

void debugGauge(float value) { Serial.println("=== Gauge Debug ==="); Serial.println("Value: " + String(value, 2) + " " + bluetoothGauge.getUnit()); Serial.println("Range: " + String(bluetoothGauge.getMin(), 1) + " - " + String(bluetoothGauge.getMax(), 1)); Serial.println("In Range: " + String(value >= bluetoothGauge.getMin() && value <= bluetoothGauge.getMax() ? "Yes" : "No")); float percentage = (value - bluetoothGauge.getMin()) / (bluetoothGauge.getMax() - bluetoothGauge.getMin()) * 100.0; Serial.println("Percentage: " + String(percentage, 1) + "%"); Serial.println("==================="); }

Ý Tưởng Dự Án

Xe Cộ & Chuyển Động

  • Đồng hồ tốc độ cho xe RC hoặc xe đạp
  • Đồng hồ đo vòng quay RPM cho động cơ
  • Đồng hồ đo lực G để đo gia tốc
  • Chỉ báo góc nghiêng

Điện & Nguồn

  • Đồng hồ voltmeter (monitor điện áp pin)
  • Đồng hồ ampe (tiêu thụ dòng điện)
  • Đồng hồ watt (sử dụng công suất)
  • Đồng hồ mức pin

Môi Trường

  • Đồng hồ áp suất khí quyển
  • Tốc độ gió (máy đo gió)
  • Đồng hồ chỉ số UV
  • Đồng hồ đo mức âm thanh (dB)

Công Nghiệp

  • Hiển thị cân trọng lượng
  • Đồng hồ lưu lượng
  • Chỉ báo mức bồn chứa
  • Đồng hồ đo momen xoắn

Tích Hợp Với Các