// ==== Slave ====
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_SLAVE"); // Bluetooth device name
Serial.println("The device started, now you can pair it with Bluetooth!");
Serial.println(BluetoothDeviceAddress().toString().c_str()); // Print Slave mac address
}
void loop() {
if (SerialBT.available()) {
char incomingChar = SerialBT.read();
Serial.print("Received: ");
Serial.println(incomingChar);
// Optionally, echo back to master
SerialBT.write(incomingChar);
}
}
// ====================================
// ==== Master ====
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
// MAC address of the slave ESP32 (you need to set it from the slave serial output or BT scan)
const char *slaveAddress = "AA:BB:CC:11:22:33"; // Replace with your slave MAC address!
if (!SerialBT.begin("ESP32_MASTER", true)) { // true = Master mode
Serial.println("An error occurred initializing Bluetooth");
return;
}
Serial.println("Bluetooth started in master mode. Connecting to slave...");
if (SerialBT.connect(slaveAddress)) {
Serial.println("Connected to slave successfully!");
} else {
Serial.println("Failed to connect. Make sure slave is on and in range.");
while (1) delay(1000); // Stop here
}
}
void loop() {
// Send data every second
SerialBT.println("Hello from Master!");
Serial.println("Sent: Hello from Master!");
delay(1000);
// Optional: read data sent back from slave
while (SerialBT.available()) {
char c = SerialBT.read();
Serial.print("Slave echoed: ");
Serial.println(c);
}
}
// ====================================
// ==== Print Slave mac address ====
#include "BluetoothSerial.h"
void setup() {
Serial.begin(115200);
delay(1000); // Wait for Serial to initialize
// Start Bluetooth (name doesn't matter here)
BluetoothSerial SerialBT;
SerialBT.begin("GetMAC");
// Get and print the MAC address
const uint8_t* mac = ESP_BT_DEV_MAC_ADDR;
Serial.print("Bluetooth MAC address: ");
for (int i = 0; i < 6; i++) {
if (mac[i] < 16) Serial.print("0");
Serial.print(mac[i], HEX);
if (i < 5) Serial.print(":");
}
Serial.println();
}
void loop() {
// Nothing to do here
}