ESP32 Bluetooth Tabel Voorbeeld - Interface voor Gestructureerde Gegevensweergave Tutorial
Overzicht
Het Bluetooth Tabel voorbeeld biedt een gestructureerde sleutel-waarde gegevensweergave 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. Definieer benoemde rijen en update hun waarden in realtime — ideaal voor dashboards, sensortoestandpanelen, systeemmonitoren en elke toepassing die georganiseerde datavisualisatie vereist.
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 indien iOS-ondersteuning nodig is.
Kenmerken
Gestructureerde Rijen: Tot 20 benoemde attribuutrijen
Realtime Updates: Update individuele rijwaarden zonder de hele tabel te verversen
Benoemde Attributen: Elke rij heeft een beschrijvend label (bijv. "Temperature", "Status")
Dynamische Waarden: Verzend elke tekenreekswaarde per rij
Tabelstructuur Synchronisatie: App vraagt tabelconfiguratie op bij verbinden
BLE & Classic Bluetooth: Kies de Bluetooth-modus die bij uw project past
Cross-Platform: BLE werkt op Android en iOS; Classic Bluetooth alleen op Android
Laag stroomverbruik optie: BLE 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 de ESP32 met uw computer via een USB-kabel.
Start de Arduino IDE op uw computer.
Selecteer het juiste ESP32 board en COM-poort.
Ga naar het Libraries icoon in de linker zijbalk van de Arduino IDE.
Zoek op "DIYables Bluetooth" en vind de DIYables Bluetooth bibliotheek van DIYables.
Klik op de Install knop om de bibliotheek te installeren.
Er wordt gevraagd om aanvullende bibliotheekafhankelijkheden te installeren.
Klik op de Install All knop om alle afhankelijkheden 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. Gebruik de BLE-code hieronder als u iOS-ondersteuning nodig heeft.
In Arduino IDE, ga naar File Examples DIYables Bluetooth Esp32Bluetooth_Table voorbeeld, of kopieer de bovenstaande code en plak deze in de editor van Arduino IDE
/* * DIYables Bluetooth Library - ESP32 Classic Bluetooth Table 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 Table feature: * - Display structured data in a two-column table * - Real-time value updates for each row * - Perfect for sensor dashboards and status displays * * 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 the table * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */#include <DIYables_BluetoothServer.h>#include <DIYables_BluetoothTable.h>#include <platforms/DIYables_Esp32Bluetooth.h>// Create Bluetooth instancesDIYables_Esp32Bluetooth bluetooth("ESP32_Table");DIYables_BluetoothServer bluetoothServer(bluetooth);// Create Table app instanceDIYables_BluetoothTable bluetoothTable;// Variables for demo dataunsignedlong lastUpdate = 0;constunsignedlong UPDATE_INTERVAL = 1000; // Update every secondint counter = 0;voidsetup() {Serial.begin(115200);delay(1000);Serial.println("DIYables Bluetooth - ESP32 Table Example");// Initialize Bluetooth server with platform-specific implementation bluetoothServer.begin();// Add table app to server bluetoothServer.addApp(&bluetoothTable);// Define table structure (add rows with attribute names) bluetoothTable.addRow("Temperature"); bluetoothTable.addRow("Humidity"); bluetoothTable.addRow("Pressure"); bluetoothTable.addRow("Counter"); bluetoothTable.addRow("Uptime"); bluetoothTable.addRow("Free Heap"); bluetoothTable.addRow("CPU Freq"); bluetoothTable.addRow("Status");Serial.print("Table rows defined: ");Serial.println(bluetoothTable.getRowCount());// Set up connection event callbacks bluetoothServer.setOnConnected([]() {Serial.println("Bluetooth connected!");// Send table structure bluetoothTable.sendTableStructure();// Send initial values updateTableValues(); }); bluetoothServer.setOnDisconnected([]() {Serial.println("Bluetooth disconnected!"); });// Optional: Handle requests for table data bluetoothTable.onDataRequest([]() {Serial.println("App requested table data"); bluetoothTable.sendTableStructure(); updateTableValues(); });Serial.println("Waiting for Bluetooth connection...");}void updateTableValues() {// TODO: Replace with actual sensor readings// Simulated temperature (20-30°C)float temperature = 20.0 + random(0, 100) / 10.0; bluetoothTable.sendValueUpdate("Temperature", String(temperature, 1) + " °C");// Simulated humidity (40-60%)int humidity = 40 + random(0, 21); bluetoothTable.sendValueUpdate("Humidity", String(humidity) + " %");// Simulated pressure (1000-1020 hPa)int pressure = 1000 + random(0, 21); bluetoothTable.sendValueUpdate("Pressure", String(pressure) + " hPa");// Counter value bluetoothTable.sendValueUpdate("Counter", String(counter)); counter++;// Uptime (in seconds)unsignedlong uptime = millis() / 1000;String uptimeStr = String(uptime / 3600) + "h " + String((uptime % 3600) / 60) + "m " + String(uptime % 60) + "s"; bluetoothTable.sendValueUpdate("Uptime", uptimeStr);// ESP32-specific: Free heap memory bluetoothTable.sendValueUpdate("Free Heap", String(ESP.getFreeHeap()) + " bytes");// ESP32-specific: CPU frequency bluetoothTable.sendValueUpdate("CPU Freq", String(ESP.getCpuFreqMHz()) + " MHz");// Status bluetoothTable.sendValueUpdate("Status", counter % 2 == 0 ? "Running" : "Active");Serial.println("Table values updated");}voidloop() {// Handle Bluetooth server communications bluetoothServer.loop();// Update table values periodically (only when connected)if (bluetooth.isConnected() && millis() - lastUpdate >= UPDATE_INTERVAL) { lastUpdate = millis(); updateTableValues(); }delay(10);}
Klik op de Upload knop in Arduino IDE om de code naar ESP32 te uploaden
Open de Serial Monitor
Bekijk het resultaat in de Serial 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 Table Example
Waiting for Bluetooth connection...
Ln 11, Col 1
ESP32 Dev Module on COM15
2
ESP32 BLE Code (werkt met app op zowel Android als iOS)
In Arduino IDE, ga naar File Examples DIYables Bluetooth Esp32BLE_Table voorbeeld, of kopieer de bovenstaande code en plak deze in de editor van Arduino IDE
/* * DIYables Bluetooth Library - ESP32 BLE Table Example * Works with DIYables Bluetooth STEM app on Android and iOS * * This example demonstrates the Bluetooth Table feature: * - Display structured data in a two-column table * - Real-time value updates for each row * - Perfect for sensor dashboards and status displays * * 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 the table * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */#include <DIYables_BluetoothServer.h>#include <DIYables_BluetoothTable.h>#include <platforms/DIYables_Esp32BLE.h>// BLE Configurationconst char* DEVICE_NAME = "ESP32BLE_Table";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 Table app instanceDIYables_BluetoothTable bluetoothTable;// Variables for demo dataunsignedlong lastUpdate = 0;constunsignedlong UPDATE_INTERVAL = 1000;int counter = 0;voidsetup() {Serial.begin(115200);delay(1000);Serial.println("DIYables Bluetooth - ESP32 BLE Table Example");// Initialize Bluetooth server with platform-specific implementation bluetoothServer.begin();// Add table app to server bluetoothServer.addApp(&bluetoothTable);// Define table structure bluetoothTable.addRow("Temperature"); bluetoothTable.addRow("Humidity"); bluetoothTable.addRow("Pressure"); bluetoothTable.addRow("Counter"); bluetoothTable.addRow("Uptime"); bluetoothTable.addRow("Free Heap"); bluetoothTable.addRow("Status");Serial.print("Table rows defined: ");Serial.println(bluetoothTable.getRowCount());// Set up connection event callbacks bluetoothServer.setOnConnected([]() {Serial.println("Bluetooth connected!"); bluetoothTable.sendTableStructure(); updateTableValues(); }); bluetoothServer.setOnDisconnected([]() {Serial.println("Bluetooth disconnected!"); }); bluetoothTable.onDataRequest([]() {Serial.println("App requested table data"); bluetoothTable.sendTableStructure(); updateTableValues(); });Serial.println("Waiting for Bluetooth connection...");}void updateTableValues() {float temperature = 20.0 + random(0, 100) / 10.0; bluetoothTable.sendValueUpdate("Temperature", String(temperature, 1) + " °C");int humidity = 40 + random(0, 21); bluetoothTable.sendValueUpdate("Humidity", String(humidity) + " %");int pressure = 1000 + random(0, 21); bluetoothTable.sendValueUpdate("Pressure", String(pressure) + " hPa"); bluetoothTable.sendValueUpdate("Counter", String(counter)); counter++;unsignedlong uptime = millis() / 1000;String uptimeStr = String(uptime / 3600) + "h " + String((uptime % 3600) / 60) + "m " + String(uptime % 60) + "s"; bluetoothTable.sendValueUpdate("Uptime", uptimeStr); bluetoothTable.sendValueUpdate("Free Heap", String(ESP.getFreeHeap()) + " bytes"); bluetoothTable.sendValueUpdate("Status", counter % 2 == 0 ? "Running" : "Active");Serial.println("Table values updated");}voidloop() { bluetoothServer.loop();if (bluetooth.isConnected() && millis() - lastUpdate >= UPDATE_INTERVAL) { lastUpdate = millis(); updateTableValues(); }delay(10);}
Klik op de Upload knop in Arduino IDE om de code naar ESP32 te uploaden
Open de Serial Monitor
Bekijk het resultaat in de Serial 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 Table 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
Wanneer u de ESP32 Classic Bluetooth code gebruikt, moet u de ESP32 eerst met uw Android-telefoon koppelen voordat u de app opent:
Ga op uw telefoon naar Instellingen > Bluetooth
Zorg dat Bluetooth aan staat
Uw telefoon zoekt naar beschikbare apparaten
Zoek en tik op "ESP32_Table" in de lijst met beschikbare apparaten
Bevestig het verbindingsverzoek (geen PIN nodig)
Wacht tot de status "Gepaired" onder de apparaatte naam verschijnt
Wanneer u de ESP32 BLE code gebruikt, is koppelen niet nodig. Ga direct door naar de volgende stap.
Open de DIYables Bluetooth App
Bij het openen van de app voor de eerste keer wordt om permissies gevraagd. Verleen alstublieft:
Nearby Devices permissie (Android 12+) / Bluetooth permissie (iOS) - nodig om te scannen en verbinden met Bluetooth-apparaten
Locatie permissie (alleen Android 11 en lager) - vereist door oudere Android versies voor het scannen van BLE-apparaten
Zorg dat Bluetooth aanstaat op uw telefoon
Tik op het startscherm op de Connect knop. De app zoekt zowel naar BLE als Classic Bluetooth apparaten.
Zoek en tik op uw apparaat in de scanresultaten om te verbinden:
Voor Classic Bluetooth: tik op "ESP32_Table"
Voor BLE: tik op "ESP32BLE_Table"
Na verbinding keert de app automatisch terug naar het startscherm. Selecteer de Table app vanuit het app-menu.
De tabel wordt gevuld met de attribuutnamen en hun huidige waarden
Kijk nu terug naar de Serial Monitor in Arduino IDE. U zult zien:
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!
App requested table data
Sent table structure
Temperature: 25.3
Humidity: 62%
Pressure: 1013 hPa
Counter: 1
Uptime: 0h 0m 5s
Free Heap: 245780
Status: Running
Ln 11, Col 1
ESP32 Dev Module on COM15
2
Bekijk hoe de tabelwaarden realtime in de app worden bijgewerkt
Creatieve Aanpassing - Pas de Code aan op uw Project
Definieer Tabelrijen
Voeg attribuutrijen toe aan de tabel (maximaal 20 rijen):
// Voeg rijen toe tijdens setup bluetoothTable.addRow("Temperature"); bluetoothTable.addRow("Humidity"); bluetoothTable.addRow("Pressure"); bluetoothTable.addRow("Status"); bluetoothTable.addRow("Uptime"); // Controleer aantal rijen int rows = bluetoothTable.getRowCount(); Serial.println("Totaal aantal rijen: " + String(rows)); // Verkrijg attribuutnaam bij index String attr = bluetoothTable.getAttribute(0); // Geeft "Temperature" terug
Update Rijwaarden
Update individuele rijwaarden via attribuutnaam of index:
// Update via attribuutnaam bluetoothTable.sendValueUpdate("Temperature", "25.3 °C"); bluetoothTable.sendValueUpdate("Humidity", "62%"); bluetoothTable.sendValueUpdate("Status", "Running"); // Update via rij-index (0-gebaseerd) bluetoothTable.sendValueUpdate(0, "25.3 °C"); // Eerste rij bluetoothTable.sendValueUpdate(1, "62%"); // Tweede rij
Verwerk Gegevensverzoeken van de App
Wanneer de app verbindt of een verversing vraagt:
bluetoothTable.onDataRequest([]() { Serial.println("App requested table data"); bluetoothTable.sendTableStructure(); // Zend rij namen // Zend actuele waarden voor alle rijen bluetoothTable.sendValueUpdate("Temperature", String(temperature, 1) + " °C"); bluetoothTable.sendValueUpdate("Humidity", String(humidity) + "%"); bluetoothTable.sendValueUpdate("Status", "Online"); });
Wis en Herbouw de Tabel
// Wis alle rijen bluetoothTable.clearTable(); // Voeg nieuwe rijen toe bluetoothTable.addRow("Sensor A"); bluetoothTable.addRow("Sensor B"); // Zend geüpdatete structuur naar de app bluetoothTable.sendTableStructure();
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!