Android 打印整数值,Arduino HC-05蓝牙模块

Android 打印整数值,Arduino HC-05蓝牙模块,android,terminal,bluetooth,arduino,arduino-uno,Android,Terminal,Bluetooth,Arduino,Arduino Uno,我有一个问题,我已经研究了一段时间了。我有一个Arduino Uno板和一个HC-05蓝牙收发器,具有TTL输出。 连接如下: RX (HC_05) --> TX (Arduino UNO) TX (HC_05) --> RX (Arduino UNO) GND (HC-05) --> GND (Arduino UNO) +5V (HC-05) --> +5V (Arduino UNO) 我有以下Arduino代码: #include <Software

我有一个问题,我已经研究了一段时间了。我有一个Arduino Uno板和一个HC-05蓝牙收发器,具有TTL输出。

连接如下:

RX (HC_05)  --> TX (Arduino UNO)

TX (HC_05)  --> RX (Arduino UNO)

GND (HC-05) --> GND (Arduino UNO)

+5V (HC-05) --> +5V (Arduino UNO)
我有以下Arduino代码:

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX

void setup()
{
  Serial.begin(9600);
  BTSerial.begin(38400);  // HC-05 default speed in AT command more
  pinMode(9, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
  pinMode(10, INPUT);
  pinMode(11, OUTPUT);
  
  digitalWrite(9, HIGH);
  Serial.println("Enter AT commands:");
  BTSerial.println("Welcome to ARBA-Beat");
}


void loop()
{

  // Keep reading from HC-05 and send to Arduino Serial Monitor
  if (BTSerial.available()) {
    Serial.println(BTSerial.read());
    BTSerial.write(BTSerial.read());
    BTSerial.flush();
  }      
}
#包括
软件串行BTSerial(10,11);//RX | TX
无效设置()
{
Serial.begin(9600);
BTSerial.begin(38400);//HC-05 AT命令中的默认速度更多
引脚模式(9,输出);//该引脚将HC-05引脚34(键引脚)拉高,以将模块切换到AT模式
pinMode(10,输入);
pinMode(11,输出);
数字写入(9,高);
Serial.println(“Enter AT commands:”);
println(“欢迎来到ARBA Beat”);
}
void循环()
{
//保持HC-05的读数并发送至Arduino串行监视器
if(BTSerial.available()){
Serial.println(BTSerial.read());
BTSerial.write(BTSerial.read());
BTSerial.flush();
}      
}
我通过蓝牙终端Android应用程序连接到蓝牙模块。一切正常(甚至蓝牙模块上的灯)。但当我从手机向Arduino发送一个字符时,我会得到以下输出:

发送到蓝牙模块的文本-a

请帮忙


谢谢

我也有同样的问题。你试过在9600运行HC-05吗?尝试下面的代码(使用PIN)。我使用该代码切换引脚2上的继电器,但如果需要,可以使用LED。我使用了相同的蓝牙应用程序,效果很好:

#include <SoftwareSerial.h>
int relay = 2; // Set pin for relay control

SoftwareSerial bleserial(8,9);

// setup the relay output and the bluetooth serial, and the serial monitor (if you want to print the outputs)
void setup() {                
  // set relay pin as output.
  pinMode(relay, OUTPUT); 
  // start bluetooth and serial monitor  
  bleserial.begin(9600);
  Serial.begin(9600);

}

void loop() {

  if(bleserial.available()){

    char char1 = bleserial.read();
    Serial.println(char1);
    // Set protocol that you want to turn on the light bulb, I chose 1 and 0 as on and off, respectively

    if(char1=='1'){
      Serial.println("ON");
      digitalWrite(relay,LOW);
    } else if (char1=='0'){
      digitalWrite(relay,HIGH);
    }
  }

}
#包括
int继电器=2;//继电器控制的设置引脚
软件序列bleserial(8,9);
//设置继电器输出、蓝牙串行和串行监视器(如果要打印输出)
无效设置(){
//将继电器引脚设置为输出。
pinMode(继电器、输出);
//启动蓝牙和串行监视器
bleserial.begin(9600);
Serial.begin(9600);
}
void循环(){
if(bleserial.available()){
char char1=bleserial.read();
序列号println(char1);
//如果要打开灯泡,我分别选择1和0作为打开和关闭
如果(字符1='1'){
序列号。打印号(“ON”);
数字写入(继电器,低电平);
}else if(char1==“0”){
数字写入(继电器,高);
}
}
}
如果您想查看布线等,请查看我博客上的条目:


我也有同样的问题。你试过在9600运行HC-05吗?尝试下面的代码(使用PIN)。我使用该代码切换引脚2上的继电器,但如果需要,可以使用LED。我使用了相同的蓝牙应用程序,效果很好:

#include <SoftwareSerial.h>
int relay = 2; // Set pin for relay control

SoftwareSerial bleserial(8,9);

// setup the relay output and the bluetooth serial, and the serial monitor (if you want to print the outputs)
void setup() {                
  // set relay pin as output.
  pinMode(relay, OUTPUT); 
  // start bluetooth and serial monitor  
  bleserial.begin(9600);
  Serial.begin(9600);

}

void loop() {

  if(bleserial.available()){

    char char1 = bleserial.read();
    Serial.println(char1);
    // Set protocol that you want to turn on the light bulb, I chose 1 and 0 as on and off, respectively

    if(char1=='1'){
      Serial.println("ON");
      digitalWrite(relay,LOW);
    } else if (char1=='0'){
      digitalWrite(relay,HIGH);
    }
  }

}
#包括
int继电器=2;//继电器控制的设置引脚
软件序列bleserial(8,9);
//设置继电器输出、蓝牙串行和串行监视器(如果要打印输出)
无效设置(){
//将继电器引脚设置为输出。
pinMode(继电器、输出);
//启动蓝牙和串行监视器
bleserial.begin(9600);
Serial.begin(9600);
}
void循环(){
if(bleserial.available()){
char char1=bleserial.read();
序列号println(char1);
//如果要打开灯泡,我分别选择1和0作为打开和关闭
如果(字符1='1'){
序列号。打印号(“ON”);
数字写入(继电器,低电平);
}else if(char1==“0”){
数字写入(继电器,高);
}
}
}
如果您想查看布线等,请查看我博客上的条目:


您在不同的函数中调用了两次
BTSerial.read()
,这可能意味着从队列中取出了两个值。在回显到终端/使用该值执行某些操作之前,请尝试将其存储在变量中。@MorrisonChang我尝试了您所说的一切,现在我得到的是盒形值。我建议将该值转换为十六进制/二进制,并将其发送到您正在使用的任何调试/日志会话,以查看您是否真的得到了所发送的值,或者正在进行其他操作(额外字符/大小端问题/crlf问题)。如果您正在遵循教程-您可能希望链接到它并指出哪个步骤失败。您在不同的函数中调用了两次
BTSerial.read()
,这可能意味着两个值被从队列中取出。在回显到终端/使用该值执行某些操作之前,请尝试将其存储在变量中。@MorrisonChang我尝试了您所说的一切,现在我得到的是盒形值。我建议将该值转换为十六进制/二进制,并将其发送到您正在使用的任何调试/日志会话,以查看您是否真的得到了所发送的值,或者正在进行其他操作(额外字符/大小端问题/crlf问题)。如果您正在遵循教程,您可能希望链接到它并指出哪一步失败。