simple bluetooth serial write help

I’ve got a HC05 blutooth working with a simple Android terminal APP.

The APP can send data to the android OK and shows on the serial monitor but I can’t get data back to the Android.

I am using the simplest possible code so I cant see why it wont receive unless my APP software has an issue?

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
}
char state;

void loop() {
  if(Serial1.available() > 0){
    state= Serial1.read(); 
    Serial.print (state);
    Serial1.write ("Hello"); 
  }
}

I've got more working now and have modified the code.

Basically in the APP I can assign ascii or hex to a button and send that info (in my case as a CHAR) and when the IF runs (it is seeing data) it responds with "hello".

I am a bit confused though as it seems to send several hello's, I assume because the ascii send one character at a time so the loop goes round.

ALso on the serial monitor I can see the "off" and "on" being received but the LED doesn't light. I have tested the IO pin and the wring so it isn't that. Is it a timing thing again in the loop?

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(3, OUTPUT);
}
char state;

void loop() {
  if(Serial1.available() > 0){
    state= Serial1.read(); 
    Serial.print (state);
    Serial1.write ("Hello"); 
    if(state == "off")
     {
       digitalWrite(3, LOW); 
     }
    if(state == "on")
     {
      digitalWrite(3, HIGH); 
     }
    }
 }

What data type is state? What will that data type hold? How many bytes does one call to Serial.read() return?


A good serial I/O tutorial.

state is a Char data type and in the APP I have limited the ASCII characters to ON and OFF.

I cant work out why ON and OFF aren't being recognised by the IF and therefore why the LED isn't being controlled.

The serial monitor says I am receiving the characters.

I'm obviously a bit new to this and I think some of my problem is that the APP outputs in hex or ascii.

The serial monitor is seeing ON or OFF when the APP buttons are pressed but are they really being recognised as ON and OFF by the char compares to trigger the IF or is the ascii transfer corrupting it somehow?

Serial1.begin(9600);i don't thinkt hat is the correct BAUD rate for the HC05, i think it should be 38400.

I've got another app that just sends a 1 or 0 and the HCO5 recognises that at 9600 so I don't think its the baud rate.

The issue with the APP that sends/receives hex or ASCII is that the ON and OFF that are sent and received in the serial monitor correctly aren't being recognised by the IF statement for some reason.

Guess I don't know enough about HEX or ASCII?

OK, I think I've got it now. Thanks for the pointers.

In my program I have to use the binary in the IF for it to be recognised when the APP sends HEX.

So if the APP button is assigned as 01 for ON then the IF must use 01 etc. What was confusing me was the serial monitor when I asked it to print the state in HEX it was showing A10 for what was a 00 button assignment in the APP and a 01 was reported as 10A10. I still don't understand this?

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(3, OUTPUT);
}
int state;

void loop() 
{
  if(Serial1.available() > 0)
  {
    state= Serial1.read(); 
    Serial1.write ("Hello"); 
    Serial.print (DEC,state);
  }
    if(state == 0)
     {
       digitalWrite(3, LOW); 
     }
    if(state == 1)
     {
      digitalWrite(3, HIGH); 
     }
    if(state == 10)
     {
      digitalWrite(3, HIGH); 
      delay (1000);
      digitalWrite(3, LOW); 
      delay (1000);
     }
 }