arduino - Arduino Nano 和 HM-10 BLE 控制器之间的通信不起作用
问题描述
我想检查我在 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 控制器读取消息。那是唯一的问题。
解决方案
您应该连接 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; }
}
}
推荐阅读
- python-3.x - PyQt:在当前命令的撤消期间从 UndoStack 弹出先前的命令
- mongodb - Parse Server 应用程序的 MongoDB 索引
- asp.net-mvc - 无法让 MVC 在 asp.net Web 表单项目中工作
- c# - DateTimeStyles 和 WhiteSpaces 字符
- python - 与 gitlab-ci 中的父范围 docker 容器共享 docker 容器中的目录?
- c# - Winforms C#中的TextBoxGrid/表格视图
- sql - SQL - 如何匹配值?
- .net - 使用 netstandard2.0 读取应用程序配置详细信息的推荐方法是什么
- matlab - Matlab 轴在负极限值上抛出错误
- angular6 - RxJs 间隔与 takeUntil 发布最后一个值