블루투스 다중 앱 예제는 여러 블루투스 앱 인터페이스 를 하나의 ESP32 프로젝트에 결합하는 방법을 보여줍니다. 모니터, 채팅, 슬라이더, 조이스틱, 온도, 플로터, 테이블, 아날로그 게이지, 로테이터를 동시에 실행하세요 — DIYables Bluetooth STEM 앱을 통해 모두 접근 가능합니다. ESP32 보드용으로 설계 되었으며 BLE(블루투스 저에너지) 와 클래식 블루투스 연결을 모두 지원합니다. 여러 제어 및 표시 인터페이스가 동시에 필요한 복잡한 프로젝트에 이상적입니다.
이 예제는 두 가지 블루투스 모드를 지원합니다:
1 × 38-pin ESP32 ESP-WROOM-32 Dev Module - Narrow 쿠팡 | 아마존
1 × (또는) 38-pin ESP32 ESP-WROOM-32 Dev Module - Wide 쿠팡 | 아마존
1 × (또는) 30-pin ESP32 ESP-WROOM-32 Dev Module - Wide 아마존
1 × (또는) ESP32 Uno-form board 아마존
1 × (또는) ESP32 S3 Uno-form board 아마존
1 × USB 케이블 타입-A to 타입-C (USB-A PC용) 쿠팡 | 아마존
1 × USB 케이블 타입-C to 타입-C (USB-C PC용) 아마존
1 × 브레드보드 쿠팡 | 아마존
1 × 점퍼케이블 쿠팡 | 아마존
1 × (추천 ) ESP32용 스크루 터미널 확장 보드 쿠팡 | 아마존
1 × (추천 ) Breakout Expansion Board for ESP32 쿠팡 | 아마존
1 × (추천 ) ESP32용 전원 분배기 쿠팡 | 아마존
공개: 이 포스팅 에 제공된 일부 링크는 아마존 제휴 링크입니다. 이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.
다음 지침을 단계별로 따르세요:
USB 케이블로 ESP32 보드를 컴퓨터에 연결합니다.
컴퓨터에서 Arduino IDE를 실행합니다.
적절한 ESP32 보드와 COM 포트를 선택합니다.
Arduino IDE 왼쪽 바의 라이브러리 아이콘으로 이동합니다.
"DIYables Bluetooth" 를 검색한 다음 DIYables의 DIYables Bluetooth 라이브러리를 찾습니다.
설치 버튼을 클릭하여 라이브러리를 설치합니다.
> 중요: 이 예제에는 많은 블루투스 앱 라이브러리가 포함되어 있으므로 컴파일된 스케치가 일반보다 큽니다. 올바른 파티션 구성을 반드시 선택해야 합니다(아래 참조).
필요에 따라 아래 두 가지 블루투스 모드 중 하나를 선택하세요:
참고: 클래식 블루투스는 iOS에서 지원되지 않습니다. iOS 지원이 필요한 경우 아래 BLE 코드를 사용하세요.
#include <DIYables_BluetoothServer .h>
#include <DIYables_BluetoothMonitor .h>
#include <DIYables_BluetoothChat .h>
#include <DIYables_BluetoothSlider .h>
#include <DIYables_BluetoothJoystick .h>
#include <DIYables_BluetoothTemperature .h>
#include <DIYables_BluetoothPlotter .h>
#include <DIYables_BluetoothTable .h>
#include <DIYables_BluetoothAnalogGauge .h>
#include <DIYables_BluetoothRotator .h>
#include <platforms/DIYables_Esp32Bluetooth.h>
DIYables_Esp32Bluetooth bluetooth("ESP32 Multi-App" );
DIYables_BluetoothServer bluetoothServer(bluetooth);
DIYables_BluetoothMonitor bluetoothMonitor;
DIYables_BluetoothChat bluetoothChat;
DIYables_BluetoothSlider bluetoothSlider(0, 255, 1);
DIYables_BluetoothJoystick bluetoothJoystick(false , 5);
DIYables_BluetoothTemperature bluetoothTemperature(-10.0, 50.0, "°C" );
DIYables_BluetoothPlotter bluetoothPlotter;
DIYables_BluetoothTable bluetoothTable;
DIYables_BluetoothAnalogGauge bluetoothGauge(0.0, 100.0, "%" );
DIYables_BluetoothRotator bluetoothRotator(ROTATOR_MODE_CONTINUOUS );
const int LED_PIN = 2;
int currentSlider1 = 128;
int currentSlider2 = 64;
int currentJoystickX = 0;
int currentJoystickY = 0;
float currentTemperature = 25.0;
float currentGaugeValue = 50.0;
float currentRotatorAngle = 0.0;
int messageCount = 0;
unsigned long lastMonitorUpdate = 0;
unsigned long lastTempUpdate = 0;
unsigned long lastPlotUpdate = 0;
unsigned long lastTableUpdate = 0;
unsigned long lastGaugeUpdate = 0;
float plotPhase = 0;
void setup () {
Serial .begin (115200);
delay (1000);
Serial .println ("DIYables Bluetooth - ESP32 Multiple Apps Example" );
pinMode (LED_PIN, OUTPUT );
digitalWrite (LED_PIN, LOW );
bluetoothServer.begin ();
bluetoothServer.addApp (&bluetoothMonitor);
bluetoothServer.addApp (&bluetoothChat);
bluetoothServer.addApp (&bluetoothSlider);
bluetoothServer.addApp (&bluetoothJoystick);
bluetoothServer.addApp (&bluetoothTemperature);
bluetoothServer.addApp (&bluetoothPlotter);
bluetoothServer.addApp (&bluetoothTable);
bluetoothServer.addApp (&bluetoothGauge);
bluetoothServer.addApp (&bluetoothRotator);
Serial .print ("Registered apps: " );
Serial .println (bluetoothServer.getAppCount ());
bluetoothPlotter.setPlotTitle ("Sensor Data" );
bluetoothPlotter.setAxisLabels ("Time" , "Value" );
bluetoothPlotter.setYAxisRange (-1.5, 1.5);
bluetoothPlotter.setMaxSamples (100);
bluetoothPlotter.setLegendLabels ("Sine" , "Cosine" , "Random" );
bluetoothTable.addRow ("Status" );
bluetoothTable.addRow ("Uptime" );
bluetoothTable.addRow ("Slider 1" );
bluetoothTable.addRow ("Slider 2" );
bluetoothTable.addRow ("Joystick X" );
bluetoothTable.addRow ("Joystick Y" );
bluetoothTable.addRow ("Temperature" );
bluetoothTable.addRow ("Gauge Value" );
bluetoothTable.addRow ("Rotator Angle" );
bluetoothTable.addRow ("Free Heap" );
setupCallbacks();
Serial .println ("Waiting for Bluetooth connection..." );
}
void setupCallbacks() {
bluetoothServer.setOnConnected ([]() {
Serial .println ("Bluetooth connected!" );
digitalWrite (LED_PIN, HIGH );
bluetoothMonitor.send ("=== ESP32 Multi-App Connected ===" );
bluetoothMonitor.send ("All apps are ready!" );
bluetoothChat.send ("Hello! ESP32 Multi-App is connected." );
});
bluetoothServer.setOnDisconnected ([]() {
Serial .println ("Bluetooth disconnected!" );
digitalWrite (LED_PIN, LOW );
});
bluetoothMonitor.onMonitorMessage ([](const String& message) {
Serial .println ("Monitor cmd: " + message);
if (message == "HELP" ) {
bluetoothMonitor.send ("Commands: STATUS, HELP, LED_ON, LED_OFF, HEAP" );
} else if (message == "STATUS" ) {
bluetoothMonitor.send ("Slider1=" + String (currentSlider1) + " Slider2=" + String (currentSlider2));
bluetoothMonitor.send ("Joystick X=" + String (currentJoystickX) + " Y=" + String (currentJoystickY));
bluetoothMonitor.send ("Temp=" + String (currentTemperature, 1) + "°C" );
bluetoothMonitor.send ("Gauge=" + String (currentGaugeValue, 1) + "%" );
bluetoothMonitor.send ("Rotator=" + String (currentRotatorAngle, 0) + "°" );
} else if (message == "LED_ON" ) {
digitalWrite (LED_PIN, HIGH );
bluetoothMonitor.send ("LED turned ON" );
} else if (message == "LED_OFF" ) {
digitalWrite (LED_PIN, LOW );
bluetoothMonitor.send ("LED turned OFF" );
} else if (message == "HEAP" ) {
bluetoothMonitor.send ("Free Heap: " + String (ESP.getFreeHeap()) + " bytes" );
} else {
bluetoothMonitor.send ("Unknown: " + message + " (type HELP)" );
}
});
bluetoothChat.onChatMessage ([](const String& message) {
Serial .println ("Chat: " + message);
bluetoothChat.send ("Echo: " + message);
if (message.equalsIgnoreCase ("ping" )) {
bluetoothChat.send ("pong!" );
} else if (message.equalsIgnoreCase ("status" )) {
bluetoothChat.send ("Uptime: " + String (millis () / 1000) + "s, Apps: " + String (bluetoothServer.getAppCount ()));
} else if (message.equalsIgnoreCase ("heap" )) {
bluetoothChat.send ("Free heap: " + String (ESP.getFreeHeap()) + " bytes" );
}
});
bluetoothSlider.onSliderValue ([](int slider1, int slider2) {
currentSlider1 = slider1;
currentSlider2 = slider2;
Serial .print ("Slider 1: " ); Serial .print (slider1);
Serial .print (", Slider 2: " ); Serial .println (slider2);
currentGaugeValue = map (slider1, 0, 255, 0, 100);
bluetoothGauge.send (currentGaugeValue);
bluetoothTable.sendValueUpdate ("Slider 1" , String (slider1));
bluetoothTable.sendValueUpdate ("Slider 2" , String (slider2));
bluetoothTable.sendValueUpdate ("Gauge Value" , String (currentGaugeValue, 1) + "%" );
});
bluetoothSlider.onGetConfig ([]() {
bluetoothSlider.send (currentSlider1, currentSlider2);
});
bluetoothJoystick.onJoystickValue ([](int x, int y) {
currentJoystickX = x;
currentJoystickY = y;
Serial .print ("Joystick X: " ); Serial .print (x);
Serial .print (", Y: " ); Serial .println (y);
bluetoothTable.sendValueUpdate ("Joystick X" , String (x));
bluetoothTable.sendValueUpdate ("Joystick Y" , String (y));
});
bluetoothJoystick.onGetConfig ([]() {
bluetoothJoystick.send (currentJoystickX, currentJoystickY);
});
bluetoothTemperature.onTemperatureRequest ([]() {
bluetoothTemperature.send (currentTemperature);
});
bluetoothPlotter.onDataRequest ([]() {
Serial .println ("Plotter data requested" );
});
bluetoothTable.onDataRequest ([]() {
Serial .println ("Table data requested" );
bluetoothTable.sendTableStructure ();
updateAllTableValues();
});
bluetoothGauge.onValueRequest ([]() {
bluetoothGauge.send (currentGaugeValue);
});
bluetoothRotator.onRotatorAngle ([](float angle) {
currentRotatorAngle = angle;
Serial .print ("Rotator: " ); Serial .print (angle); Serial .println ("°" );
bluetoothTable.sendValueUpdate ("Rotator Angle" , String (angle, 0) + "°" );
});
}
void updateAllTableValues() {
bluetoothTable.sendValueUpdate ("Status" , "Running" );
unsigned long uptime = millis () / 1000;
String uptimeStr;
if (uptime >= 60) {
uptimeStr = String (uptime / 60) + "m " + String (uptime % 60) + "s" ;
} else {
uptimeStr = String (uptime) + "s" ;
}
bluetoothTable.sendValueUpdate ("Uptime" , uptimeStr);
bluetoothTable.sendValueUpdate ("Slider 1" , String (currentSlider1));
bluetoothTable.sendValueUpdate ("Slider 2" , String (currentSlider2));
bluetoothTable.sendValueUpdate ("Joystick X" , String (currentJoystickX));
bluetoothTable.sendValueUpdate ("Joystick Y" , String (currentJoystickY));
bluetoothTable.sendValueUpdate ("Temperature" , String (currentTemperature, 1) + " °C" );
bluetoothTable.sendValueUpdate ("Gauge Value" , String (currentGaugeValue, 1) + "%" );
bluetoothTable.sendValueUpdate ("Rotator Angle" , String (currentRotatorAngle, 0) + "°" );
bluetoothTable.sendValueUpdate ("Free Heap" , String (ESP.getFreeHeap()) + " bytes" );
}
void loop () {
bluetoothServer.loop ();
if (!bluetooth.isConnected ()) {
delay (10);
return ;
}
if (millis () - lastMonitorUpdate >= 5000) {
lastMonitorUpdate = millis ();
messageCount++;
bluetoothMonitor.send ("[INFO] Heartbeat #" + String (messageCount) + " - Uptime: " + String (millis () / 1000) + "s" );
}
if (millis () - lastTempUpdate >= 2000) {
lastTempUpdate = millis ();
static float tempOffset = 0;
tempOffset += random (-10, 11) / 10.0;
if (tempOffset > 5.0) tempOffset = 5.0;
if (tempOffset < -5.0) tempOffset = -5.0;
currentTemperature = 25.0 + tempOffset;
bluetoothTemperature.send (currentTemperature);
bluetoothTable.sendValueUpdate ("Temperature" , String (currentTemperature, 1) + " °C" );
}
if (millis () - lastPlotUpdate >= 100) {
lastPlotUpdate = millis ();
float sine = sin (plotPhase);
float cosine = cos (plotPhase);
float noise = random (-50, 51) / 100.0;
bluetoothPlotter.send (sine, cosine, noise);
plotPhase += 0.1;
if (plotPhase > 2 * PI ) plotPhase = 0;
}
if (millis () - lastTableUpdate >= 5000) {
lastTableUpdate = millis ();
unsigned long uptime = millis () / 1000;
String uptimeStr;
if (uptime >= 60) {
uptimeStr = String (uptime / 60) + "m " + String (uptime % 60) + "s" ;
} else {
uptimeStr = String (uptime) + "s" ;
}
bluetoothTable.sendValueUpdate ("Uptime" , uptimeStr);
bluetoothTable.sendValueUpdate ("Free Heap" , String (ESP.getFreeHeap()) + " bytes" );
}
if (millis () - lastGaugeUpdate >= 3000) {
lastGaugeUpdate = millis ();
float sensorValue = 50.0 + 30.0 * sin (millis () / 10000.0);
currentGaugeValue = sensorValue;
bluetoothGauge.send (currentGaugeValue);
bluetoothTable.sendValueUpdate ("Gauge Value" , String (currentGaugeValue, 1) + "%" );
}
delay (10);
}
중요: 도구 > 파티션 구성 으로 이동하여 "Huge APP (3MB No OTA/1MB SPIFFS)" 를 선택합니다. 다중 앱 예제는 상당히 많은 플래시 공간을 사용하므로 필수 설정입니다.
Arduino IDE에서 업로드 버튼을 클릭하여 ESP32에 코드를 업로드합니다.
시리얼 모니터를 엽니다.
시리얼 모니터에서 결과를 확인합니다. 다음과 같이 표시됩니다:
∞
Newbiely | Arduino IDE 2.3.8
8
Serial .println ("Hello World!" );
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
DIYables Bluetooth - ESP32 Multiple Apps Example
Waiting for Bluetooth connection...
Ln 11, Col 1
ESP32 Dev Module on COM15
2
#include <DIYables_BluetoothServer .h>
#include <DIYables_BluetoothMonitor .h>
#include <DIYables_BluetoothChat .h>
#include <DIYables_BluetoothSlider .h>
#include <DIYables_BluetoothJoystick .h>
#include <DIYables_BluetoothTemperature .h>
#include <DIYables_BluetoothPlotter .h>
#include <DIYables_BluetoothTable .h>
#include <DIYables_BluetoothAnalogGauge .h>
#include <DIYables_BluetoothRotator .h>
#include <platforms/DIYables_Esp32BLE.h>
const char* DEVICE_NAME = "ESP32BLE Multi-App" ;
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" ;
DIYables_Esp32BLE bluetooth(DEVICE_NAME, SERVICE_UUID, TX_UUID, RX_UUID);
DIYables_BluetoothServer bluetoothServer(bluetooth);
DIYables_BluetoothMonitor bluetoothMonitor;
DIYables_BluetoothChat bluetoothChat;
DIYables_BluetoothSlider bluetoothSlider(0, 255, 1);
DIYables_BluetoothJoystick bluetoothJoystick(false , 5);
DIYables_BluetoothTemperature bluetoothTemperature(-10.0, 50.0, "°C" );
DIYables_BluetoothPlotter bluetoothPlotter;
DIYables_BluetoothTable bluetoothTable;
DIYables_BluetoothAnalogGauge bluetoothGauge(0.0, 100.0, "%" );
DIYables_BluetoothRotator bluetoothRotator(ROTATOR_MODE_CONTINUOUS );
int currentSlider1 = 128;
int currentSlider2 = 64;
int currentJoystickX = 0;
int currentJoystickY = 0;
float currentTemperature = 25.0;
float currentGaugeValue = 50.0;
float currentRotatorAngle = 0.0;
int messageCount = 0;
unsigned long lastMonitorUpdate = 0;
unsigned long lastTempUpdate = 0;
unsigned long lastPlotUpdate = 0;
unsigned long lastTableUpdate = 0;
unsigned long lastGaugeUpdate = 0;
float plotPhase = 0;
void setup () {
Serial .begin (115200);
delay (1000);
Serial .println ("DIYables Bluetooth - ESP32 BLE Multiple Apps Example" );
pinMode (2, OUTPUT );
digitalWrite (2, LOW );
bluetoothServer.begin ();
bluetoothServer.addApp (&bluetoothMonitor);
bluetoothServer.addApp (&bluetoothChat);
bluetoothServer.addApp (&bluetoothSlider);
bluetoothServer.addApp (&bluetoothJoystick);
bluetoothServer.addApp (&bluetoothTemperature);
bluetoothServer.addApp (&bluetoothPlotter);
bluetoothServer.addApp (&bluetoothTable);
bluetoothServer.addApp (&bluetoothGauge);
bluetoothServer.addApp (&bluetoothRotator);
Serial .print ("Registered apps: " );
Serial .println (bluetoothServer.getAppCount ());
bluetoothPlotter.setPlotTitle ("Sensor Data" );
bluetoothPlotter.setAxisLabels ("Time" , "Value" );
bluetoothPlotter.setYAxisRange (-1.5, 1.5);
bluetoothPlotter.setMaxSamples (100);
bluetoothPlotter.setLegendLabels ("Sine" , "Cosine" , "Random" );
bluetoothTable.addRow ("Status" );
bluetoothTable.addRow ("Uptime" );
bluetoothTable.addRow ("Slider 1" );
bluetoothTable.addRow ("Slider 2" );
bluetoothTable.addRow ("Joystick X" );
bluetoothTable.addRow ("Joystick Y" );
bluetoothTable.addRow ("Temperature" );
bluetoothTable.addRow ("Gauge Value" );
bluetoothTable.addRow ("Rotator Angle" );
bluetoothTable.addRow ("Messages" );
setupCallbacks();
Serial .println ("Waiting for Bluetooth connection..." );
}
void setupCallbacks() {
bluetoothServer.setOnConnected ([]() {
Serial .println ("Bluetooth connected!" );
digitalWrite (2, HIGH );
bluetoothMonitor.send ("=== ESP32 BLE Multi-App Connected ===" );
bluetoothMonitor.send ("All apps are ready!" );
bluetoothChat.send ("Hello! ESP32 BLE Multi-App is connected." );
});
bluetoothServer.setOnDisconnected ([]() {
Serial .println ("Bluetooth disconnected!" );
digitalWrite (2, LOW );
});
bluetoothMonitor.onMonitorMessage ([](const String& message) {
Serial .println ("Monitor cmd: " + message);
if (message == "HELP" ) {
bluetoothMonitor.send ("Commands: STATUS, HELP, LED_ON, LED_OFF, HEAP" );
} else if (message == "STATUS" ) {
bluetoothMonitor.send ("Slider1=" + String (currentSlider1) + " Slider2=" + String (currentSlider2));
bluetoothMonitor.send ("Joystick X=" + String (currentJoystickX) + " Y=" + String (currentJoystickY));
bluetoothMonitor.send ("Temp=" + String (currentTemperature, 1) + "°C" );
bluetoothMonitor.send ("Gauge=" + String (currentGaugeValue, 1) + "%" );
bluetoothMonitor.send ("Rotator=" + String (currentRotatorAngle, 0) + "°" );
} else if (message == "LED_ON" ) {
digitalWrite (2, HIGH );
bluetoothMonitor.send ("LED turned ON" );
} else if (message == "LED_OFF" ) {
digitalWrite (2, LOW );
bluetoothMonitor.send ("LED turned OFF" );
} else if (message == "HEAP" ) {
bluetoothMonitor.send ("Free heap: " + String (ESP.getFreeHeap()) + " bytes" );
} else {
bluetoothMonitor.send ("Unknown: " + message + " (type HELP)" );
}
});
bluetoothChat.onChatMessage ([](const String& message) {
Serial .println ("Chat: " + message);
bluetoothChat.send ("Echo: " + message);
if (message.equalsIgnoreCase ("ping" )) {
bluetoothChat.send ("pong!" );
} else if (message.equalsIgnoreCase ("status" )) {
bluetoothChat.send ("Uptime: " + String (millis () / 1000) + "s, Apps: " + String (bluetoothServer.getAppCount ()));
}
});
bluetoothSlider.onSliderValue ([](int slider1, int slider2) {
currentSlider1 = slider1;
currentSlider2 = slider2;
Serial .print ("Slider 1: " ); Serial .print (slider1);
Serial .print (", Slider 2: " ); Serial .println (slider2);
currentGaugeValue = map (slider1, 0, 255, 0, 100);
bluetoothGauge.send (currentGaugeValue);
bluetoothTable.sendValueUpdate ("Slider 1" , String (slider1));
bluetoothTable.sendValueUpdate ("Slider 2" , String (slider2));
bluetoothTable.sendValueUpdate ("Gauge Value" , String (currentGaugeValue, 1) + "%" );
});
bluetoothSlider.onGetConfig ([]() {
bluetoothSlider.send (currentSlider1, currentSlider2);
});
bluetoothJoystick.onJoystickValue ([](int x, int y) {
currentJoystickX = x;
currentJoystickY = y;
Serial .print ("Joystick X: " ); Serial .print (x);
Serial .print (", Y: " ); Serial .println (y);
bluetoothTable.sendValueUpdate ("Joystick X" , String (x));
bluetoothTable.sendValueUpdate ("Joystick Y" , String (y));
});
bluetoothJoystick.onGetConfig ([]() {
bluetoothJoystick.send (currentJoystickX, currentJoystickY);
});
bluetoothTemperature.onTemperatureRequest ([]() {
bluetoothTemperature.send (currentTemperature);
});
bluetoothPlotter.onDataRequest ([]() {
Serial .println ("Plotter data requested" );
});
bluetoothTable.onDataRequest ([]() {
Serial .println ("Table data requested" );
bluetoothTable.sendTableStructure ();
updateAllTableValues();
});
bluetoothGauge.onValueRequest ([]() {
bluetoothGauge.send (currentGaugeValue);
});
bluetoothRotator.onRotatorAngle ([](float angle) {
currentRotatorAngle = angle;
Serial .print ("Rotator: " ); Serial .print (angle); Serial .println ("°" );
bluetoothTable.sendValueUpdate ("Rotator Angle" , String (angle, 0) + "°" );
});
}
void updateAllTableValues() {
bluetoothTable.sendValueUpdate ("Status" , "Running" );
unsigned long uptime = millis () / 1000;
String uptimeStr;
if (uptime >= 60) {
uptimeStr = String (uptime / 60) + "m " + String (uptime % 60) + "s" ;
} else {
uptimeStr = String (uptime) + "s" ;
}
bluetoothTable.sendValueUpdate ("Uptime" , uptimeStr);
bluetoothTable.sendValueUpdate ("Slider 1" , String (currentSlider1));
bluetoothTable.sendValueUpdate ("Slider 2" , String (currentSlider2));
bluetoothTable.sendValueUpdate ("Joystick X" , String (currentJoystickX));
bluetoothTable.sendValueUpdate ("Joystick Y" , String (currentJoystickY));
bluetoothTable.sendValueUpdate ("Temperature" , String (currentTemperature, 1) + " °C" );
bluetoothTable.sendValueUpdate ("Gauge Value" , String (currentGaugeValue, 1) + "%" );
bluetoothTable.sendValueUpdate ("Rotator Angle" , String (currentRotatorAngle, 0) + "°" );
bluetoothTable.sendValueUpdate ("Messages" , String (messageCount));
}
void loop () {
bluetoothServer.loop ();
if (!bluetooth.isConnected ()) {
delay (10);
return ;
}
if (millis () - lastMonitorUpdate >= 5000) {
lastMonitorUpdate = millis ();
messageCount++;
bluetoothMonitor.send ("[INFO] Heartbeat #" + String (messageCount) + " - Uptime: " + String (millis () / 1000) + "s" );
}
if (millis () - lastTempUpdate >= 2000) {
lastTempUpdate = millis ();
static float tempOffset = 0;
tempOffset += random (-10, 11) / 10.0;
if (tempOffset > 5.0) tempOffset = 5.0;
if (tempOffset < -5.0) tempOffset = -5.0;
currentTemperature = 25.0 + tempOffset;
bluetoothTemperature.send (currentTemperature);
bluetoothTable.sendValueUpdate ("Temperature" , String (currentTemperature, 1) + " °C" );
}
if (millis () - lastPlotUpdate >= 100) {
lastPlotUpdate = millis ();
float sine = sin (plotPhase);
float cosine = cos (plotPhase);
float noise = random (-50, 51) / 100.0;
bluetoothPlotter.send (sine, cosine, noise);
plotPhase += 0.1;
if (plotPhase > 2 * PI ) plotPhase = 0;
}
if (millis () - lastTableUpdate >= 5000) {
lastTableUpdate = millis ();
unsigned long uptime = millis () / 1000;
String uptimeStr;
if (uptime >= 60) {
uptimeStr = String (uptime / 60) + "m " + String (uptime % 60) + "s" ;
} else {
uptimeStr = String (uptime) + "s" ;
}
bluetoothTable.sendValueUpdate ("Uptime" , uptimeStr);
bluetoothTable.sendValueUpdate ("Messages" , String (messageCount));
}
if (millis () - lastGaugeUpdate >= 3000) {
lastGaugeUpdate = millis ();
float sensorValue = 50.0 + 30.0 * sin (millis () / 10000.0);
currentGaugeValue = sensorValue;
bluetoothGauge.send (currentGaugeValue);
bluetoothTable.sendValueUpdate ("Gauge Value" , String (currentGaugeValue, 1) + "%" );
}
delay (10);
}
중요: 도구 > 파티션 구성 으로 이동하여 "Huge APP (3MB No OTA/1MB SPIFFS)" 를 선택합니다. 다중 앱 예제는 상당히 많은 플래시 공간을 사용하므로 필수 설정입니다.
Arduino IDE에서 업로드 버튼을 클릭하여 ESP32에 코드를 업로드합니다.
시리얼 모니터를 엽니다.
시리얼 모니터에서 결과를 확인합니다. 다음과 같이 표시됩니다:
∞
Newbiely | Arduino IDE 2.3.8
8
Serial .println ("Hello World!" );
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
DIYables Bluetooth - ESP32 BLE Multiple Apps Example
Waiting for Bluetooth connection...
Ln 11, Col 1
ESP32 Dev Module on COM15
2
ESP32 클래식 블루투스 코드 를 사용하는 경우, 앱을 열기 전에 Android 폰과 ESP32를 페어링해야 합니다:
폰의 설정 > 블루투스 로 이동합니다.
블루투스가 켜져 있는지 확인합니다.
폰이 사용 가능한 기기를 검색합니다.
사용 가능한 기기 목록에서 "ESP32 Multi-App" 을 찾아 탭합니다.
페어링 요청을 확인합니다(PIN 필요 없음).
기기 이름 아래에 "페어링됨" 이 표시될 때까지 기다립니다.
ESP32 BLE 코드 를 사용하는 경우, 페어링이 필요 없습니다. 다음 단계로 진행하면 됩니다.
DIYables 블루투스 앱을 엽니다.
앱을 처음 열 때 권한을 요청합니다. 다음을 허용해 주세요:
폰에서 블루투스가 켜져 있는지 확인합니다.
홈 화면에서 연결 버튼을 탭합니다. 앱이 BLE 및 클래식 블루투스 기기를 모두 검색합니다.
참고: 홈 화면에서 설정 아이콘을 탭하여 홈 화면에서 앱을 숨기거나 표시할 수 있습니다. 자세한 내용은 DIYables 블루투스 앱 사용 설명서 를 참조하세요.
다음 앱 중 일부를 탭하여 ESP32와 인터랙션하세요: Monitor , Chat , Slider , Joystick , Temperature , Plotter , Table , Analog Gauge , Rotator
앱 간에 자유롭게 전환하세요 — 모두 동일한 블루투스 연결을 공유합니다.
Arduino IDE의 시리얼 모니터를 다시 보면 다음과 같이 표시됩니다:
∞
Newbiely | Arduino IDE 2.3.8
8
Serial .println ("Hello World!" );
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
Bluetooth connected!
Monitor: Uptime: 5s | Free heap: 245780 bytes
Temperature: 25.3°C
Plotter values sent
Table values updated
Gauge value: 50.0
Ln 11, Col 1
ESP32 Dev Module on COM15
2
각 앱은 별도의 객체로 생성되고 동일한 블루투스 서버에 등록됩니다. 단일 블루투스 연결을 공유하지만 독립적으로 작동합니다:
#include <DIYables_BluetoothMonitor .h>
#include <DIYables_BluetoothChat .h>
#include <DIYables_BluetoothSlider .h>
#include <DIYables_BluetoothJoystick .h>
#include <DIYables_BluetoothTemperature .h>
#include <DIYables_BluetoothPlotter .h>
#include <DIYables_BluetoothTable .h>
#include <DIYables_BluetoothAnalogGauge .h>
#include <DIYables_BluetoothRotator .h>
DIYables_BluetoothMonitor bluetoothMonitor;
DIYables_BluetoothChat bluetoothChat;
DIYables_BluetoothSlider bluetoothSlider(0, 100, 1);
DIYables_BluetoothJoystick bluetoothJoystick;
DIYables_BluetoothTemperature bluetoothTemperature(-10.0, 50.0, "°C" );
DIYables_BluetoothPlotter bluetoothPlotter;
DIYables_BluetoothTable bluetoothTable;
DIYables_BluetoothAnalogGauge bluetoothGauge(0.0, 100.0, "km/h" );
DIYables_BluetoothRotator bluetoothRotator(ROTATOR_MODE_LIMITED , 0, 180);
bluetoothServer.addApp (bluetoothMonitor);
bluetoothServer.addApp (bluetoothChat);
bluetoothServer.addApp (bluetoothSlider);
bluetoothServer.addApp (bluetoothJoystick);
bluetoothServer.addApp (bluetoothTemperature);
bluetoothServer.addApp (bluetoothPlotter);
bluetoothServer.addApp (bluetoothTable);
bluetoothServer.addApp (bluetoothGauge);
bluetoothServer.addApp (bluetoothRotator);
각 앱에는 자체 이벤트 핸들러가 있습니다:
void setup () {
bluetoothMonitor.onMonitorMessage ([](String message) {
Serial .println ("Monitor received: " + message);
});
bluetoothChat.onChatMessage ([](String message) {
Serial .println ("Chat: " + message);
bluetoothChat.send ("Echo: " + message);
});
bluetoothSlider.onSliderValue ([](int slider1, int slider2) {
Serial .println ("Slider: " + String (slider1));
});
bluetoothJoystick.onJoystickValue ([](int x, int y) {
Serial .println ("Joystick: X=" + String (x) + " Y=" + String (y));
});
bluetoothRotator.onRotatorAngle ([](float angle) {
Serial .println ("Rotator: " + String (angle) + "°" );
});
}