[SOLVED] HC-06 returning gibberish data

Hello,
I recently started a project with ws2812b LEDs using the FastLED library. I made an android app to control the LEDs (color, pattern, brightness etc) and to do that I'm using the HC06 bluetooth module on my arduino uno. I've set the app to send a string (in byte array format) and from then on the arduino to receive the data and start processing it. The problem that I'm currently facing is that for some reason the arduino is displaying/receiving gibberish data instead of the string i'm currently sending.

Arduino code:

SoftwareSerial mySerial(5, 6); // using the hc06 with software serial so I can connect my laptop at the same time

void setup() {
  Serial.begin(9600);
  mySerial.begin(19200);
}

void loop() {    
  if (mySerial.available()){
    char outBuffer[6];
    int i = 0;
    
    while(mySerial.available() > 0){
      outBuffer[i] = (byte)mySerial.read();;
      i++;
      delay(10); // there was a post on the forum suggesting to use delay in order for the arduino to have more 
                // time processing the incoming bytes
   }
    Serial.println(outBuffer);
  }
}

Java code that sends the data from the android app

btSocket.getOutputStream().write(btData.getBytes());

Running this code i've gotten this results:

  • FF881FFF8BC⸮⸮⸮

  • ⸮SFF661FF4621E

  • ⸮SFF661FF4621E

  • ⸮SFF661FF4621E

  • S⸮43E1EF

  • ⸮⸮⸮⸮2⸮600⸮⸮⸮

  • ٚ⸮⸮⸮⸮

  • j⸮⸮⸮00

  • ⸮⸮2 ⸮C00

  • ⸮700FF4800⸮⸮⸮

Strings that the app is sending:

  • FF8B25
  • F4621E
  • F43E1E
  • FF0600
  • FFD000
  • FFB300
  • FB8C00
  • FF4800

I'm using 19200 baud rate because it produced the best results (using 9600 the results didnt even have a letter in them, they were pure gibberish).

I'm also using the 1k/2k voltage divider (instead of using 1k I used a 10k as well to check and it gave better results)

Sometimes, very rarely, the arduino displayed the exact string I was sending, but that only happens like 1 every 50 times.

The reason I want to receive strings on my arduino is because I want to use HSV colors in order for me to control the brightness better and because the color picker I have on the app doesnt give rgb colors back (also judging from the results I get, even if I used integers I would still have this problem). Ideally I want to receive a string with the format "color;brightness;mode;power" and later assign the numbers to the corresponding variables but I'm currently testing by sending the color string only.

The serial input basics tutorial shows robust ways to receive serial data.

What baud rate is the HC06 set to? The default baud rate is 9600.

Set the Serial baud rate at the same or faster than the software serial baud rate. Don't forget to set the serial monitor the same.

Configuring the HC06.

stathis_99:
Hello,
I made an android app

It looks like a simple Baud rate issue. If it clearly isn't, you need to send a string of "hellos" from a standard android terminal.

  1. If your message holds 6 chars, the outBuffer must have a length of min 7! At end of your message you must add a '\0' as an and end of the char string.
  2. Your delay(10) is absolutely wrong. In this 10 ms the HC-05 can send up to 20 chars.
  3. Your print bitrate must be like or faster then the HC-05 bitrate.

groundFungus:
Configuring the HC06.

I've followed the guide but for some reason I'm not getting a response from my hc06. Even a simple sketch as this one

if (Serial.available())
{
mySerial.write(Serial.read());
}

if (mySerial.available())
{
Serial.write(mySerial.read());
}

doesn't return a response, and the sketch on the guide you mentioned only shows "testing x baud rate" but doesnt give out what baud rate the module is set to.
Forgot to add, I manually tested all the baud rates, that's why I know the 19200 gives the best results. So should I assume that my hc06 is set to 19200? I can't get the AT commands to work.

It looks like a simple Baud rate issue. If it clearly isn't, you need to send a string of "hellos" from a standard android terminal.

Sending a bunch of "hello" from a standard android terminal had these results:
llo
⸮⸮⸮⸮ff
hello
⸮002ff
llo2ff
[002ff
⸮⸮⸮⸮f
⸮⸮⸮ff
⸮002ff
⸮⸮⸮⸮f

RudolfAtRTC:

  1. If your message holds 6 chars, the outBuffer must have a length of min 7! At end of your message you must add a '\0' as an and end of the char string.
  2. Your delay(10) is absolutely wrong. In this 10 ms the HC-05 can send up to 20 chars.
  3. Your print bitrate must be like or faster then the HC-05 bitrate.

Tried all 3 but nothing changed on the receiving end.

stathis_99:
Sending a bunch of "hello" from a standard android terminal had these results:
llo
⸮⸮⸮⸮ff
hello
⸮002ff
llo2ff
[002ff
⸮⸮⸮⸮f
⸮⸮⸮ff
⸮002ff
⸮⸮⸮⸮f

You don't say whether you checked the baud rates, and it looks like you haven't. You don't say that you have configured Bluetooth for 19200 - AT+BAUD5. If you haven't specifically done that, it isn't, and you had best change your code to the default 9600 and leave the config alone. You also might try sending hellos TO Android, which is what i should have said in the first place. Something like:

loop{
myserial.println("hello");
delay(1000);
}

Nick_Pyner:
You don't say whether you checked the baud rates, and it looks like you haven't. You don't say that you have configured Bluetooth for 19200 - AT+BAUD5. If you haven't specifically done that, it isn't, and you had best change your code to the default 9600
You also might try sending hellos TO Android, which is what i should have said in the first place

Oh my bad sorry, I thought you wanted to test whether the app is reading the input and sending it correctly. I've run the test you meant and now it seems more clearly that my hc06 is set to 19200 (see attachments). Don't know why it is set there at default, but running it at 19200 baud rate send the hellos perfecty while running it at 9600 did not. But now its weird that im not receiving correct data since its transmitting correct data. Also I did check every baud rate and the 19200 was getting the best results, thats why i had it set to 19200 (I forgot to mention it at the op).

SoftwareSerial mySerial(5, 6); // using the hc06 with software serial so I can connect my laptop at the same time

This is your issue. swSerial heavily relies on interrupts being turned on, and .show() turns them off. Unless you can control the moment that the transmission from the BT-unit takes place (which i think is not possible) swSerial is going to be unreliable. hwSerial does also depend on interrupts being turned on, but only once per received byte, which is an interval in which you can send a signal to quite a few leds. Using hwSerial you will for sure receive complete bytes, though you may still lose a complete byte if you turn interrupts off for more time than it takes to send anything from 1 to 2 complete bytes. If you want to use Serial debug, you can use swSerial for that.

Deva_Rishi, I thought the same when I saw the title but the code in the original post did not call show() nor did it have any code for LED strips. That is why I suspected a baud ate problem.

I use software serial with an application that uses WS2812 strips. Indeed, you cannot use SoftwareSerial and FastLED functions at the same time as FastLED show() turns off interrupts. What I do in my application is, as soon as SoftwareSerial availabel() returns greater than 0, I go to a function that stops using show() and reads and processes the serial input.

Deva_Rishi:

SoftwareSerial mySerial(5, 6); // using the hc06 with software serial so I can connect my laptop at the same time

This is your issue. swSerial heavily relies on interrupts being turned on, and .show() turns them off.

I did not know this neither did i find anything like that online so I left it out of the post because I thought it was irrelevant (oups). Thank you very much.

groundFungus:
I use software serial with an application that uses WS2812 strips. Indeed, you cannot use SoftwareSerial and FastLED functions at the same time as FastLED show() turns off interrupts. What I do in my application is, as soon as SoftwareSerial availabel() returns greater than 0, I go to a function that stops using show() and reads and processes the serial input.

I will probably try to do something like this aswell.

Commenting out the .show() shows the data correctly!! Thank you all very much for the help, I will figure out how to do the rest of this exactly by myself (probably something similar to groundFungus). Again thanks, this was bugging me for a few days.

OP, I hope that this is a lesson. We wasted our time under a false impression because you did not post the actual code that caused the problem.

Had I known that what you were actually doing I would have known what to do in the first reply.

Yeah kinda my bad. But I did mention about the library and the leds at the beginnng of the post, maybe I should have let the code in aswell. I'm inexperienced with the hc06 module so I though it was a coding error and I posted the part where I read the data, not the entire sketch (because it's over 300 lines). Should have had a code block with the entire sketch at the end

OP, I hope that this is a lesson. We wasted our time under a false impression because you did not post the actual code that caused the problem.

I dunno, i read the post, and then i thought, people do know this, why hasn't anyone mentioned it ? (thinking about it i was a little disappointed actually)
I would avoid swSerial in combination with ledstrip if possible hehe, but i have a fully working solution as well, keep in mind your app will simply have to wait for confirmation of a correct transmission or otherwise send again. Also usually the signal to the strip is not very long (time wise) and if you have a low framerate there is a lot of space in which to receive. Still hwSerial is easier to implement, what do you really use it for now ? If it is just for debug, there is no real use for it. (i have mine occupied with DMX which you just can do on swSerial, although actually on some boards maybe you can if an AVR can receive at 115200 already hmm there is an idea) You could even combine both on the hwSerial, the BT-unit will not respond to you debug commands, and as long as you don't share the arduino's RX pin (you don't know when the BT-unit will start to send stuff) there is no problem.

To be honest its only used for debugging so I might as well use the hwSerial. I'll probably do enough debugging right now while its on swSerial so I can debug it fully and then switch to hwSerial when I connect it back to my led strips.
Thanks again for the help :slight_smile:

Deva_Rishi:
I dunno, i read the post, and then i thought, people do know this, why hasn't anyone mentioned it ?

Yes, well, that's interesting. I thought I could see the problem, and didn't even think it was necessary to see the code, but I actually had no idea of what was going on. My next suspicion was going to be about the Bluetooth module being fake, as OP's is pretty clearly 19200 by defualt, and that may suggest an HC-05 masquerading as an HC-06.

⸮⸮⸮⸮ff
hello
⸮002ff

But this clearly indicates broken bytes.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.