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