[Solved] Bluetooth module sending only 63 bytes when Arduino is "stressed"

Hello !

I'm running this little snippet of code to test my new Bluetooth module (BlueSMiRF Silver (RN-42)) :

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>

int bluetoothTx = 9 ;
int bluetoothRx = 8 ;
String inData;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
LiquidCrystal lcd(2,3,4,5,6,7);

void setup()
{
	Serial.begin(9600);
	delay(100);
	bluetooth.begin(115200);
	bluetooth.print("$$");
	delay(100);
	bluetooth.println("U,9600,N");
	bluetooth.begin(9600);
	delay(100);
	lcd.begin(16,2);
	lcd.clear();
}

void loop()
{
	while (bluetooth.available() > 0) {
		char recu = bluetooth.read() ;
		inData += recu ;
		
		if (recu == '

See the for-loop commented at the end ? When it's activated, I can only receive data up to 63 bytes at a time. What I do to test it : I open minicom on the serial port. Everything works fine for short strings (I use my Android smartphone to send them), such as "aaaa$". It displays :

Data received : aaaa$

But when I send a string that is longer than 63 characters, only the 63 first characters are sent, the next ones are lost. Since the "$" is after the 63 first characters, nothing is displayed. The 63 characters are added to inData nonetheless. So when I then send a short string such as "aaaa$", it gets added to InData itself and the "$" triggers the displaying of everything and the emptying of inData :

Data received: longstring_longstring_longstring_longstring_[imagine a lot of characters here]_longstring_end_of_longstringaaaa$

When the last lines of the code are commented, such as what I've pasted here, everything is passed right away, even veeery long strings.

Anyone know why it behaves like that, and what I could do in the eventuality where the loop() function has to perform a lot of operations AND I have to transmit long strings in one shot ?

Thank you !) {
Serial.print("Data received :");
Serial.println(inData);
inData = "";
}
}
/for(int i=0 ; i<1000 ; i++) {
int victim = random(1,1000);
}
/
}


See the for-loop commented at the end ? When it's activated, I can only receive data up to 63 bytes at a time. What I do to test it : I open minicom on the serial port. Everything works fine for short strings (I use my Android smartphone to send them), such as "aaaa$". It displays :

> Data received : aaaa$

But when I send a string that is longer than 63 characters, only the 63 first characters are sent, the next ones are lost. Since the "$" is after the 63 first characters, nothing is displayed. The 63 characters are added to inData nonetheless. So when I then send a short string such as "aaaa$", it gets added to InData itself and the "$" triggers the displaying of everything and the emptying of inData :

> Data received: longstring_longstring_longstring_longstring_**[imagine a lot of characters here]**_longstring_end_of_longstringaaaa$

When the last lines of the code are commented, such as what I've pasted here, everything is passed right away, even veeery long strings.

Anyone know why it behaves like that, and what I could do in the eventuality where the loop() function has to perform a lot of operations AND I have to transmit long strings in one shot ?

Thank you !

The Arduino can read from the serial buffer many times faster than serial can put data into it. So, in your code bluetooth.available() will often be zero because the one byte that was available will have been removed from the buffer and the program will have moved on to your for loop. Therefore, the for loop will run many times, possibly each time through loop() and not just once. If this slows down the processing of incoming serial data then the 64 byte serial buffer will fill up and you will lose data.

Your first priority is to ensure that serial data is processed as quickly as possible. Can you increase the baud rate so that it arrives faster so there is more chance of data being available when you check ? Can you change the while to if so that you process data as it arrives and can do something else if no data is available at that time ? Can you provide feedback to the sending device indicating that it should pause sending ?

Thank you for your answer.

I won't increase the baud rate, because when I was using the default baud rate (115'200) some data arrived mangled. The Sparkfun tutorial itself advised to use a lower bitrate such as 9600 to avoid this. I might try again, though.

Would an "if" really help ? I mean, if there's nothing the "if" is not executed, so the rest will be, and if the rest takes a long time then new data will arrive in the meantime... no ?

Finally, I can't ask the modem when it's ready as far as I know.

So I'm going to try and increase the baud rate. If it doesn't work I'll stick to sending strings shorters than 64 characters. My custom Android app will have to make sure the strings are short, but what I intend to do with it it shouldn't be a problem.

Would an "if" really help ? I mean, if there's nothing the "if" is not executed, so the rest will be, and if the rest takes a long time then new data will arrive in the meantime... no ?

The trick is to look for data very frequently and if it is available, process it. If not, do whatever else you need to do but don't block execution of the code. For instance, don't use a for loop as you do to generate 1000 random numbers, instead, generate one random number each time through loop() so that you can continue to check for new serial data.

I understand better now, thank you.

I will avoid big loops that would slow loop() down. If I have to use a function that is inherently slow, I will stick to short strings because I've tested with 115'200 baud and it doesn't solve the problem.

Hi Biganon, I have same problem with you, Do you know anyway to solved. Data i want to send is more than 63 characters. Sorry about my bad english. Thanks