Pages: [1] 2   Go Down
Author Topic: Bluetooth: Android->Arduino wrong characters transmitted  (Read 2600 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I have a very strange situation.
I own an Arduino UNO and this Bluetooth module:
http://dx.com/p/jy-mcu-arduino-bluetooth-wireless-serial-port-module-104299

I have written an Android App to communicate with my Arduino, pairing etc.
works nicely.
But if I send as example the String "testtest" from my Android Smartphone,
the Arduino recieves "t¬.   ¹º", the number of characters is right, but only the first one is
correct.
I can confirm this behaviour with different transmitted data.

This is how i initialize my Bluetooth module:
Code:
#include <SoftwareSerial.h>
SoftwareSerial btConnection(8,9);
int pin = 1234;
char* name = "Arduino";

[snip]
void setupBTConnection()
{
  Serial.println("Init BT");
  btConnection.begin(9600);
  delay(cmdDelay);
  btConnection.print("AT");
  delay(cmdDelay);
  btConnection.print("AT+PIN");
  btConnection.print(pin);
  delay(cmdDelay);
  btConnection.print("AT+NAME");
  btConnection.print(name);
  delay(cmdDelay);
  btConnection.print("AT+BAUD8");
  delay(2000);
  btConnection.flush();
  btConnection.begin(115200);
  delay(2000);
  btConnection.flush();
  Serial.println("BT ready");
}

And this is how I read the data:
Code:
String inData="";

void loop()
{
   while(btConnection.available() > 0)
   {
      char aChar = btConnection.read();
      Serial.println("recieved ");
      Serial.println(aChar);
      if(aChar == '\n')
      {
         Serial.println("Ended");
        // recieved whole data, do something


        inData="";

      }
      else
      {
         inData+=(char)aChar;
      }
   }
}
The strange thing is, that it also never goes inside the
if(aChar == '\n') block.
The first char received is always right, and if I just send one char commands via bluetooth
everything works as expected. But I need to send more data.

First thought was that the bps are wrong, but all internet resources confirm that 115200 is the right one for bluetooth.
If i set it lower I only receive garbage, not even one char correct.
So is there something wrong in how I read the incomming data?

If I connect from CoolTerm on OSX to the Arduino over Bluetooth
with the following settings: baudrate 115200, data bits 8, parity none, stop bits 1
everything works.

So I am currently not sure where the issue is (Arduino or Android), but since
this subforum is about Networking i thought someone already tried to connect
from an Android device.

In Java (Android) I just do:
Code:
byte[] send = "testtest".getBytes();
 mChatService.write(send);
where mChatService holts the BluetoothSocket and writes the data on the OutputStream.
Basically it consists of the Android Sample App "BluetoothChat" modified a bit.
Basically I do createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); to connect to the Arduino.

Any ideas what to try out next?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would try leaving out the "AT+BAUD8" an the .begin(115200);  That should leave the rate set to 9600.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I now did the work and tried all baud rates from 1200 to 115200, all others did result
in more wrong chars. With 115200 at least the number of displayed chars was correct.
I tried a few bluetooth apps to send messages to my arduino do check if others work.
According to the sources I found, 115200 should be Ok for Android, so i checked with that:
works: BlueTerm, BluControl (it just sends one char, this also works fine with my code)
does not work: my App, Android Sdk BluetoothChat sample App, Amarino 2.0, SENA BTerm

I find it surprising that Amarino does not work. It should require 115200 baut rate, but instead of the
random integers I receive char garbage.

Any other ideas that might help? Possible sources of errors?
Logged

HU
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I would like to ask about your code. I have the same bluetooth modul but v1.05. I want to change te name and pin code, but I can't. When send "AT" from terminal, is replies OK. But all the other commands replies ERROR. So I tried your code, but it isn't work for me. What is the value of "cmdDelay"? If you run your code, do you get reply OK after each AT command? Thx.
« Last Edit: January 03, 2013, 04:51:22 am by Redy029 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I'm not sure, but maybe the condition "\n" is not correct for Char...

Try "NULL" or "\0"...

See the example Str3 in:
http://arduino.cc/en/Reference/String
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Redy029:
Code:
int cmdDelay = 1000;
Also values like 100 work for me, but a small delay is necessary.
And yes, I get OK for all commands, the Bluetooth device is then available with the new name and
another pin.

@kev8:
I don't think that is the problem, as
Code:
Serial.println(aChar);
already prints the single chars wrong.
The first one is always correct, the other ones are wrong.
Very strange situation. And I already checked all Baud rates.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Next guess:  There might be an AT commands to set data bits, parity and stop bits.  Perhaps those are not set to 8, None, and 1 by default and you have to set them manually.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

HU
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

durpex
Code:
int cmdDelay = 1000;
Also values like 100 work for me, but a small delay is necessary.
And yes, I get OK for all commands, the Bluetooth device is then available with the new name and
another pin.

That was my fault, I mixed up the TX and RX cables. I changed them and now it works. I think this way is faster and easier, then through a terminal. Thanx for the help, your code saved my day.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your suggestion johnwasser.

I tried to play around with the AT commands, but this is a bit messy, as I can't find any documentation
for this specific module.
As it did not respond to AT+UART? command (the one where I could configure parity and stop bits)
I downloaded the following code I found in the forum (just modified slightly):
Code:
/*
  Set Up a JY-MCU Bluetooth Dongle. Version 1.0
 
  http://dx.com/p/jy-mcu-arduino-bluetooth-wireless-serial-port-module-104299?item=6
  http://byron76.blogspot.com/2011/09/one-board-several-firmwares.html
 
  It's a very simple and inexpensive Bluetooth Serial Module. 4-Pin connection: 5V, GND, TX, RX.
  You can configure the NAME, PIN, and BAUD using this utility, settings are stored in the
  module firmware until you modify them again.
 
  This utility will search for the bluetooth module on all the possible BAUD options, then configure
  the module according to the configuration parameters below:
 
  Note: You can only configure the module when it does not have an active bluetooth connection!
 
  Jeff Simpson
  jeffsimpson@alum.wpi.edu
*/

#include <SoftwareSerial.h>


/* Beginning of Configuration Section */

// NAME maximum of 20 characters
char NAME[21] = "MyRobot";

// 4 digit numeric pin
char PIN[5] = "4444";
SoftwareSerial btConnection(10,11);
/*
  character code for BAUD
  1 for 1200 bps
  2     2400 bps
  3     4800 bps
  4     9600 bps
  5    19200 bps
  6    38400 bps
  7    57600 bps
  8   115200 bps
  9   230400 bps
  A   460800 bps
  B   921600 bps
  C  1382400 bps
  */
char BAUD='8';

/* End of Configuration Section */

char response[21]; // Storage for responses from module
int rv=0; // return value
int found=0; // module found

void setup() {
 
  // Open Serial Port for display
  Serial.begin(115200);
  Serial.print("Searching for BT Dongle");
 
  // Search for bluetooth module on all 12 possible baudrates
  long baud[12] = {1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 1382400};
  for(int a = 0; a < 12; a++)
  {
    btConnection.begin(baud[a]);
    btConnection.write("AT"); // "AT" will respond with "OK" if it is working
    delay(100);
    while(btConnection.available() < 0);
    btConnection.readBytes(response, 2);
    Serial.println(response);
    if (strncmp(response, "OK", 2) == 0)
    {
      Serial.print("\nBluetooth Module found at BAUD ");
      Serial.println(baud[a]);
      found=1;
      break;
    }
    else
    {
      Serial.print(".");
      //Serial.println(baud[a]);
    }
  }
  if (found==0)
  {
    Serial.println("\nERROR: Blueooth Module not found!");
    Serial.println("Make sure there are no active bluetooth connections and try again");
    while(1);
  }
  delay(100);

  btConnection.write("AT+VERSION"); // Check the firmware version, because we can
  while(btConnection.available() < 0);
  btConnection.readBytes(response,2);
  if (strncmp(response, "OK", 2)==0)
  {
    rv = btConnection.readBytes(response, 20);
    response[rv] = 0;
    Serial.print("BT Module Firmware Version: ");
    Serial.println(response);
  }
  else
  {
    Serial.println("Error checking BT Version");
  }
  delay(100);
 
  btConnection.write("AT+NAME"); // Set the bluetooth device name
  btConnection.write(NAME);
  while(btConnection.available() < 0);
  btConnection.readBytes(response,9);
  if (strncmp(response, "OKsetname", 9)==0)
  {
    Serial.print("Setting Name: ");
    Serial.println(NAME);
  }
  else
  {
    Serial.println("Error Setting Bluetooth Name");
  }
  delay(100);
 
  btConnection.write("AT+PIN"); // Set the PIN
  btConnection.write(PIN);
  while(btConnection.available() < 0);
  btConnection.readBytes(response, 8);
  if (strncmp(response,"OKsetPIN", 8)==0)
  {
    Serial.print("Setting PIN: ");
    Serial.println(PIN);
  }
  else
  {
    Serial.println("Error Setting Bluetooth PIN");
    Serial.println(response);
  }
  delay(100);
 
  btConnection.write("AT+BAUD"); // Set the BAUD
  btConnection.write(BAUD);
  while(btConnection.available() < 0);
  btConnection.readBytes(response, 2);
  if (strncmp(response, "OK", 2)==0)
  {
    rv = btConnection.readBytes(response, 7);
    response[rv] = 0;
    Serial.print("Setting BAUD: ");
    Serial.println(response);
  }
  else
  {
    Serial.println("Error Setting Bluetooth BAUD");
    Serial.println(response);
  }
  Serial.println("Bluetooth Setup Completed");
}

void loop() {
}


My output is the following:
Code:
Searching for BT Dongle
.
.
.
.
.
.
.Oé
.Oé
.Oé
.Oé
.Oé
.
ERROR: Blueooth Module not found!
Make sure there are no active bluetooth connections and try again

So instead of OK I recieve .
But I am sure I correctly recieved OK some time ago.
What could cause such an issue?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    while(btConnection.available() < 0);
The value returned by the available method is the number of bytes in the input buffer that have not been read. It is IMPOSSIBLE for that value to be less than 0.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

O is 0x4F and K is 0x4B so (adding Start and stop bits) in binary that should be:

XXXS01001111sXXXS01001011sXXX

é is 0xE9 in ISO Latin 1 so what you seem to be getting is:

XXXS01001111sXXXS11101001sXXX

Start = 0
stop = 1
X = 1 (dead time between characters)

XXX001001111 1XXX0 010010111XXX
XXX001001111 10111 010011XXX

Looks like it is somehow picking up three extra bits between the first and second character.  Instead of the one stop bit (1) of the first character and the single Start bit (0) of the second character it is is seeing 10111.  If there is a delay between characters it will be filled with 1's so if, instead of 8N1 it was sending 9Z2 (9 data bits, Zero parity, 2 stop bits) that would cause the 9th data bit to be read as a STOP bit, the parity to be read as a START bit, the STOP bits to be read as two data bits and the delay between characters to be read as a third data bit.

Just a note:  the Arduino serial code seems to ignore all framing errors.  It doesn't care if a STOP bit isn't 1 or if the Parity doesn't match.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your detailed explanation johnwasser.

I did some further experimenting but did not have any success.
The question is now what causes that problems.
I tried other ports on my Arduino UNO, so I think the Arduino itself should be ok.
So either the JY-MCU is defect, or there is an issue with the SoftwareSerial library.
In the meantime I will try to buy another one, but I would like to solve that issue.
Any ideas on what to try next?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any ideas on what to try next?

If you have access to an oscilloscope I would check the serial waveforms coming from the bluetooth module to make sure they have the expected bit timing and contents.  If they do, something is wrong with SoftwareSerial.  If they don't, something is wrong with the bluetooth module.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Unfortunately I do not have a oscilloscope available.
But I borrowed an Android Mega from a friend.
If I use the Hardware serial ports on the Mega, everything works perfectly, no odd bits  smiley-eek
Using SoftwareSerial it does not work on the Uno or Mega.

So I assume that the bluetooth module is fine, but the SoftwareSerial has a problem (or I used it the wrong way).
I have to give back the borrowed Mega, so I would like to get the issue solved.
I have a bit of software engineering background, but I am new to the Arduino stuff,
so any hints where to start are welcome.

Will try to have a look at the SoftwareSerial source, but I think without oscilloscope it is hard for me to check
my modifications.

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I had the bit order reversed.

OK = XXXS11110010sXXXS11010010sXXX
Oé = XXXS11110010sXXXS10010111sXXX

OK = 111 0 11110010 1 0 11010010 1 11111
Oé = 111 0 11110010 1  x11 0 10010111 1 11111

This makes more sense.  Looks like the SoftwareSerial just wasn't fast enough to catch the start bit of the second character.  It saw the first two '1' bits of the second character as idle line and saw the 0 in the third bit as a start bit.  You'd think that it wouldn't have the same problems are lower bit rates but apparently it does.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1] 2   Go Up
Jump to: