ESP32 Bluetooth Monitor Voorbeeld - Realtime Seriële Monitor Interface Tutorial
Overzicht
Het Bluetooth Monitor voorbeeld biedt een draadloze seriële monitorinterface die toegankelijk is via de DIYables Bluetooth STEM app. Ontworpen voor ESP32 boards met ondersteuning voor zowel BLE (Bluetooth Low Energy) als Classic Bluetooth verbindingen. Stream realtime statusberichten naar de app, ontvang en verwerk tekstcommando’s, toon systeeminformatie en debug uw projecten draadloos — perfect voor ongebonden monitoring, remote debugging en systeemstatusdisplays.
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 Classic Bluetooth niet. Gebruik BLE als u iOS-ondersteuning nodig heeft.
Kenmerken
Realtime streaming: Continue statusberichten verzenden naar het app-scherm
Commandoverwerking: Ontvang en verwerk tekstcommando’s van de app
Systeemmonitoring: Toon uptime, vrije heap, CPU-info en meer
LED-besturing: Ingebouwde LED AAN/UIT commando’s voor snelle tests
Periodieke updates: Automatische heartbeat en statusberichten op configureerbare intervallen
BLE & Classic Bluetooth: Kies de Bluetooth-modus die het beste bij uw project past
Cross-platform: BLE modus werkt op Android en iOS; Classic Bluetooth alleen op Android
Laag stroomverbruik optie: BLE modus verbruikt minder stroom dan Classic Bluetooth
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.
Start de Arduino IDE op uw computer.
Selecteer het juiste ESP32 board en de COM-poort.
Navigeer in de linkerzijbalk van de Arduino IDE naar het Libraries-icoon.
Zoek op "DIYables Bluetooth" en vind de DIYables Bluetooth bibliotheek van DIYables.
Klik op de Installeren-knop om de bibliotheek te installeren.
Er wordt u gevraagd enkele andere bibliotheekafhankelijkheden te installeren.
Klik op de Installeer alles-knop om alle afhankelijkheden te installeren.
Kies een van de twee Bluetooth modi hieronder, afhankelijk van uw behoeften:
ESP32 Classic Bluetooth Code (werkt alleen met app op Android)
Let op: Classic Bluetooth wordt NIET ondersteund op iOS. Heeft u iOS-ondersteuning nodig, gebruik dan de BLE-code hieronder.
Ga in de Arduino IDE naar Bestand Voorbeelden DIYables Bluetooth Esp32Bluetooth_Monitor voorbeeld, of kopieer de bovenstaande code en plak deze in de Arduino IDE editor.
/* * DIYables Bluetooth Library - ESP32 Classic Bluetooth Monitor 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 Monitor feature: * - Send real-time status messages to the mobile app * - Display system information and sensor readings * - Receive and process commands from the app * - Perfect for debugging and system monitoring * * 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 view monitor output * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */#include <DIYables_BluetoothServer.h>#include <DIYables_BluetoothMonitor.h>#include <platforms/DIYables_Esp32Bluetooth.h>// Create Bluetooth instancesDIYables_Esp32Bluetooth bluetooth("ESP32_Monitor");DIYables_BluetoothServer bluetoothServer(bluetooth);// Create Monitor app instanceDIYables_BluetoothMonitor bluetoothMonitor;// Variables for demounsignedlong lastUpdate = 0;constunsignedlong UPDATE_INTERVAL = 3000; // Send update every 3 secondsint messageCount = 0;bool ledState = false;// ESP32 built-in LED (may vary by board)constint LED_PIN = 2;voidsetup() {Serial.begin(115200);delay(1000);Serial.println("DIYables Bluetooth - ESP32 Monitor Example");// Initialize LEDpinMode(LED_PIN, OUTPUT);digitalWrite(LED_PIN, LOW);// Initialize Bluetooth server with platform-specific implementation bluetoothServer.begin();// Add monitor app to server bluetoothServer.addApp(&bluetoothMonitor);// Set up connection event callbacks bluetoothServer.setOnConnected([]() {Serial.println("Bluetooth connected!"); bluetoothMonitor.send("=== ESP32 Monitor Connected ==="); bluetoothMonitor.send("System Ready"); bluetoothMonitor.send("Type HELP for available commands"); bluetoothMonitor.send(""); }); bluetoothServer.setOnDisconnected([]() {Serial.println("Bluetooth disconnected!"); });// Set up message handler for incoming commands bluetoothMonitor.onMonitorMessage([](const String& message) {Serial.print("Received command: ");Serial.println(message); handleCommand(message); });Serial.println("Waiting for Bluetooth connection...");}void handleCommand(const String& cmd) {if (cmd == "HELP") { bluetoothMonitor.send("Available Commands:"); bluetoothMonitor.send(" LED_ON - Turn LED on"); bluetoothMonitor.send(" LED_OFF - Turn LED off"); bluetoothMonitor.send(" STATUS - Show system status"); bluetoothMonitor.send(" HEAP - Show memory info"); bluetoothMonitor.send(" CLEAR - Clear monitor (if supported)"); bluetoothMonitor.send(" HELP - Show this help"); }elseif (cmd == "LED_ON") {digitalWrite(LED_PIN, HIGH); ledState = true; bluetoothMonitor.send("✓ LED turned ON"); }elseif (cmd == "LED_OFF") {digitalWrite(LED_PIN, LOW); ledState = false; bluetoothMonitor.send("✓ LED turned OFF"); }elseif (cmd == "STATUS") { showStatus(); }elseif (cmd == "HEAP") { bluetoothMonitor.send("=== Memory Info ==="); bluetoothMonitor.send("Free Heap: " + String(ESP.getFreeHeap()) + " bytes"); bluetoothMonitor.send("Min Free Heap: " + String(ESP.getMinFreeHeap()) + " bytes"); bluetoothMonitor.send("Heap Size: " + String(ESP.getHeapSize()) + " bytes"); bluetoothMonitor.send("==================="); }elseif (cmd == "CLEAR") { bluetoothMonitor.send(""); }else { bluetoothMonitor.send("✗ Unknown command: " + cmd); bluetoothMonitor.send("Type HELP for available commands"); }}void showStatus() { bluetoothMonitor.send("=== System Status ===");// LED Status bluetoothMonitor.send("LED State: " + String(ledState ? "ON" : "OFF"));// Uptimeunsignedlong uptime = millis() / 1000; bluetoothMonitor.send("Uptime: " + String(uptime / 3600) + "h " + String((uptime % 3600) / 60) + "m " + String(uptime % 60) + "s");// ESP32-specific info bluetoothMonitor.send("Free Heap: " + String(ESP.getFreeHeap()) + " bytes"); bluetoothMonitor.send("CPU Freq: " + String(ESP.getCpuFreqMHz()) + " MHz"); bluetoothMonitor.send("Chip Model: " + String(ESP.getChipModel()));// Messages sent bluetoothMonitor.send("Messages Sent: " + String(messageCount)); bluetoothMonitor.send("====================");}void sendPeriodicUpdate() { messageCount++;// Example of different message typesif (messageCount % 3 == 0) { bluetoothMonitor.send("[INFO] Heartbeat #" + String(messageCount)); } elseif (messageCount % 5 == 0) { bluetoothMonitor.send("[HEAP] Free: " + String(ESP.getFreeHeap()) + " bytes"); }else { bluetoothMonitor.send("[TIME] Uptime: " + String(millis() / 1000) + "s"); }Serial.print("Sent update #");Serial.println(messageCount);}voidloop() {// Handle Bluetooth server communications bluetoothServer.loop();// Send periodic updates (only when connected)if (bluetooth.isConnected() && millis() - lastUpdate >= UPDATE_INTERVAL) { lastUpdate = millis(); sendPeriodicUpdate(); }delay(10);}
Klik op de Upload-knop om de code naar de ESP32 te uploaden.
Open de Seriële Monitor.
Bekijk het resultaat in de Seriële Monitor. Het ziet er ongeveer zo 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 Monitor Example
Waiting for Bluetooth connection...
Ln 11, Col 1
ESP32 Dev Module on COM15
2
ESP32 BLE Code (werkt met app op Android en iOS)
Ga in de Arduino IDE naar Bestand Voorbeelden DIYables Bluetooth Esp32BLE_Monitor voorbeeld, of kopieer de bovenstaande code en plak deze in de Arduino IDE editor.
/* * DIYables Bluetooth Library - ESP32 BLE Monitor Example * Works with DIYables Bluetooth STEM app on Android and iOS * * This example demonstrates the Bluetooth Monitor feature: * - Send real-time status messages to the mobile app * - Display system information and sensor readings * - Receive and process commands from the app * - Perfect for debugging and system monitoring * * 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 view monitor output * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */#include <DIYables_BluetoothServer.h>#include <DIYables_BluetoothMonitor.h>#include <platforms/DIYables_Esp32BLE.h>// BLE Configurationconst char* DEVICE_NAME = "ESP32BLE_Monitor";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 Monitor app instanceDIYables_BluetoothMonitor bluetoothMonitor;// Variables for demounsignedlong lastUpdate = 0;constunsignedlong UPDATE_INTERVAL = 3000;int messageCount = 0;bool ledState = false;voidsetup() {Serial.begin(115200);delay(1000);Serial.println("DIYables Bluetooth - ESP32 BLE Monitor Example");// Initialize built-in LEDpinMode(2, OUTPUT); // ESP32 built-in LED is usually on GPIO 2digitalWrite(2, LOW);// Initialize Bluetooth server with platform-specific implementation bluetoothServer.begin();// Add monitor app to server bluetoothServer.addApp(&bluetoothMonitor);// Set up connection event callbacks bluetoothServer.setOnConnected([]() {Serial.println("Bluetooth connected!"); bluetoothMonitor.send("=== ESP32 BLE Monitor Connected ==="); bluetoothMonitor.send("System Ready"); bluetoothMonitor.send("Type HELP for available commands"); bluetoothMonitor.send(""); }); bluetoothServer.setOnDisconnected([]() {Serial.println("Bluetooth disconnected!"); });// Set up message handler for incoming commands bluetoothMonitor.onMonitorMessage([](const String& message) {Serial.print("Received command: ");Serial.println(message); handleCommand(message); });Serial.println("Waiting for Bluetooth connection...");}void handleCommand(const String& cmd) {if (cmd == "HELP") { bluetoothMonitor.send("Available Commands:"); bluetoothMonitor.send(" LED_ON - Turn LED on"); bluetoothMonitor.send(" LED_OFF - Turn LED off"); bluetoothMonitor.send(" STATUS - Show system status"); bluetoothMonitor.send(" HEAP - Show free heap memory"); bluetoothMonitor.send(" HELP - Show this help"); }elseif (cmd == "LED_ON") {digitalWrite(2, HIGH); ledState = true; bluetoothMonitor.send("LED turned ON"); }elseif (cmd == "LED_OFF") {digitalWrite(2, LOW); ledState = false; bluetoothMonitor.send("LED turned OFF"); }elseif (cmd == "STATUS") { showStatus(); }elseif (cmd == "HEAP") { bluetoothMonitor.send("Free heap: " + String(ESP.getFreeHeap()) + " bytes"); }else { bluetoothMonitor.send("Unknown command: " + cmd); bluetoothMonitor.send("Type HELP for available commands"); }}void showStatus() { bluetoothMonitor.send("=== System Status ==="); bluetoothMonitor.send("LED State: " + String(ledState ? "ON" : "OFF"));unsignedlong uptime = millis() / 1000; bluetoothMonitor.send("Uptime: " + String(uptime / 3600) + "h " + String((uptime % 3600) / 60) + "m " + String(uptime % 60) + "s"); bluetoothMonitor.send("Free Heap: " + String(ESP.getFreeHeap()) + " bytes"); bluetoothMonitor.send("Messages Sent: " + String(messageCount)); bluetoothMonitor.send("====================");}void sendPeriodicUpdate() { messageCount++;if (messageCount % 3 == 0) { bluetoothMonitor.send("[INFO] Heartbeat #" + String(messageCount)); } elseif (messageCount % 5 == 0) { bluetoothMonitor.send("[HEAP] Free: " + String(ESP.getFreeHeap()) + " bytes"); }else { bluetoothMonitor.send("[TIME] Uptime: " + String(millis() / 1000) + "s"); }Serial.print("Sent update #");Serial.println(messageCount);}voidloop() { bluetoothServer.loop();if (bluetooth.isConnected() && millis() - lastUpdate >= UPDATE_INTERVAL) { lastUpdate = millis(); sendPeriodicUpdate(); }delay(10);}
Klik op de Upload-knop om de code naar de ESP32 te uploaden.
Open de Seriële Monitor.
Bekijk het resultaat in de Seriële Monitor. Het ziet er ongeveer zo 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 Monitor Example
Waiting for Bluetooth connection...
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 de ESP32 koppelen met uw Android-telefoon voordat u de app opent:
Ga naar de Instellingen > Bluetooth van uw telefoon
Zorg dat Bluetooth is ingeschakeld
Uw telefoon zoekt naar beschikbare apparaten
Zoek en tik op "ESP32_Monitor" in de lijst met beschikbare apparaten
Bevestig het koppelingsverzoek (geen pincode nodig)
Wacht tot er "Gekoppeld" onder de apparaattnaam verschijnt
Gebruikt u de ESP32 BLE code, dan is koppelen niet nodig. Ga gewoon door met de volgende stap.
Open de DIYables Bluetooth App
Bij de eerste keer openen vraagt de app om permissies. Verleen alstublieft:
Nabije apparaten toestemming (Android 12+) / Bluetooth toestemming (iOS) - nodig om te scannen en verbinding te maken met Bluetooth-apparaten
Locatie toestemming (Android 11 en lager) - vereist door oudere Android-versies voor BLE-scanning
Zorg dat Bluetooth aan staat op uw telefoon
Tik op het startscherm op de Verbinden-knop. De app zoekt naar BLE en Classic Bluetooth apparaten.
Zoek en tik uw apparaat aan in de zoekresultaten om verbinding te maken:
Voor Classic Bluetooth: tik "ESP32_Monitor"
Voor BLE: tik "ESP32BLE_Monitor"
Na verbinding keert de app automatisch terug naar het startscherm. Selecteer de Monitor app in het app-menu.
Let op: U kunt op het instellingenicoon op het startscherm tikken om apps te verbergen/tonen op het startscherm. Voor meer informatie, zie de DIYables Bluetooth App Gebruikershandleiding.
U ziet statusberichten binnenkomen in het monitor-scherm
Typ LED_ON in het invoerveld en tik op Verzenden — de ingebouwde LED van de ESP32 gaat dan AAN en de monitor toont een bevestigingsbericht
Kijk nu opnieuw naar de Seriële Monitor in de Arduino IDE. U ziet:
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
Bluetooth connected!
Sent update #1
Sent update #2
Received command: HELP
Received command: STATUS
Ln 11, Col 1
ESP32 Dev Module on COM15
2
Typ commando’s in de app (HELP, STATUS, LED_ON, LED_OFF, HEAP) en observeer de reacties
Creatieve Aanpassing - Pas de Code aan op Uw Project
Berichten naar de App Verzenden
Gebruik de send() methode om tekstberichten naar het monitor-scherm te streamen:
U kunt zo veel aangepaste commando’s toevoegen als u wilt door meer elseif blokken toe te voegen. Voeg bijvoorbeeld RELAY_ON / RELAY_OFF toe om een relais te bedienen, of READ om een sensorwaarde op te vragen — elk woord dat u in de app typt wordt een commando.
Verbinding Gebeurtenissen Afhandelen
U kunt detecteren wanneer de app verbinding maakt of verbreekt met de ESP32:
bluetoothServer.setOnConnected([]() { Serial.println("Bluetooth connected!"); bluetoothMonitor.send("=== ESP32 Monitor Connected ==="); bluetoothMonitor.send("System Ready"); bluetoothMonitor.send("Type HELP for available commands"); }); bluetoothServer.setOnDisconnected([]() { Serial.println("Bluetooth disconnected!"); }); if (bluetoothServer.isConnected()) { bluetoothMonitor.send("Status update"); }
Hoe de Monitor te Gebruiken
App Interface Bedieningselementen
De monitorinterface in de DIYables Bluetooth App biedt:
Berichtweergave: Scrollbare lijst van ontvangen berichten met auto-scroll
Tekstinvoer: Typ commando’s onderaan
Verzendknop: Tik om het getypte commando naar de ESP32 te sturen
Ingebouwde Commando’s
De voorbeeldcode bevat deze ingebouwde commando’s:
HELP ? Toont alle beschikbare commando’s
LED_ON ? Zet de ingebouwde LED aan
LED_OFF ? Zet de ingebouwde LED uit
STATUS ? Toont systeemstatus (LED status, uptime, heap, verzonden berichten)
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!