ESP32 connection with another device via Bluetooth
23:04 28 Oct 2024

I want to connect the ESP32 CP2102 with another device that sends some data to ESP32 via Bluetooth. This device is Digispark ATtiny 85 connected with HC05 bluetooth module by wires.

The code on Digispark is:

#include 

#define RX_PIN 1  // Connect to HC-05 TX
#define TX_PIN 2  // Connect to HC-05 RX

SoftwareSerial bluetooth(RX_PIN, TX_PIN);

void setup() {
  bluetooth.begin(9600);
  delay(1000);
}

void loop() {
  bluetooth.println("BT from left");
  delay(2000);
}

And when i connect to Serial Bluetooth Terminal on my phone i got these messegaes every 2s. I entered in AT mode i the HC05 is in Role=0, PSWD=9876. The next code is on ESP32;

#include "BluetoothSerial.h"

BluetoothSerial ESP_BT;

const char* hc05_address = "98:DA:50:03:C1:3C";
const char* pin = "9876"; 

bool connected = false;

void setup() {
  Serial.begin(115200);
  ESP_BT.begin("ESP32_Client");

  //ESP_BT.setPin(pin);

  Serial.println("Attempting to connect to HC-05...");

  connected = ESP_BT.connect(hc05_address);

  if (connected) {
    Serial.println("Connected to HC-05 successfully!");
  } else {
    Serial.println("Failed to connect to HC-05. Make sure it is in pairing mode.");
  }
}

void loop() {
  if (connected && ESP_BT.available()) {
    String incomingData = ESP_BT.readString();
    Serial.print("Received from HC-05: ");
    Serial.println(incomingData);
  }
}

But the problem is that setPin is not recognizable in library. But still without I cannot connect both devices together.

Do you know maybe why?

Later I will need to send and recive data on both devices.

Edit: I have edited a code for ESP32:

#include "BluetoothSerial.h"
#include "esp_bt_device.h"
#include "esp_bt_main.h"
#include "esp_gap_bt_api.h"

BluetoothSerial ESP_BT;

const char* hc05_address_str = "98:da:50:03:c1:3c";
const char* pin = "1234";  

bool connected = false;

void macStrToBytes(const char* macStr, uint8_t* macBytes) {
    int values[6];
    sscanf(macStr, "%x:%x:%x:%x:%x:%x", &values[0], &values[1], &values[2], &values[3], &values[4], &values[5]);
    for (int i = 0; i < 6; ++i) {
        macBytes[i] = (uint8_t)values[i];
    }
}

void gapCallback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) {
    if (event == ESP_BT_GAP_PIN_REQ_EVT) {
        Serial.println("PIN requested. Setting default PIN...");
        esp_bt_gap_set_pin(ESP_BT_PIN_TYPE_FIXED, 4, (uint8_t*)pin);
    }
}

void setup() {
  Serial.begin(115200);

  esp_bt_gap_register_callback(gapCallback);
  esp_bt_gap_set_pin(ESP_BT_PIN_TYPE_FIXED, 4, (uint8_t*)pin); 

  ESP_BT.begin("ESP32_Client", true);

  Serial.println("Attempting to connect to HC-05...");
  delay(2000);  

  uint8_t hc05_address[6];
  macStrToBytes(hc05_address_str, hc05_address);

  connected = ESP_BT.connect(hc05_address);

  if (connected) {
    Serial.println("Connected to HC-05 successfully!");
  } else {
    Serial.println("Failed to connect to HC-05. Make sure it is in pairing mode.");
  }
}

void loop() {
  if (connected && ESP_BT.available()) {
    String incomingData = ESP_BT.readString();
    Serial.print("Received from HC-05: ");
    Serial.println(incomingData);
  }
}

End set the password for HC05 for 1234.

But after it my console shows:

11:16:19.677 -> [  1018][I][BluetoothSerial.cpp:707] _init_bt(): device name set
11:16:19.677 -> [  1018][I][BluetoothSerial.cpp:248] esp_spp_cb(): ESP_SPP_INIT_EVT
11:16:19.677 -> Attempting to connect to HC-05...
11:16:19.677 -> [  1030][I][BluetoothSerial.cpp:254] esp_spp_cb(): ESP_SPP_INIT_EVT: master: start
11:16:19.721 -> [  1043][I][BluetoothSerial.cpp:575] esp_bt_gap_cb(): ESP_BT_GAP_CONFIG_EIR_DATA_EVT: stat:0 num:4
11:16:19.721 -> [  1052][I][BluetoothSerial.cpp:329] esp_spp_cb(): ESP_SPP_START_EVT
11:16:21.719 -> [  3029][I][BluetoothSerial.cpp:1034] connect(): master : remoteAddress
11:16:24.372 -> [  5723][I][BluetoothSerial.cpp:605] esp_bt_gap_cb(): ESP_BT_GAP_ACL_CONN_CMPL_STAT_EVT ACL connection complete status event
11:16:24.451 -> [  5800][I][BluetoothSerial.cpp:264] esp_spp_cb(): ESP_SPP_DISCOVERY_COMP_EVT num=1
11:16:24.451 -> [  5807][D][BluetoothSerial.cpp:267] esp_spp_cb(): ESP_SPP_DISCOVERY_COMP_EVT: spp [0] channel: 1 service name:Port
11:16:24.482 -> [  5817][I][BluetoothSerial.cpp:273] esp_spp_cb(): ESP_SPP_DISCOVERY_COMP_EVT: spp connect to remote 98:da:50:03:c1:3c channel 1
11:16:24.482 -> [  5831][I][BluetoothSerial.cpp:334] esp_spp_cb(): ESP_SPP_CL_INIT_EVT handle:130 sec_id:56
11:16:24.557 -> [  5910][I][BluetoothSerial.cpp:532] esp_bt_gap_cb(): ESP_BT_GAP_PIN_REQ_EVT (min_16_digit=0)
11:16:24.589 -> [  5918][I][BluetoothSerial.cpp:537] esp_bt_gap_cb(): Input pin code: ""=0x0
11:16:24.589 -> [  5930][E][BluetoothSerial.cpp:525] esp_bt_gap_cb(): authentication failed, status:9
11:16:24.589 -> [  5937][I][BluetoothSerial.cpp:310] esp_spp_cb(): ESP_SPP_CLOSE_EVT status:0 handle:130 close_by_remote:1 attempt 0
11:16:24.622 -> [  5948][D][BluetoothSerial.cpp:790] waitForConnect(): connection closed!
11:16:24.622 -> Failed to connect to HC-05. Make sure it is in pairing mode.

Please help with this situation.

bluetooth microcontroller esp32 hc-05