-2

I want to create a system that can send messages between android app with arduino using bluetooth module. I Used bluetooth terminal (android) made by qwerty

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
String Data = "";

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

void loop() // run over and over
{
  while(mySerial.available()==0)
  {}

  char character;
  while(mySerial.available()>0)
  {
    character = mySerial.read();
    mySerial.write(character);
  }

  data = data + character;

  if (character == 13) {  
    mySerial.print("Received: ");
    mySerial.println(data);
    data = "";
  } 
}

Everything it's ok when I send single character, but if I send string data (more than 1 character) arduino can't received it correctly. always error data in second character received.

Anyone can describe and help me to solve the problem? Any response are appreciated.

If solved the problem, I will posted the guide.

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
patusacyber
  • 5
  • 1
  • 5

2 Answers2

1

If you are trying to get more than one character, use arrays to store all the characters from the buffer at once.

May be this will give you some idea.

this post does it nicely

Community
  • 1
  • 1
Ccr
  • 686
  • 7
  • 25
  • Hy CCR. Thanjs you.. I have try that's tutorial, its worked if using terminal. But if I used Bluetooth communication (using android phone) I confused to implement the code. Any solution or sample using Bluetooth communication? – patusacyber Aug 27 '15 at 16:14
1
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
String Data = "";

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

void loop() // run over and over
{
  while(mySerial.available()==0)
  {}

  char character;
  while(mySerial.available()>0)
  {
    character = mySerial.read();
    mySerial.write(character);
    data = data + character;
  }

 // data = data + character;

  if (character == 13) {  
    mySerial.print("Received: ");
    mySerial.println(data);
    data = "";`enter code here`
  } 
}
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Shravya
  • 26
  • 1