ESP32 블루투스 디지털 핀 예제 핀 제어 및 모니터 인터페이스 튜토리얼
개요
블루투스 디지털 핀 예제는 DIYables Bluetooth STEM 앱을 통해 ESP32 GPIO 핀을 원격으로 제어하고 모니터링하는 기능을 제공합니다. ESP32 보드용으로 설계되었으며 BLE(블루투스 저에너지)와 클래식 블루투스 연결을 모두 지원합니다. 핀을 입력 또는 출력으로 구성하고, 출력 핀을 토글하고, 디지털 및 아날로그 입력 상태를 읽고, 실시간 핀 변경 알림을 수신하세요 — 홈 자동화, 릴레이 제어, 버튼 모니터링, 센서 읽기에 적합합니다.
이 예제는 두 가지 블루투스 모드를 지원합니다:
- ESP32 BLE (블루투스 저에너지): Android와 iOS 모두에서 작동
- ESP32 클래식 블루투스: Android에서만 작동. iOS는 클래식 블루투스를 지원하지 않습니다. iOS 지원이 필요한 경우 BLE를 사용하세요.

기능
- 출력 핀 제어: 앱에서 디지털 출력 핀을 HIGH/LOW로 토글
- 입력 핀 모니터링: 디지털 및 아날로그 입력 핀 상태 읽기
- 커스텀 핀 이름: 각 핀에 친숙한 이름 지정 (예: "LED", "Btn1", "A34")
- 실시간 업데이트: 입력 핀 상태 변경 시 자동 알림
- 최대 16개 핀: 동시에 최대 16개의 설정 가능한 핀 지원
- 혼합 모드: 동일한 설정에서 입력 및 출력 핀 조합
- BLE & 클래식 블루투스: 프로젝트에 맞는 블루투스 모드 선택
- 크로스 플랫폼: BLE 모드는 Android와 iOS 모두에서 작동; 클래식 블루투스는 Android에서만 작동
필요한 하드웨어
| 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용 전원 분배기 | 쿠팡 | 아마존 |
공개: 이 포스팅 에 제공된 일부 링크는 아마존 제휴 링크입니다. 이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.
ESP32 코드
빠른 시작
다음 지침을 단계별로 따르세요:
- ESP32가 처음이라면 ESP32 - 소프트웨어 설치 튜토리얼을 참조하세요.
- USB 케이블로 ESP32 보드를 컴퓨터에 연결합니다.
- 컴퓨터에서 Arduino IDE를 실행합니다.
- 적절한 ESP32 보드와 COM 포트를 선택합니다.
- Arduino IDE 왼쪽 바의 라이브러리 아이콘으로 이동합니다.
- "DIYables Bluetooth"를 검색한 다음 DIYables의 DIYables Bluetooth 라이브러리를 찾습니다.
- 설치 버튼을 클릭하여 라이브러리를 설치합니다.

- 다른 라이브러리 종속성 설치 여부를 묻는 메시지가 표시됩니다.
- 모두 설치 버튼을 클릭하여 모든 라이브러리 종속성을 설치합니다.

필요에 따라 아래 두 가지 블루투스 모드 중 하나를 선택하세요:
ESP32 클래식 블루투스 코드 (Android의 앱에서만 작동)
참고: 클래식 블루투스는 iOS에서 지원되지 않습니다. iOS 지원이 필요한 경우 아래 BLE 코드를 사용하세요.
- Arduino IDE에서 파일 예제 DIYables Bluetooth Esp32Bluetooth_PinControl 예제로 이동하거나, 위의 코드를 복사하여 Arduino IDE 편집기에 붙여넣습니다.
/*
* DIYables Bluetooth Library - ESP32 Classic Bluetooth Pin Control 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 Pin Control/Monitor feature:
* - Control digital output pins via Bluetooth
* - Monitor digital input pins
* - Monitor analog input pins
* - Configure pin modes (INPUT/OUTPUT)
*
* 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
*
* 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 control pins
*
* Tutorial: https://diyables.io/bluetooth-app
* Author: DIYables
*/
#include <DIYables_BluetoothServer.h>
#include <DIYables_BluetoothPinControl.h>
#include <platforms/DIYables_Esp32Bluetooth.h>
// Create Bluetooth instances
DIYables_Esp32Bluetooth bluetooth("ESP32_Pins");
DIYables_BluetoothServer bluetoothServer(bluetooth);
// Create Pin Control/Monitor app instance
DIYables_BluetoothPinControl bluetoothPins;
// Pin configuration (adjust for your ESP32 board)
const int LED_PIN = 2; // Built-in LED on most ESP32 boards
const int OUTPUT_PIN_1 = 16;
const int OUTPUT_PIN_2 = 17;
const int INPUT_PIN_1 = 18;
const int INPUT_PIN_2 = 19;
const int ANALOG_PIN_1 = 34; // ESP32 ADC1 pins (input only)
const int ANALOG_PIN_2 = 35;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("DIYables Bluetooth - ESP32 Pin Control/Monitor Example");
// Initialize pins
pinMode(LED_PIN, OUTPUT);
pinMode(OUTPUT_PIN_1, OUTPUT);
pinMode(OUTPUT_PIN_2, OUTPUT);
pinMode(INPUT_PIN_1, INPUT_PULLUP);
pinMode(INPUT_PIN_2, INPUT_PULLUP);
// Initialize Bluetooth server with platform-specific implementation
bluetoothServer.begin();
// Add digital pins app to server
bluetoothServer.addApp(&bluetoothPins);
// Configure which pins are accessible via Bluetooth with custom names
bluetoothPins.enablePin(LED_PIN, BT_PIN_OUTPUT, "LED");
bluetoothPins.enablePin(OUTPUT_PIN_1, BT_PIN_OUTPUT, "Out1");
bluetoothPins.enablePin(OUTPUT_PIN_2, BT_PIN_OUTPUT, "Out2");
bluetoothPins.enablePin(INPUT_PIN_1, BT_PIN_INPUT, "Btn1");
bluetoothPins.enablePin(INPUT_PIN_2, BT_PIN_INPUT, "Btn2");
bluetoothPins.enablePin(ANALOG_PIN_1, BT_PIN_INPUT, "A34");
bluetoothPins.enablePin(ANALOG_PIN_2, BT_PIN_INPUT, "A35");
// Set up connection event callbacks
bluetoothServer.setOnConnected([]() {
Serial.println("Bluetooth connected!");
});
bluetoothServer.setOnDisconnected([]() {
Serial.println("Bluetooth disconnected!");
});
// Set up callback for pin write commands
bluetoothPins.onPinWrite([](int pin, int state) {
digitalWrite(pin, state);
Serial.print("Pin ");
Serial.print(pin);
Serial.print(" set to ");
Serial.println(state ? "HIGH" : "LOW");
});
// Set up callback for pin read commands
bluetoothPins.onPinRead([](int pin) -> int {
// Read analog pins with analogRead, digital pins with digitalRead
int state;
if (pin == ANALOG_PIN_1 || pin == ANALOG_PIN_2) {
state = analogRead(pin);
Serial.print("Analog pin ");
Serial.print(pin);
Serial.print(" read: ");
Serial.println(state);
} else {
state = digitalRead(pin);
Serial.print("Digital pin ");
Serial.print(pin);
Serial.print(" read: ");
Serial.println(state ? "HIGH" : "LOW");
}
return state;
});
// Set up callback for pin mode changes
bluetoothPins.onPinModeChange([](int pin, int mode) {
pinMode(pin, mode == BT_PIN_OUTPUT ? OUTPUT : INPUT_PULLUP);
Serial.print("Pin ");
Serial.print(pin);
Serial.print(" mode changed to ");
Serial.println(mode == BT_PIN_OUTPUT ? "OUTPUT" : "INPUT");
});
Serial.println("Waiting for Bluetooth connection...");
Serial.print("Enabled pins: ");
Serial.println(bluetoothPins.getEnabledPinCount());
}
void loop() {
// Handle Bluetooth server communications
bluetoothServer.loop();
// Optional: Monitor input pins and send updates
static unsigned long lastInputCheck = 0;
static int lastInputState1 = HIGH;
static int lastInputState2 = HIGH;
static int lastAnalogState1 = 0;
static int lastAnalogState2 = 0;
if (millis() - lastInputCheck >= 100) {
lastInputCheck = millis();
// Check digital input pin 1
int currentState1 = digitalRead(INPUT_PIN_1);
if (currentState1 != lastInputState1) {
lastInputState1 = currentState1;
bluetoothPins.updatePinState(INPUT_PIN_1, currentState1);
Serial.print("Input pin ");
Serial.print(INPUT_PIN_1);
Serial.print(" changed to ");
Serial.println(currentState1 ? "HIGH" : "LOW");
}
// Check digital input pin 2
int currentState2 = digitalRead(INPUT_PIN_2);
if (currentState2 != lastInputState2) {
lastInputState2 = currentState2;
bluetoothPins.updatePinState(INPUT_PIN_2, currentState2);
Serial.print("Input pin ");
Serial.print(INPUT_PIN_2);
Serial.print(" changed to ");
Serial.println(currentState2 ? "HIGH" : "LOW");
}
// Check analog input 1 (send update if changed by more than 50 - ESP32 has 12-bit ADC)
int currentAnalog1 = analogRead(ANALOG_PIN_1);
if (abs(currentAnalog1 - lastAnalogState1) > 50) {
lastAnalogState1 = currentAnalog1;
bluetoothPins.updatePinState(ANALOG_PIN_1, currentAnalog1);
Serial.print("Analog pin ");
Serial.print(ANALOG_PIN_1);
Serial.print(" changed to ");
Serial.println(currentAnalog1);
}
// Check analog input 2 (send update if changed by more than 50)
int currentAnalog2 = analogRead(ANALOG_PIN_2);
if (abs(currentAnalog2 - lastAnalogState2) > 50) {
lastAnalogState2 = currentAnalog2;
bluetoothPins.updatePinState(ANALOG_PIN_2, currentAnalog2);
Serial.print("Analog pin ");
Serial.print(ANALOG_PIN_2);
Serial.print(" changed to ");
Serial.println(currentAnalog2);
}
}
delay(10);
}
- Arduino IDE에서 업로드 버튼을 클릭하여 ESP32에 코드를 업로드합니다.
- 시리얼 모니터를 엽니다.
- 시리얼 모니터에서 결과를 확인합니다. 다음과 같이 표시됩니다:
8
Serial.println("Hello World!");
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
DIYables Bluetooth - ESP32 Pin Control/Monitor Example
Waiting for Bluetooth connection...
Enabled pins: 7
ESP32 BLE 코드 (Android과 iOS 앱 모두에서 작동)
- Arduino IDE에서 파일 예제 DIYables Bluetooth Esp32BLE_PinControl 예제로 이동하거나, 위의 코드를 복사하여 Arduino IDE 편집기에 붙여넣습니다.
/*
* DIYables Bluetooth Library - ESP32 BLE Pin Control/Monitor Example
* Works with DIYables Bluetooth STEM app on Android and iOS
*
* This example demonstrates the Bluetooth Pin Control/Monitor feature:
* - Control digital output pins via Bluetooth
* - Monitor digital input pins
* - Monitor analog input pins
* - Configure pin modes (INPUT/OUTPUT)
*
* 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
*
* 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 control pins
*
* Tutorial: https://diyables.io/bluetooth-app
* Author: DIYables
*/
#include <DIYables_BluetoothServer.h>
#include <DIYables_BluetoothPinControl.h>
#include <platforms/DIYables_Esp32BLE.h>
// BLE Configuration
const char* DEVICE_NAME = "ESP32BLE_Pins";
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 Pin Control/Monitor app instance
DIYables_BluetoothPinControl bluetoothPins;
// Pin configuration (ESP32 GPIOs)
const int LED_PIN = 2; // Built-in LED
const int OUTPUT_PIN_1 = 16;
const int OUTPUT_PIN_2 = 17;
const int INPUT_PIN_1 = 25;
const int INPUT_PIN_2 = 26;
const int ANALOG_PIN_1 = 34; // Input-only ADC pin
const int ANALOG_PIN_2 = 35; // Input-only ADC pin
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("DIYables Bluetooth - ESP32 BLE Pin Control/Monitor Example");
// Initialize pins
pinMode(LED_PIN, OUTPUT);
pinMode(OUTPUT_PIN_1, OUTPUT);
pinMode(OUTPUT_PIN_2, OUTPUT);
pinMode(INPUT_PIN_1, INPUT_PULLUP);
pinMode(INPUT_PIN_2, INPUT_PULLUP);
// Initialize Bluetooth server with platform-specific implementation
bluetoothServer.begin();
// Add digital pins app to server
bluetoothServer.addApp(&bluetoothPins);
// Configure which pins are accessible via Bluetooth with custom names
bluetoothPins.enablePin(LED_PIN, BT_PIN_OUTPUT, "LED");
bluetoothPins.enablePin(OUTPUT_PIN_1, BT_PIN_OUTPUT, "Out1");
bluetoothPins.enablePin(OUTPUT_PIN_2, BT_PIN_OUTPUT, "Out2");
bluetoothPins.enablePin(INPUT_PIN_1, BT_PIN_INPUT, "Btn1");
bluetoothPins.enablePin(INPUT_PIN_2, BT_PIN_INPUT, "Btn2");
bluetoothPins.enablePin(ANALOG_PIN_1, BT_PIN_INPUT, "A34");
bluetoothPins.enablePin(ANALOG_PIN_2, BT_PIN_INPUT, "A35");
// Set up connection event callbacks
bluetoothServer.setOnConnected([]() {
Serial.println("Bluetooth connected!");
});
bluetoothServer.setOnDisconnected([]() {
Serial.println("Bluetooth disconnected!");
});
// Set up callback for pin write commands
bluetoothPins.onPinWrite([](int pin, int state) {
digitalWrite(pin, state);
Serial.print("Pin ");
Serial.print(pin);
Serial.print(" set to ");
Serial.println(state ? "HIGH" : "LOW");
});
// Set up callback for pin read commands
bluetoothPins.onPinRead([](int pin) -> int {
int state;
if (pin == ANALOG_PIN_1 || pin == ANALOG_PIN_2) {
state = analogRead(pin);
Serial.print("Analog pin ");
Serial.print(pin);
Serial.print(" read: ");
Serial.println(state);
} else {
state = digitalRead(pin);
Serial.print("Digital pin ");
Serial.print(pin);
Serial.print(" read: ");
Serial.println(state ? "HIGH" : "LOW");
}
return state;
});
// Set up callback for pin mode changes
bluetoothPins.onPinModeChange([](int pin, int mode) {
pinMode(pin, mode == BT_PIN_OUTPUT ? OUTPUT : INPUT_PULLUP);
Serial.print("Pin ");
Serial.print(pin);
Serial.print(" mode changed to ");
Serial.println(mode == BT_PIN_OUTPUT ? "OUTPUT" : "INPUT");
});
Serial.println("Waiting for Bluetooth connection...");
Serial.print("Enabled pins: ");
Serial.println(bluetoothPins.getEnabledPinCount());
}
void loop() {
bluetoothServer.loop();
static unsigned long lastInputCheck = 0;
static int lastInputState1 = HIGH;
static int lastInputState2 = HIGH;
static int lastAnalogState1 = 0;
static int lastAnalogState2 = 0;
if (millis() - lastInputCheck >= 100) {
lastInputCheck = millis();
int currentState1 = digitalRead(INPUT_PIN_1);
if (currentState1 != lastInputState1) {
lastInputState1 = currentState1;
bluetoothPins.updatePinState(INPUT_PIN_1, currentState1);
Serial.print("Input pin ");
Serial.print(INPUT_PIN_1);
Serial.print(" changed to ");
Serial.println(currentState1 ? "HIGH" : "LOW");
}
int currentState2 = digitalRead(INPUT_PIN_2);
if (currentState2 != lastInputState2) {
lastInputState2 = currentState2;
bluetoothPins.updatePinState(INPUT_PIN_2, currentState2);
Serial.print("Input pin ");
Serial.print(INPUT_PIN_2);
Serial.print(" changed to ");
Serial.println(currentState2 ? "HIGH" : "LOW");
}
// ESP32 has 12-bit ADC (0-4095)
int currentAnalog1 = analogRead(ANALOG_PIN_1);
if (abs(currentAnalog1 - lastAnalogState1) > 40) { // ~1% of 4095
lastAnalogState1 = currentAnalog1;
bluetoothPins.updatePinState(ANALOG_PIN_1, currentAnalog1);
Serial.print("Analog pin ");
Serial.print(ANALOG_PIN_1);
Serial.print(" changed to ");
Serial.println(currentAnalog1);
}
int currentAnalog2 = analogRead(ANALOG_PIN_2);
if (abs(currentAnalog2 - lastAnalogState2) > 40) {
lastAnalogState2 = currentAnalog2;
bluetoothPins.updatePinState(ANALOG_PIN_2, currentAnalog2);
Serial.print("Analog pin ");
Serial.print(ANALOG_PIN_2);
Serial.print(" changed to ");
Serial.println(currentAnalog2);
}
}
delay(10);
}
- Arduino IDE에서 업로드 버튼을 클릭하여 ESP32에 코드를 업로드합니다.
- 시리얼 모니터를 엽니다.
- 시리얼 모니터에서 결과를 확인합니다. 다음과 같이 표시됩니다:
8
Serial.println("Hello World!");
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
DIYables Bluetooth - ESP32 BLE Pin Control/Monitor Example
Waiting for Bluetooth connection...
Enabled pins: 7
모바일 앱
- ESP32 클래식 블루투스 코드를 사용하는 경우, 앱을 열기 전에 Android 폰과 ESP32를 페어링해야 합니다:
- 폰의 설정 > 블루투스로 이동합니다.
- 블루투스가 켜져 있는지 확인합니다.
- 폰이 사용 가능한 기기를 검색합니다.
- 사용 가능한 기기 목록에서 "ESP32_Pins"을 찾아 탭합니다.
- 페어링 요청을 확인합니다(PIN 필요 없음).
- 기기 이름 아래에 "페어링됨"이 표시될 때까지 기다립니다.
- ESP32 BLE 코드를 사용하는 경우, 페어링이 필요 없습니다. 다음 단계로 진행하면 됩니다.
- DIYables 블루투스 앱을 엽니다.
- 앱을 처음 열 때 권한을 요청합니다. 다음을 허용해 주세요:
- 주변 기기 권한 (Android 12+) / 블루투스 권한 (iOS) - 블루투스 기기를 검색하고 연결하는 데 필요
- 위치 권한 (Android 11 이하만 해당) - BLE 기기 검색을 위해 이전 Android 버전에서 필요
- 폰에서 블루투스가 켜져 있는지 확인합니다.
- 홈 화면에서 연결 버튼을 탭합니다. 앱이 BLE 및 클래식 블루투스 기기를 모두 검색합니다.

- 검색 결과에서 기기를 찾아 탭하여 연결합니다:
- 클래식 블루투스의 경우: "ESP32_Pins" 탭
- BLE의 경우: "ESP32BLE_Pins" 탭
- 연결되면 앱이 자동으로 홈 화면으로 돌아갑니다. 앱 메뉴에서 Digital Pins 앱을 선택합니다.

참고: 홈 화면에서 설정 아이콘을 탭하여 홈 화면에서 앱을 숨기거나 표시할 수 있습니다. 자세한 내용은 DIYables 블루투스 앱 사용 설명서를 참조하세요.
- 출력 핀을 탭하여 토글하고, 입력 핀이 실시간으로 업데이트되는 것을 확인하세요.

Arduino IDE의 시리얼 모니터를 다시 보면 다음과 같이 표시됩니다:
8
Serial.println("Hello World!");
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Bluetooth connected!
Pin 2 set to HIGH
Pin 16 set to LOW
Input pin 18 changed to LOW
Analog pin 34 changed to 2048
- 앱에서 핀을 토글하고 시리얼 모니터에서 실시간 피드백을 확인하세요.
창의적인 커스터마이징 - 프로젝트에 맞게 코드 조정하기
핀 설정
커스텀 이름과 모드로 핀을 활성화합니다:
// Enable individual pins with mode and custom name
bluetoothPins.enablePin(2, BT_PIN_OUTPUT, "LED"); // Built-in LED
bluetoothPins.enablePin(16, BT_PIN_OUTPUT, "Relay1"); // Relay control
bluetoothPins.enablePin(17, BT_PIN_OUTPUT, "Relay2"); // Relay control
bluetoothPins.enablePin(18, BT_PIN_INPUT, "Button"); // Button input
bluetoothPins.enablePin(34, BT_PIN_INPUT, "Sensor"); // Analog sensor
// Disable a specific pin
bluetoothPins.disablePin(17);
// Check pin status
bool isEnabled = bluetoothPins.isPinEnabled(2);
int mode = bluetoothPins.getPinMode(2);
int count = bluetoothPins.getEnabledPinCount();
핀 쓰기 명령 처리
앱이 핀을 토글할 때 하드웨어를 제어하는 onPinWrite() 콜백을 사용합니다:
bluetoothPins.onPinWrite([](int pin, int state) {
digitalWrite(pin, state);
Serial.print("Pin ");
Serial.print(pin);
Serial.print(" set to ");
Serial.println(state ? "HIGH" : "LOW");
});
핀 읽기 명령 처리
핀 상태를 앱에 반환하는 onPinRead() 콜백을 사용합니다:
bluetoothPins.onPinRead([](int pin) -> int {
if (pin == 34 || pin == 35) {
// Read analog value for ADC pins
return analogRead(pin);
} else {
// Read digital value for other pins
return digitalRead(pin);
}
});
연결 이벤트 처리
bluetoothServer.setOnConnected([]() {
Serial.println("Bluetooth connected!");
});
bluetoothServer.setOnDisconnected([]() {
Serial.println("Bluetooth disconnected!");
// Safety: turn off all outputs when disconnected
digitalWrite(2, LOW);
digitalWrite(16, LOW);
digitalWrite(17, LOW);
});
디지털 핀 사용 방법
앱 인터페이스 컨트롤
DIYables 블루투스 앱의 디지털 핀 인터페이스에는 다음이 제공됩니다:
- 핀 목록: 이름과 현재 상태와 함께 활성화된 모든 핀 표시
- 토글 버튼: 출력 핀을 탭하여 HIGH/LOW 토글
- 입력 표시: 입력 핀 상태의 실시간 표시
- 아날로그 값: ADC 핀의 원시 아날로그 값 표시
핀 모드
- BT_PIN_OUTPUT (0): 디지털 출력 제어 — 앱에서 토글
- BT_PIN_INPUT (1): 디지털 또는 아날로그 입력 모니터링 — 현재 상태 표시