ESP32 Bluetooth Digital Pins Voorbeeld - Pinbesturing en Monitor Interface Tutorial
Overzicht
Het Bluetooth Digital Pins voorbeeld biedt bediening en monitoring van ESP32 GPIO-pinnen op afstand via de DIYables Bluetooth STEM-app. Ontworpen voor ESP32 bordjes met ondersteuning voor zowel BLE (Bluetooth Low Energy) als Classic Bluetooth verbindingen. Configureer pinnen als input of output, schakel output-pinnen in- en uit, lees digitale en analoge inputwaarden uit en ontvang real-time meldingen bij pinstatuswijzigingen — ideaal voor home automation, relaisbesturing, knopmonitoring en sensoruitlezing.
Dit voorbeeld ondersteunt twee Bluetooth-modi:
ESP32 BLE (Bluetooth Low Energy): Werkt op zowel Android als iOS
ESP32 Classic Bluetooth: Werkt alleen op Android. iOS ondersteunt geen Classic Bluetooth. Gebruik BLE als u iOS-ondersteuning nodig heeft.
Kenmerken
Output Pin Besturing: Schakel digitale output-pinnen HIGH/LOW via de app
Input Pin Monitoring: Lees digitale en analoge inputpinstatussen uit
Aangepaste Pin Namen: Ken vriendelijke namen toe aan elke pin (bijv. "LED", "Btn1", "A34")
Real-Time Updates: Automatische meldingen bij verandering van inputpinstatussen
Tot 16 Pinnen: Ondersteuning voor maximaal 16 configureerbare pinnen tegelijk
Gemengde Modi: Combineer input- en outputpinnen in dezelfde setup
BLE & Classic Bluetooth: Kies de Bluetooth-modus die past bij uw project
Cross-Platform: BLE modus werkt op Android en iOS; Classic Bluetooth alleen Android
Openbaarmaking: Sommige van de links in deze sectie zijn Amazon-affiliate links. We kunnen een commissie ontvangen voor aankopen die via deze links worden gedaan, zonder extra kosten voor u. We waarderen uw steun.
Verbind het ESP32-board met uw computer via een USB-kabel.
Open de Arduino IDE op uw computer.
Selecteer het juiste ESP32-board en COM-poort.
Ga naar het Bibliotheken-icoon in de linkerzijbalk van de Arduino IDE.
Zoek op "DIYables Bluetooth" en selecteer de DIYables Bluetooth bibliotheek van DIYables.
Klik op de Installeren-knop om de bibliotheek te installeren.
U wordt gevraagd om additionele bibliotheekafhankelijkheden te installeren.
Klik op de Alles installeren-knop om alle bibliotheekafhankelijkheden te installeren.
Kies één van de twee Bluetooth-modi hieronder, afhankelijk van uw behoeften:
ESP32 Classic Bluetooth Code (werkt alleen met app op Android)
Opmerking: Classic Bluetooth wordt NIET ondersteund op iOS. Als u iOS-ondersteuning nodig heeft, gebruik dan de BLE-code hieronder.
Ga in Arduino IDE naar Bestand Voorbeelden DIYables Bluetooth Esp32Bluetooth_PinControl, of kopieer de bovenstaande code en plak deze in de editor van 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 instancesDIYables_Esp32Bluetooth bluetooth("ESP32_Pins");DIYables_BluetoothServer bluetoothServer(bluetooth);// Create Pin Control/Monitor app instanceDIYables_BluetoothPinControl bluetoothPins;// Pin configuration (adjust for your ESP32 board)constint LED_PIN = 2; // Built-in LED on most ESP32 boardsconstint OUTPUT_PIN_1 = 16;constint OUTPUT_PIN_2 = 17;constint INPUT_PIN_1 = 18;constint INPUT_PIN_2 = 19;constint ANALOG_PIN_1 = 34; // ESP32 ADC1 pins (input only)constint ANALOG_PIN_2 = 35;voidsetup() {Serial.begin(115200);delay(1000);Serial.println("DIYables Bluetooth - ESP32 Pin Control/Monitor Example");// Initialize pinspinMode(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 digitalReadint 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());}voidloop() {// Handle Bluetooth server communications bluetoothServer.loop();// Optional: Monitor input pins and send updatesstaticunsignedlong lastInputCheck = 0;staticint lastInputState1 = HIGH;staticint lastInputState2 = HIGH;staticint lastAnalogState1 = 0;staticint lastAnalogState2 = 0;if (millis() - lastInputCheck >= 100) { lastInputCheck = millis();// Check digital input pin 1int 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 2int 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);}
Klik op de Upload-knop in Arduino IDE om de code naar de ESP32 te uploaden.
Open de Seriële Monitor.
Bekijk het resultaat in de Seriële Monitor. Het ziet er als volgt uit:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
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
Ln 11, Col 1
ESP32 Dev Module on COM15
2
ESP32 BLE Code (werkt met app op zowel Android als iOS)
Ga in Arduino IDE naar Bestand Voorbeelden DIYables Bluetooth Esp32BLE_PinControl, of kopieer de bovenstaande code en plak deze in de editor van 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 Configurationconst 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 instancesDIYables_Esp32BLE bluetooth(DEVICE_NAME, SERVICE_UUID, TX_UUID, RX_UUID);DIYables_BluetoothServer bluetoothServer(bluetooth);// Create Pin Control/Monitor app instanceDIYables_BluetoothPinControl bluetoothPins;// Pin configuration (ESP32 GPIOs)constint LED_PIN = 2; // Built-in LEDconstint OUTPUT_PIN_1 = 16;constint OUTPUT_PIN_2 = 17;constint INPUT_PIN_1 = 25;constint INPUT_PIN_2 = 26;constint ANALOG_PIN_1 = 34; // Input-only ADC pinconstint ANALOG_PIN_2 = 35; // Input-only ADC pinvoidsetup() {Serial.begin(115200);delay(1000);Serial.println("DIYables Bluetooth - ESP32 BLE Pin Control/Monitor Example");// Initialize pinspinMode(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());}voidloop() { bluetoothServer.loop();staticunsignedlong lastInputCheck = 0;staticint lastInputState1 = HIGH;staticint lastInputState2 = HIGH;staticint lastAnalogState1 = 0;staticint 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);}
Klik op de Upload-knop in Arduino IDE om de code naar de ESP32 te uploaden.
Open de Seriële Monitor.
Bekijk het resultaat in de Seriële Monitor. Het ziet er als volgt uit:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
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
Ln 11, Col 1
ESP32 Dev Module on COM15
2
Mobiele App
Installeer de DIYables Bluetooth App op uw smartphone: Android | iOS
Als u de ESP32 Classic Bluetooth code gebruikt, moet u het ESP32 eerst koppelen met uw Android-telefoon voordat u de app opent:
Ga naar de Instellingen > Bluetooth op uw telefoon.
Zorg dat Bluetooth aan staat.
Uw telefoon zoekt naar beschikbare apparaten.
Zoek en tik op "ESP32_Pins" in de lijst met beschikbare apparaten.
Bevestig het koppelingsverzoek (geen pincode nodig).
Wacht tot het apparaat "Gekoppeld" weergeeft onder de naam.
Als u de ESP32 BLE code gebruikt, is koppelen niet nodig. Ga direct door naar de volgende stap.
Open de DIYables Bluetooth App.
Bij het eerste gebruik vraagt de app om toestemming. Verleen de volgende:
Nearby Devices toestemming (Android 12+) / Bluetooth toestemming (iOS) — vereist om te scannen en verbinding te maken met Bluetooth-apparaten
Locatie toestemming (alleen Android 11 en lager) — vereist door oudere Android-versies om BLE-apparaten te scannen
Zorg dat Bluetooth aanstaat op uw telefoon.
Tik op het startscherm op de Verbinden-knop. De app zoekt naar zowel BLE als Classic Bluetooth apparaten.
Zoek en tik op uw apparaat in de zoekresultaten om te verbinden:
Voor Classic Bluetooth: tik op "ESP32_Pins"
Voor BLE: tik op "ESP32BLE_Pins"
Na verbinding keert de app automatisch terug naar het startscherm. Selecteer de Digital Pins app in het app-menu.
U bent welkom om de link naar deze tutorial te delen. Gebruik onze inhoud echter niet op andere websites. We hebben veel moeite en tijd gestoken in het maken van de inhoud, respecteer alstublieft ons werk!