无法从arduino中的蓝牙模块接收正确的数据
Can't receive correct data from bluetooth module in arduino
我想检查我的 arduino 蓝牙模块是否工作,所以我寻找简单的教程如何连接和编程 arduino 以便使用蓝牙。
我用这个原理图连接我的arduino和BT。
http://cdn.instructables.com/F4V/PPKD/HITAAYZC/F4VPPKDHITAAYZC.LARGE.jpg
我成功地将我的 BT 模块与 phone 和平板电脑(当时是一个)配对,而且我通过我的 BT 模块向该设备发送数据也没有问题(phone/tablet)。
不幸的是,我无法从我的设备接收到任何正确的数据。
我试过两种方法:
SoftwareSerial 连接 - 当我检查 BTSerial.available()>0
(将数据发送到任何提到的设备工作正常)时,它几乎没有给我任何信息。
硬件串行连接 - 在 Serial1/Serial2/Serial3 上没有成功,但至少当我检查 Serial1(例如)传入字节时,我得到了关于 1 个字节传入的信息。
不幸的是,它是一些某种错误的数据,因为如果我通过 SENA BTterm 从我的 phone 发送 char "a",我会得到 0(作为 int)和一些奇怪的字符(如果我将此数据存储在 char 变量中)。
我假设我的 BT 模块工作正常,因为如果它不能与任何其他设备建立连接。
我使用的一些代码:
软件序列号:
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX
// Connect the HC-05 TX to Arduino pin 2 RX.
// Connect the HC-05 RX to Arduino pin 3 TX through a voltage divider.
//
char c = ' ';
void setup()
{
Serial.begin(9600);
Serial.println("Arduino is ready");
// HC-05 default serial speed for commincation mode is 9600
BTserial.begin(9600);
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
//Serial.write("yolo");
if (BTserial.available() > 0)
{
Serial.write("yolo");
c = BTserial.read();
Serial.write(c);
}
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available() > 0)
{
//Serial.write("yolo");
c = Serial.read();
BTserial.println(c);
}
}
硬件序列号版本:
char data;
char data2;
void setup() {
// put your setup code here, to run once:
Serial1.begin(9600);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial1.available() > 0)
{
Serial.print("Incoming bytes:");
Serial.println(Serial1.available());
data = Serial1.read();
Serial.println(data);
}
data2 = Serial.read();
if(data2 != -1)
{
Serial.print( "Sending: ");
Serial.println( data2);
Serial1.println(data2);
}
//delay(200);
}
我的问题的解决方案是简单地将与 BT 模块通信的串口更改为 Serial0。最简单的解决方案是最好的。
我 post 这里是我的解决方案,因为我一直遇到同样的问题,但找不到任何解决方案。所以我希望这会帮助很多人。如果您有 Arduino Mega 开发板并且正在使用软件串行库,请记住并非开发板上的所有引脚都可以用作 RX。
如图书馆子网站所述:
" 并非 Mega 和 Mega 2560 上的所有引脚都支持更改中断,因此只有以下引脚可用于 RX:10、11、12、13、14、15、50、51、52、53、A8(62)、A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69) "
这里有更多信息:https://www.arduino.cc/en/Reference/SoftwareSerial
希望这对我有所帮助
我想检查我的 arduino 蓝牙模块是否工作,所以我寻找简单的教程如何连接和编程 arduino 以便使用蓝牙。
我用这个原理图连接我的arduino和BT。 http://cdn.instructables.com/F4V/PPKD/HITAAYZC/F4VPPKDHITAAYZC.LARGE.jpg
我成功地将我的 BT 模块与 phone 和平板电脑(当时是一个)配对,而且我通过我的 BT 模块向该设备发送数据也没有问题(phone/tablet)。 不幸的是,我无法从我的设备接收到任何正确的数据。
我试过两种方法:
SoftwareSerial 连接 - 当我检查 BTSerial.available()>0
(将数据发送到任何提到的设备工作正常)时,它几乎没有给我任何信息。
硬件串行连接 - 在 Serial1/Serial2/Serial3 上没有成功,但至少当我检查 Serial1(例如)传入字节时,我得到了关于 1 个字节传入的信息。
不幸的是,它是一些某种错误的数据,因为如果我通过 SENA BTterm 从我的 phone 发送 char "a",我会得到 0(作为 int)和一些奇怪的字符(如果我将此数据存储在 char 变量中)。
我假设我的 BT 模块工作正常,因为如果它不能与任何其他设备建立连接。
我使用的一些代码:
软件序列号:
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX
// Connect the HC-05 TX to Arduino pin 2 RX.
// Connect the HC-05 RX to Arduino pin 3 TX through a voltage divider.
//
char c = ' ';
void setup()
{
Serial.begin(9600);
Serial.println("Arduino is ready");
// HC-05 default serial speed for commincation mode is 9600
BTserial.begin(9600);
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
//Serial.write("yolo");
if (BTserial.available() > 0)
{
Serial.write("yolo");
c = BTserial.read();
Serial.write(c);
}
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available() > 0)
{
//Serial.write("yolo");
c = Serial.read();
BTserial.println(c);
}
}
硬件序列号版本:
char data;
char data2;
void setup() {
// put your setup code here, to run once:
Serial1.begin(9600);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial1.available() > 0)
{
Serial.print("Incoming bytes:");
Serial.println(Serial1.available());
data = Serial1.read();
Serial.println(data);
}
data2 = Serial.read();
if(data2 != -1)
{
Serial.print( "Sending: ");
Serial.println( data2);
Serial1.println(data2);
}
//delay(200);
}
我的问题的解决方案是简单地将与 BT 模块通信的串口更改为 Serial0。最简单的解决方案是最好的。
我 post 这里是我的解决方案,因为我一直遇到同样的问题,但找不到任何解决方案。所以我希望这会帮助很多人。如果您有 Arduino Mega 开发板并且正在使用软件串行库,请记住并非开发板上的所有引脚都可以用作 RX。
如图书馆子网站所述: " 并非 Mega 和 Mega 2560 上的所有引脚都支持更改中断,因此只有以下引脚可用于 RX:10、11、12、13、14、15、50、51、52、53、A8(62)、A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69) "
这里有更多信息:https://www.arduino.cc/en/Reference/SoftwareSerial
希望这对我有所帮助