Serial communication via Bluetooth works but is has connection issues.

Hi

I just mannaged to get 2 bluetooth modules to communicate. it has been really tricky to get it running due to defective firmware in one module. I guess I have spend 25+ hours figuring out what was wrong...

Now I can transmitt data from a arduino that reads a adc and via bluetooth to a reciever arduino. Just what I needed.
But the connection can sometimes be tricky to get running. It appears that there is a timing issue and if the recieving arduino starts while the sender is transmitting it newer connects like it was intended.

I quess that some kind of end of transmition would be the solution but I cannot get it to work.
Even the samples for serial in the arduino IDE does not help me.

My code is:

int a, b, c;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
delay(100); // allowing my bluetooth module to boot up correctly. Might be a to long break, but it works for now.

Serial.println("The serial port is up ");
Serial.println("and running ");
Serial.println("");
}

void loop() {
while (Serial.available() > 0 )
{
//Serial.println("*** RECIEVING DATA ***");

a = Serial.parseInt(); // reads the ints from serial to the a,b and c ints.
b = Serial.parseInt();
c = Serial.parseInt();

Serial.println(a); // send the recieved data to serial so I can see if it looks right
Serial.println(b);
Serial.println(c);

Visual(); // to turn a led if b>20
if (b > 20)
{
Serial.println("*** ALARM ");// the b parameter is > 20
Serial.println("
END OF DATA ");
Serial.println();
}
else {
Serial.println("
END OF DATA ***");
Serial.println();
}
//Serial.flush();
}
}

void Visual() // turns a led on if the data in b is more than 20.
{
if (b > 20) {
digitalWrite(13, HIGH);
}
else
{
digitalWrite(13, LOW);
}
}

/*
Serial Monitor

The sending Arduino is sending a series of 3 integers and a Serial.println
It looks like
01,0,3

The first int is a counter that counts up for each transmition.
The next int is a reading from a sensor. This is really what Im after.
The last int is just rubbish nmbers, but I might use it later for a checksum or similar error detection purpose to get data validation a cheap way.

This is a sample of two reaadings in my serial monitor.

52 // the first int who is just a counter
293 // the first int who is just a counter
198
*** ALARM ***
*** END OF DATA ***

53
0
0
*** END OF DATA ***

*/

I would recommend setting up a proper connection before transmitting data through pair, bind and connect. Here is an example..
http://www.martyncurrey.com/connecting-2-arduinos-by-bluetooth-using-a-hc-05-and-a-hc-06-pair-bind-and-link/

Chaitanya1
Pairing bind and connection has been set up. So has the connection speed.

The problem is that the parsing of the data does sometimes go wrong. This depends on the time the recieving arduino starts. Sometimes it connects while the senderis sending transmitting data. In this instance the numbers in my 3 ints are shuffeled around.

I need some kind of end of line/newline detection on my reciever. But I check for a \r it does not work. Odd

Pete

pls send the complete code.. I might be of better use..

If Serial.available () returns 1 or 2 you are still trying to ready 3 bytes. Maybe you are getting out of sync? Try changing this:

  while (Serial.available() > 0 )
  {
    //Serial.println("*** RECIEVING DATA ***");

    a = Serial.parseInt();  // reads the ints from serial to the a,b and c ints.
    b = Serial.parseInt();
    c = Serial.parseInt();

to this:

  if (Serial.available() >= 3 )
  {
    //Serial.println("*** RECIEVING DATA ***");

    a = Serial.parseInt();  // reads the ints from serial to the a,b and c ints.
    b = Serial.parseInt();
    c = Serial.parseInt();

and see if it helps. In the long run you probably should have an end of transmission character and only send data after you have read it, you will know the sender is waiting.