Arduino Nano 和 HM-10 BLE 控制器之间的通信不工作
Communication between Arduino Nano and HM-10 BLE controller is not working
我想检查 Arduino 中的 SerialMonitor IDE 和 BLE 控制器之间的通信是否正常。
我向我的 SerialMonitor 输入命令 AT
,它应该 return 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 phone 的蓝牙设备中看到 HM-10 控制器,但我什么也看不到。
更新 3:
我使用了 this Whosebug post 的代码并且运行良好。我终于可以在 Android phone 上的蓝牙设备中看到控制器了 return 在 AT+NAME?
命令后输入了名称 MLT-BT05
。看起来你必须读取每个字符的消息并在字符之间放置 10ms 的延迟,否则将无法从 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; }
}
}
我想检查 Arduino 中的 SerialMonitor IDE 和 BLE 控制器之间的通信是否正常。
我向我的 SerialMonitor 输入命令 AT
,它应该 return 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 phone 的蓝牙设备中看到 HM-10 控制器,但我什么也看不到。
更新 3:
我使用了 this Whosebug post 的代码并且运行良好。我终于可以在 Android phone 上的蓝牙设备中看到控制器了 return 在 AT+NAME?
命令后输入了名称 MLT-BT05
。看起来你必须读取每个字符的消息并在字符之间放置 10ms 的延迟,否则将无法从 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; }
}
}