首页 > 解决方案 > 将 ESP32 与 HC-05 BT 模块配对

问题描述

我想将 ESP32 微控制器设置为主控,并让他连接到 HC-05 bt 模块。我正在使用 Arduino IDE 的示例SerialToSerialBTM.ino,但是如果将蓝牙模块的 MAC 地址放在串行监视器中打印“连接成功”,然后打印此错误,ESP32 会自行重置并重新启动。

Stack smashing protect failure!

abort() was called at PC 0x400d5ee8 on core 0

Backtrace: 0x4009194c:0x3ffcfb30 0x40091b7d:0x3ffcfb50 0x400d5ee8:0x3ffcfb70 0x400fe443:0x3ffcfb90 0x400f58d2:0x3ffcfbe0 0x4008e0bd:0x3ffcfc10

如果我放一个随意的 MAC 地址,它总是打印“连接成功”,但它不会重新启动。我在 Arduino IDE 中使用的代码是这样的,谁能告诉我有关此错误的更多信息以及如何使我的代码正常工作?

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

String MACadd = "98:D3:32:11:31:37";
uint8_t address[6]  = {0x98, 0xD3, 0x32, 0x11, 0x31, 0x37};
//uint8_t address[6]  = {0x11, 0x1D, 0xA5, 0x02, 0xC3, 0x22};
String name = "HC-05";
char *pin = "1234"; //<- standard pin would be provided by default
bool connected;

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

  SerialBT.begin("ESP32test", true); 
  SerialBT.setPin(pin);
  Serial.println("The device started in master mode, make sure remote BT device is on!"); 

  connected = SerialBT.connect(address);

  if(connected) {
    Serial.println("Connected Succesfully!");
  } 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 upto 10 secs max
  if (SerialBT.disconnect()) {
    Serial.println("Disconnected Succesfully!");
  }
  // this would reconnect to the name(will use address, if resolved) or address used with connect(name/address).
  SerialBT.connect();
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}

标签: arduinobluetoothmicrocontrolleresp32hc-05

解决方案


代替

connected = SerialBT.connect(address)

像这样尝试

connected = SerialBT.connect(name)

推荐阅读