Bluetooth Data Retrieval Problem

Hello everyone,

I'm trying to receive data from phone over Bluetooth using the HM-19 Bluetooth module. I've also attempted this with the HC-06 module, for future reference. However, sometimes when it detects the received data, it only receives what seems like bits and pieces. Here is my code:

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

void loop() {
  Serial1.flush();
  if (Serial1.available() > 0) {
    String tempText = Serial1.readString();
    Serial.println(tempText);
  }
}

For example, if I send "Hello", it will work most of the times, but sometimes it reads "Heo", or different combinations of half the data or something. Interestingly it doesnt read garbage data, but it just seems to leave some parts out. I've used different apps to send the data both through android phones and iphones.

Any help will be appreciated.
Thanks.

Your sketch is working fine in my following MEGA.
hc5-19.png
Figure-1:

smMz.png
Figure-2:

I would like the following sketch instead of your's one which use String (with Capital S) "data type" that cause "memory fragmentation/holes" in the limited memory space of AVR Architecture.

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

void loop()
{
  while (Serial1.available())
  {
    char x = Serial1.read();
    Serial.print(x);
  }
}

smMz.png

hc5-19.png

Yes. I understand and have done everything in this schematic and sketch as well, except for the char part. I would like to keep the variable in some form of String type. I have simplified the code quite extremely. Basically I have led strips that are connected in matrix and im making them scroll text from right to left. I want it to get the code data from the bluetooth module, sent from an app. I have confirmed that the app sends everything properly but the receiving is usually messed up. Out of 10 tries, about 6 or 7 of them will be broken text. For example if I type "Hello", it will be "Heo" more than half the time, until it finally works. And the end user could send data at any point in type in a form of a String data type. If I were to use char, what else would I have to do?

1. The approach is:
Start with something called “basic (simple and elementary)” and make it work and then add all other complexities in an incremental order.

2. Your initial problem is the reception of broken message which I have not encountered even with String Class (Capital C). So, use C-string and check that you are not receiving any broken message and then move to the String if you really need to use it.

I'm trying to receive data from phone over Bluetooth using the HM-19 Bluetooth module. I've also attempted this with the HC-06 module, for future reference.

It's unclear to me if you are working with an HC06(Bluetooth 2.0) or an HM19 which is a BLE device. The protocols are very different.

cattledog:
It's unclear to me if you are working with an HC06(Bluetooth 2.0) or an HM19 which is a BLE device. The protocols are very different.

I have read somewhere that HC05 and HC19 are almost the same except that HM19 is of low power category.

I think this is misleading nonsense. The “except that HM19 is of low power category” means that it is a long way from a plain-vanilla HC-06, and the two are not casually interchangeable. I rather feel that OP is actually using an HC-06 anyway, which would perhaps explain why there is something going down the line, even if it is junk, rather than nothing. IF HC-06 IS BEING USED it would suggest the code is just a bit clunky, and maybe a simple delay(3); at the start of the while is all that is required.

a similar example would be

// reset button starts this
String readString;
char c;

void setup()
{
    pinMode(0, INPUT_PULLUP);// only needed for  JY-MCU v1.06?
    Serail1.begin(9600);
    Serail1.println("OK then, you first, say something.....");
    Serail1.println("Go on, type something and hit Send,");
    Serail1.println("then I will repeat it!");
    Serail1.println("");
}

void loop() {
    while (Serail1.available()) 
  {
    delay(3);  
    c = Serail1.read();
    readString += c; 
  }// end while
  if (readString.length() >0) 
  {  
    Serail1.write(c);  
   readString="";  
  } // end if
}

Yes, there are Strings, but I have never known that to be a problem in a situation like this.

IF BLE IS BEING USED, I believe there is now something about using it for SPP data transfer on the Martyn Currey website.

Nick_Pyner:
IF HC-06 IS BEING USED it would suggest the code is just a bit clunky, and maybe a simple

delay(3);

at the start of the while is all that is required.

Would appreciate if you kindly briefly explain the technical reason of inserting the said 3 ms delay. If this is just a fix, then it is Ok for me for this particular case and not in general.

I do it because it was suggested to me by somebody I trust, it fixed the problem I had, and I have used it ever since. I think this might have come from LarryD in Saskatoon, but it was so long ago that I cannot be sure. I also think the problem I had was much the same as OP's, but I cannot be sure of that either. The posted code has been used by a lot of people and has never been questioned. There are a number of ways for getting a basic transfer of serial data, and I have never used serial flush, but it appears that there is nothing much wrong with OP's.

Nick_Pyner:
I do it because it was suggested to me by somebody I trust, [...].

Cool! :slight_smile: