void setup(void) {
Serial.begin(115200); // starting serial terminal
Serial.println("-----------------");
uint8_t macBT[6]; // array variable holding the mac address to store the
esp_read_mac(macBT, ESP_MAC_BT);
}
void loop() {
uint8_t macBT[6];
esp_read_mac(macBT, ESP_MAC_BT);
Serial.printf("%02X:%02X:%02X:%02X:%02X:%02X\r\n", macBT[0], macBT[1], macBT[2], macBT[3], macBT[4], macBT[5]);
delay(10000);
}
i have selected the the shown address and copied them aside and use it as i talked about it above
#include "BluetoothSerial.h"
#define USE_NAME // Comment this to use MAC address instead of a slaveName
const char *pin = "1234"; // Change this to reflect the pin expected by the real slave BT device
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif
BluetoothSerial SerialBT;
#ifdef USE_NAME
String slaveName = "myslave"; // Change this to reflect the real name of your slave BT device
#else
String MACadd = "7C:9E:BD:49:4E:2A"; // This only for printing 7C:9E:BD:49:4E:2A
uint8_t address[6] = {0x7C, 0x9E, 0xBD, 0x49, 0x4E, 0x2A}; // Change this to reflect real MAC address of your slave BT device
#endif
String myName = "MasterBT";
void setup() {
bool connected;
Serial.begin(115200);
SerialBT.begin(myName, true);
Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
#ifndef USE_NAME
SerialBT.setPin(pin);
Serial.println("Using PIN");
#endif
// connect(address) is fast (up to 10 secs max), connect(slaveName) is slow (up to 30 secs max) as it needs
// to resolve slaveName to address first, but it allows to connect to different devices with the same name.
// Set CoreDebugLevel to Info to view devices Bluetooth address and device names
#ifdef USE_NAME
connected = SerialBT.connect(slaveName);
Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
#else
connected = SerialBT.connect(address);
Serial.print("Connecting to slave BT device with MAC "); Serial.println(MACadd);
#endif
if(connected) {
Serial.println("Connected Successfully!");
} else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
}
}
// Disconnect() may take up to 10 secs max
if (SerialBT.disconnect()) {
Serial.println("Disconnected Successfully!");
}
// This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
SerialBT.connect();
if(connected) {
Serial.println("Reconnected Successfully!");
} else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
}
}
}
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}
#include "BluetoothSerial.h"
//#define USE_PIN // Uncomment this to use PIN during pairing. The pin is specified on the line below
const char *pin = "1234"; // Change this to more secure PIN.
String device_name = "myslave";
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin(device_name); //Bluetooth device name
Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str());
//Serial.printf("The device with name \"%s\" and MAC address %s is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str(), SerialBT.getMacString()); // Use this after the MAC method is implemented
#ifdef USE_PIN
SerialBT.setPin(pin);
Serial.println("Using PIN");
#endif
}
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}
for this part of master this is initialisation and declaration#include "BluetoothSerial.h" #define USE_NAME // Comment this to use MAC address instead of a slaveName const char *pin = "1234"; // Change this to reflect the pin expected by the real slave BT device #if !defined(CONFIG_BT_SPP_ENABLED) #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip. #endif BluetoothSerial SerialBT; #ifdef USE_NAME String slaveName = "myslave"; // Change this to reflect the real name of your slave BT device #else String MACadd = "7C:9E:BD:49:4E:2A"; // This only for printing 7C:9E:BD:49:4E:2A uint8_t address[6] = {0x7C, 0x9E, 0xBD, 0x49, 0x4E, 0x2A}; // Change this to reflect real MAC address of your slave BT device #endif String myName = "MasterBT";for the first line it is included with a library of bluetooth which is able to open bluetooth terminal for both configurations
String MACadd = "7C:9E:BD:49:4E:2A";in void setup we start the bluetooth terminal so it start operating, and also start the serial terminal
void setup() { bool connected; Serial.begin(115200); SerialBT.begin(myName, true); Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str()); #ifndef USE_NAME SerialBT.setPin(pin); Serial.println("Using PIN"); #endif // connect(address) is fast (up to 10 secs max), connect(slaveName) is slow (up to 30 secs max) as it needs // to resolve slaveName to address first, but it allows to connect to different devices with the same name. // Set CoreDebugLevel to Info to view devices Bluetooth address and device names #ifdef USE_NAME connected = SerialBT.connect(slaveName); Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str()); #else connected = SerialBT.connect(address); Serial.print("Connecting to slave BT device with MAC "); Serial.println(MACadd); #endif if(connected) { Serial.println("Connected Successfully!"); } else { while(!SerialBT.connected(10000)) { Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app."); } } // Disconnect() may take up to 10 secs max if (SerialBT.disconnect()) { Serial.println("Disconnected Successfully!"); } // This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address). SerialBT.connect(); if(connected) { Serial.println("Reconnected Successfully!"); } else { while(!SerialBT.connected(10000)) { Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app."); } } }in void loop we set and communicate between two boards (esp32)