아두이노 우노 R4 WiFi 블루투스 아날로그 게이지 예제 BLE를 통한 게이지 표시 튜토리얼
개요
블루투스 아날로그 게이지 예제는 DIYables 블루투스 STEM 앱을 통해 시각적 아날로그 스타일 게이지 디스플레이를 제공합니다. BLE(Bluetooth Low Energy) 를 사용하는 Arduino UNO R4 WiFi 전용으로 설계되었으며, 스마트폰에서 구성 가능한 범위와 단위로 아름다운 게이지에 모든 아날로그 값을 표시합니다. 스피드미터, 압력 게이지, RPM 표시 및 다이얼 스타일 시각화의 이점이 있는 모든 값에 적합합니다.
참고: Arduino UNO R4 WiFi는 BLE(Bluetooth Low Energy)만 지원합니다. 클래식 블루투스는 지원하지 않습니다. DIYables 블루투스 앱은 Android에서 BLE와 클래식 블루투스를 모두 지원하고, iOS에서는 BLE를 지원합니다. 이 보드는 BLE를 사용하므로 앱은 Android와 iOS 모두에서 작동합니다.
"DIYables Bluetooth"를 검색한 다음 DIYables의 DIYables Bluetooth 라이브러리를 찾습니다.
Install 버튼을 클릭하여 라이브러리를 설치합니다.
다른 라이브러리 종속성 설치를 요청받습니다.
Install All 버튼을 클릭하여 모든 라이브러리 종속성을 설치합니다.
BLE 코드
Arduino IDE에서 File Examples DIYables Bluetooth ArduinoBLE_AnalogGauge 예제로 이동하거나, 위의 코드를 복사하여 Arduino IDE 편집기에 붙여 넣습니다.
/* * DIYables Bluetooth Library - Bluetooth 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: * - 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: Analog sensor (potentiometer, pressure sensor, etc.) * * 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 gauge * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */#include <DIYables_BluetoothServer.h>#include <DIYables_BluetoothAnalogGauge.h>#include <platforms/DIYables_ArduinoBLE.h>// BLE Configurationconst char* DEVICE_NAME = "Arduino_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 instancesDIYables_ArduinoBLE 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 valuefloat currentValue = 0.0;unsignedlong lastUpdate = 0;constunsignedlong UPDATE_INTERVAL = 200; // Update every 200ms// Optional: Analog input pin for sensorconstint ANALOG_PIN = A0;// Function to read sensor valuefloat readSensorValue() {// TODO: Replace with actual sensor reading// Examples:// - Pressure sensor: readPressure()// - Voltage sensor: analogRead(A0) * (5.0 / 1023.0)// - Speed sensor: calculateSpeed()// Option 1: Read from analog pin and map to gauge range// int rawValue = analogRead(ANALOG_PIN);// return map(rawValue, 0, 1023, 0, 100);// Option 2: Simulated data (sine wave)staticfloat phase = 0; phase += 0.05;if (phase > 2 * PI) phase = 0;return 50 + 50 * sin(phase); // Oscillates between 0-100}voidsetup() {Serial.begin(9600);while (!Serial);Serial.println("DIYables Bluetooth - 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, 5.0); // For voltage (0-5V)// 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());}voidloop() {// 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 MonitorSerial.print("Gauge: ");Serial.print(currentValue, 1);Serial.print(" ");Serial.println(bluetoothGauge.getUnit()); }delay(10);}
Arduino IDE에서 Upload 버튼을 클릭하여 코드를 Arduino UNO R4 WiFi에 업로드합니다.
시리얼 모니터를 엽니다.
시리얼 모니터에서 결과를 확인합니다. 아래와 같이 표시됩니다:
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 - Analog Gauge Example
Waiting for Bluetooth connection...
// Send a value to the gaugebluetoothGauge.send(75.5);// Read from sensor and sendfloat sensorValue = analogRead(A0) * (100.0 / 1023.0);bluetoothGauge.send(sensorValue);
값 요청 처리
bluetoothGauge.onValueRequest([]() {float value = readSensor(); bluetoothGauge.send(value);Serial.print("Requested: ");Serial.println(value);});