0

我想检查我在 Arduino IDE 中的 SerialMonitor 和 BLE 控制器之间的通信是否正常。

AT我向我的 SerialMonitor输入了命令,它应该返回OK响应,但什么也没发生。

这是我使用的方案:

在此处输入图像描述

代码:

 #include <SoftwareSerial.h>
SoftwareSerial bleSerial(2, 3); // RX, TX
void setup() {
  //initialize serial port for logs
  Serial.begin(9600);
  while (!Serial) {
  }
  bleSerial.begin(9600);
}
void loop() {
  if (bleSerial.available()) {
     Serial.write(bleSerial.read());
  }

  if (Serial.available()) {
    bleSerial.write(Serial.read());
  }
}

更新:

更改了SoftwareSerial bleSerial(3, 2) 的值;// RX,TX仍然不起作用。

更新2:

我试过切换引脚和代码,没有任何效果。我至少应该在我的 Android 手机上的蓝牙设备中看到 HM-10 控制器,但我什么也看不到。

更新3:

我使用了这个 Stackoverflow 帖子中的代码,并且工作正常。我终于可以在我的 Android 手机上的蓝牙设备中看到控制器了。它在命令MLT-BT05后返回了名称。AT+NAME?看起来您必须读取每个字符的消息并在字符之间放置 10 毫秒的延迟,否则将无法从 BLE 控制器读取消息。那是唯一的问题。

4

1 回答 1

0

您应该连接 RX-TX 和 TX-RX(不是 RX-RX 和 TX-TX,如您的图形所示),因此更改电缆和代码

 SoftwareSerial bleSerial(2, 3); // RX, TX

 SoftwareSerial bleSerial(3, 2); // RX, TX

根据此图连接(包括分压器) 在此处输入图像描述

Abd 使用以下草图进行测试(详细信息请阅读评论):

//  SerialIn_SerialOut_HM-10_01
//
//  Uses hardware serial to talk to the host computer and AltSoftSerial for communication with the bluetooth module
//
//  What ever is entered in the serial monitor is sent to the connected device
//  Anything received from the connected device is copied to the serial monitor
//  Does not send line endings to the HM-10
//
//  Pins
//  BT VCC to Arduino 5V out. 
//  BT GND to GND
//  Arduino D8 (SS RX) - BT TX no need voltage divider 
//  Arduino D9 (SS TX) - BT RX through a voltage divider (5v to 3.3v)
//

#include <SoftwareSerial.h>
SoftwareSerial BTserial;      

char c=' ';
bool NL = true;

void setup() 
{
    Serial.begin(9600);
    Serial.print("Sketch:   ");   Serial.println(__FILE__);
    Serial.print("Uploaded: ");   Serial.println(__DATE__);
    Serial.println(" ");

    BTserial.begin(9600);  
    Serial.println("BTserial started at 9600");
}

void loop()
{
    // Read from the Bluetooth module and send to the Arduino Serial Monitor
    if (BTserial.available())
    {
        c = BTserial.read();
        Serial.write(c);
    }

    // Read from the Serial Monitor and send to the Bluetooth module
    if (Serial.available())
    {
        c = Serial.read();

        if (c!=10 & c!=13 ) 
        {  
             BTserial.write(c);
        }

        // Echo the user input to the main window. The ">" character indicates the user entered text.
        if (NL) { Serial.print("\r\n>");  NL = false; }
        Serial.write(c);
        if (c==10) { NL = true; }
    }
}
于 2020-04-19T10:01:41.603 回答