0

I have made a home automation project where am using an Arduino Uno and HC-05 Bluetooth module and an Android app. I did everything properly but when I send commands from my Android app it works sometimes and sometimes it doesn't work.

I checked these commands on the serial monitor and found that data command I recieve at serial monitor did not match the commands which I send from my Android app. For example my Android app send command "ON1" but on serial monitor it shows "OL1?". I thought it might be a problem with the HC-05 so I changed it and used a new HC-05 module, but the problem is still there.

This time I checked the commands from another Android app (Bluetooth Terminal HC-05) and there was the same problem. Sometimes it read the command correctly app and sometimes it read something else.

I am so confused weather the problem is in my hardware side or software side.I tried a lot but didn't get any solution about my problem anywhere. Can anybody help me please?

This is the Arduino code of my project:

#include <SoftwareSerial.h>
SoftwareSerial Genotronex(2, 3); //TX, RX  pins of arduino respetively
String command;
void setup() {
  Genotronex.begin(9600);
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop() {
  while (Genotronex.available()) {
  //Check if there is an available byte
    to read
    delay(10); //Delay added to make thing stable
    char c = Genotronex.read(); //Conduct a serial read
    command += c; //build the string.
  }
  if (command.length() > 0) {
    Serial.print(command);
    if (command == "ON1") {}
    //this command will be given as an input to switch on light1
    digitalWrite(9, HIGH);
  } else if (command == "OFF1") {
    //this command will be given as an input to switch off light1 simillarly other commands work
    digitalWrite(9, LOW);
  } else if (command == "ON2") {
    digitalWrite (10, HIGH);
  } else if ( command == "OFF2") {
    digitalWrite (10, LOW);
  } else if (command == "ON3") {
    digitalWrite (11, HIGH);
  } else if (command == "OFF3") {
    digitalWrite (11, LOW);
  } else if (command == "ON4") {
    digitalWrite (12, HIGH);
  } else if (command == "OFF4") {
    digitalWrite (12, LOW);
  } else if (command == "ON5") {
    digitalWrite (13, HIGH);
  } else if (command == "OFF5") {
    digitalWrite (13, LOW);
  } else if (command == "all on") {
    //using this command you can switch on all devices
    digitalWrite (9, HIGH);
    digitalWrite (10, HIGH);
    digitalWrite (11, HIGH);
    digitalWrite (12, HIGH);
    digitalWrite (13, HIGH);
  } else if (command == "off") {
    //using this command you can switch off all devices
    digitalWrite (9, LOW);
    digitalWrite (10, LOW);
    digitalWrite (11, LOW);
    digitalWrite (12, LOW);
    digitalWrite (13, LOW);
  }
  command = "";
} //Reset the variable
  • Could you re-read your question please, and check it says what you want? Edit any fixes into the question, rather than posting them here. I am confused about how the system is set up -- for example, you say the Android app sending the data didn't receive the right commands. Do you mean the Arduino didn't receive the right commands? What commands did it receive? Your method of reading until there is nothing left is dangerous, but let's find out what the problem really is before guessing solutions. – Mark Smith Dec 3 '17 at 9:01
  • I have edit it as u mention. Thanks for ur guidness. plz let me know is that ok now ? – bc120400339 Imran Ali Dec 3 '17 at 13:38
0

I suggest to have a look at this answer: Change the baud rate of HC-05

In many case and especially when you send data from Android and receive bad data on Arduino, it's because the speed is not set correctly. On the other side (getting data from the Arduino), it's a bit more complicated. You have to build your own "time out" system on Android as the Bluetooth library don't have such a feature.

-2

Use 38400 baud rate for Bluetooth. This will resolve your problem.

  • 1
    How? What's the reasoning behind this? (I'm not saying you're wrong, but back up your answer with detail). How would the OP go about using that baud rate? In other words, as your answer stands it's not of much use. It may be factually correct, but as an answer it lacks detail. – Majenko Feb 23 '18 at 11:12
  • Depending on the module/version, the default baud rate of the HC-05 can be 9600 or 38400. If you wish to use another rate, you need to set it with an AT command, for example to 9600, you'd use: "AT+BAUD4", if you have a module that defaults to 38400. Edit: more here, for example: instructables.com/id/Changing-Baud-Rate-of-HC-05-Bluetooth – DocWeird Apr 24 '18 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.