HC-06 erratic comunication with Arduino Mega 2560.

Hi, after couple of days reading instructions and visiting forums I am still unable to properly get information from PC/Android to Arduino Mega using a HC-06 Bluetooth module.
Wiring is pretty much what one finds all over the internet.
BT Rx connected at the junction of two resistors, a 2k2 going to ground and a 1k5 going to Arduino Mega pin14 (TX3) .
BT Tx connected straight to Arduino Mega pin15 (RX3).
A LED is connected from Arduino pin 10 to ground via a 1K resistor.

The code is as follows:

String readString;
char c;
int LedPin =10;
int flag=0;

void setup() {
Serial.begin(9600);
Serial.println("OK then, you first, say something.....");
Serial.println("Go on, type something and hit Send,");
Serial.println("or just hit the Enter key,");
Serial.println("then I will repeat it!");
Serial.println("");
pinMode(LedPin, OUTPUT);
Serial3.begin(9600);
}

void writeString(String stringData) { // Used to serially push out a String with Serial.write()
for (int i = 0; i < stringData.length(); i++) {
Serial3.write(stringData*); // Push each char 1 by 1 on each loop pass*

  • // Nothing is printed at the android terminal software*
  • }*
    }
    void loop() {
    while (Serial3.available()) {
  • delay(3);*
  • c = Serial3.read();*
  • readString += c;*
    }

if (readString.length() >0) {

  • if (flag==0){ *

  • digitalWrite(LedPin, HIGH); //works fine*

  • flag = 1;*

  • } else {*

  • digitalWrite(LedPin, LOW); //works fine*

  • flag = 0;*

  • }*

  • writeString(readString);*

  • Serial.println(readString); //nothing (sometimes garbage) on monitor*

  • Serial3.write("Response OK"); // nothing shows up at the android device*

  • readString="";*
    }
    }
    1- Sending something from Android, anything, will turn led on and off
    2- If I try to detect what was sent from Android, it does not work, nothing is printable, I just cannot see what is in readString.
    3- Except from the opening text at the setup() nothing but a few garbage is sent to the monitor.
    4- nothing arrives at the Android device.
    5- Tried external power supply with no results.
    6- Pairing works with no problems.
    Any assistance will be welcome.
    Thanks
    Paulo

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

Dear Robin2, thanks for the Serial Input Basics, I had a quick read and it seems it adresseses many of my problems.

How about serial data from the Arduino to the Android device, I have no control on that end, how to format the data? Should I use Serial.write or Serial.print?

Regards
Paulo

Dear Robin,

I just tested your Example 1 as below:

// Example 1 - Receiving single characters

char receivedChar;
boolean newData = false;

void setup() {
Serial.begin(9600);
Serial3.begin(9600);
Serial.println("");
}

void loop() {
recvOneChar();
showNewData();
}

void recvOneChar() {
if (Serial3.available() > 0) {
receivedChar = Serial3.read();
newData = true;
}
}

void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChar);
newData = false;
}
}

My hardware setup for the HC-06 board can be found at:


Only consider the bluetooth HC-06, I do not have the humidity sensor connected and that mine is an Arduino Mega.

When I send data from the Android phone to the HC-06 what I get on the monitor screen is as follows:

This just in ...

This just in ...

This just in ...

I sent from Android one at a time: a d s

Sould I consider the possibility my HC-06 is out of order?

Thanks
Paulo

Temporarily change this line

Serial.println(receivedChar);

to

Serial.println( (byte) receivedChar);

and see what happens. It should print the ASCII codes for the characters so you can see if it is receiving non-printable characters.

And please post your program using the code button </>

so it looks like this

. See How to use the Forum

...R

Hi Robin,

Just tested. With Serial.println( (byte) receivedChar);
It returns 240 for anything I type. In the case below I typed: A B C (one at a time)

This just in ... 240
This just in ... 240
This just in ... 240

I also testes several comm speeds for Serial3.begin(...) and with 38400 it behaves like below (In the case below I typed: A B C (one at a time)):

This just in ... This just in ... A This just in ... This just in ... B This just in ... This just in ... C

What do you think?
Paulo

pcborges:
I also testes several comm speeds for Serial3.begin(...) and with 38400 it behaves like below (In the case below I typed: A B C (one at a time)):

This just in ... This just in ... A This just in ... This just in ... B This just in ... This just in ... C

What do you think?
Paulo

That looks correct. My guess is that the blank lines are linefeed characters (try the trick with (byte) and see what happens).

...R

Hi, check test...

"A" sent from Android. "A" received sucessfuly by Android.

...
Serial.print("This just in ... ");
Serial.print(receivedChar);
Serial.print(" - ");
Serial.println( (byte) receivedChar);
Serial3.write(receivedChar);

This just in ... - 1 //any char sent from Android will generate this line ? Monitor No_Line_Ending
This just in ... A - 65

Thanks
Paulo

I'm not sure if you are looking for more advice. If so please post the complete program you have been trying and explain what you need help with.

I think you are just working with my first example which receives single characters. You will probably find that my second example that expects an end-marker (in the example it is a linefeed) will be more reliable if the device sending data is sending a linefeed character or some other terminator.

...R

Hi Robin, excuse me for not been clear enough.
Yes, I am still using your first exemple with some minor modifications and still looking for advice.

But you are right, Yesterday I was Reading your Serial Input Basics again and saw your additional exemples to send multiple characters and I am experimenting with them today.

In my Project I need to send data to the Aduino and get some other information back, so I need to be able to handle data flowing on both directions.

I am not new to programing but new to Arduino and C.
Still getting used to C's particularities on treating with strings.

I will try your examples and adapt them to my requirements:
1- need to send data that I can identify as text and values from Android to Arduino.
2- need to send formated data back (text and numbers)

Thanks for your assistance
Paulo

If you have the choice then the technique in my 3rd example with start and end markers will be the most reliable.

I am not familiar with Android programming but if you are writing your own code then the same technique would also be appropriate.

...R

Hi there,

I am using your 3rd example as the base.
That is the best so far to send data from Android to Arduino but I am having problems sending data back to Android.
I am not writing an Android app, just using one of the many available at the internet, just an Androis BT terminal, so I can just type and send for instance with easy, Works fine.

The code below has its own problems but a string sent from Android is sucessfuly printed at monitor and echoed back to Android:

Note: The echo part eventualy stops working I do not know why and I have to disconnect and reconnect the BT to have it working back again.

String readString;
char c;
int LedPin =10;

void setup() {
 Serial.begin(9600);
 Serial.println("OK then, you first, say something.....");
 Serial.println("");
 pinMode(LedPin, OUTPUT);
 Serial3.begin(38400);
}

void writeString(String stringData) { // Used to serially push out a String with Serial.write()
  for (int i = 0; i < stringData.length(); i++)  {
    Serial3.write(stringData[i]);   // Push each char 1 by 1 on each loop pass
  }
}


void loop() {
 while (Serial3.available()) {
  delay(3);
  c = Serial3.read();
  readString += c;
 }
 
 if (readString.length() >0) {
   
    if(readString[1]==1){
      digitalWrite(LedPin, LOW);
    } else {
      digitalWrite(LedPin, HIGH);
    }
    
    writeString(readString);
    Serial.println(readString);
    readString="";
 } 

}

The code below is a variation of your 3rd example, echo back to Android is not sent and I am trying to understand why. I believe it has something to do with the line:
---String readString;

// Example 3 - Receive with start- and end-markers

const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;

void setup() {
  Serial3.begin(38400);
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
}

void loop() {
  recvWithStartEndMarkers();
  showNewData();
}

void writeString(String stringData) { // Used to serially push out a String with Serial.write()
  for (int i = 0; i < stringData.length(); i++)  {
    Serial3.write(stringData[i]);   // Push each char 1 by 1 on each loop pass
  }
}

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;
  
  while (Serial3.available() > 0 && newData == false) {
    rc = Serial3.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      } else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    } else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}

void showNewData() {
  if (newData == true) {
    Serial.print("This just in ... ");
    Serial.println(receivedChars);
    writeString(receivedChars);
    newData = false;
  }
}

I will get back to Reading about String and Char to try to understand what is going on, any help is welcome.

Thanks
Paulo

The simple solution is (I think) to change this line

writeString(receivedChars);

to

Serial3.println(receivedChars);

...R

Hi Robin

It is working now.
I had to replace Serial3.write by Serial3.println but also use another BT terminal app.
I can now have reliable BT communication and connection for na extended period of time.

Thanks for your assistance.
Regards
Paulo