When connected to an android device, we received the address of the hc-05 to be 00:14:03:05:0c:38. The address of our esp32 is A8:42:E3:D8:95:E2. The HC-05 is configured with the following AT commands:
-ROLE: 0
-CMODE: 1
-PSWRD: 1234
-UART: 38400,0,0
With the HC-05 in slave mode, and the esp32 in master mode, we uploaded the SoftwareSerial code below to the HC-05 and the BluetoothSerial code below to the esp32. After the slave code is uploaded to the Arduino with the HC-05, the module blinks at a slow rate of once per second. We are able to connect to the HC-05 with a phone or pc and we are also able to connect to the ESP32 with a phone or pc, but the esp32 won't connect to the HC-05 directly with the uploaded code. We are not sure how to proceed.
HC-05 Slave Code:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2,3); // RX, TX pins of HC-05
void setup() {
Serial.begin(38400);
BTSerial.begin(38400);
}
void loop() {
if (BTSerial.available()) {
char data = BTSerial.read();
Serial.print("Received from ESP32: ");
Serial.println(data);
}
if (Serial.available()) {
char data = Serial.read();
BTSerial.print(data);
}
}
ESP32 Master Code:
#include "BluetoothSerial.h"
#define Slave_Name "H-C-2010-06-01"
#define MACadd "00:14:03:05:0C:38"
uint8_t address[6] {0x00, 0x14, 0x03, 0x05, 0x0C, 0x38};
const char *pin = "1234";
#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;
String myName = "ESP32-BT-Master";
void setup() {
bool connected;
Serial.begin(38400);
SerialBT.begin(myName, true);
Serial.print("The device started in master mode, make sure slave BT device is on!\n");
SerialBT.setPin(pin);
Serial.println("Using PIN");
connected = SerialBT.connect(address);
Serial.print("Connecting to slave BT device with MAC ");
Serial.println(MACadd);
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);
}