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

时间:2017-09-29 03:47:48

标签: 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 <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();
  }      
}

我通过蓝牙终端Android应用连接蓝牙模块。一切正常(甚至是蓝牙模块上的灯)。但是当我从手机发送一个角色到Arduino时,我得到以下输出:

发送到蓝牙模块的文字 -

enter image description here

请帮助

谢谢

1 个答案:

答案 0 :(得分:0)

我有完全相同的问题。你试过在9600运行HC-05吗?尝试下面的代码(使用您的引脚)。我使用代码在引脚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);
    }
  }

}

如果您想查看布线等,请查看我博客上的条目:

https://engineersportal.com/blog/2017/11/15/bluetooth-home-automation-light-bulb-switch-using-an-hc-05-a-relay-and-arduino