Arduino Uno R3 + Xbee Shield + Bluetooth Bee V2: how to?

Hi, I'm trying to write a sketch in order to estabilish a bluetooth connection between Arduino and Android.

I have an Arduino Uno R3 + Libelium Xbee Shield + BluetoothBee V2.

In AT mode, I can smoothly do it with the following commands:

AT+INIT
AT+IAC=9E8B33
AT+CLASS=0
AT+INQM=1,9,48
AT+INQ
AT+PAIR=MAC_ADDRESS,20
AT+LINK=MAC_ADDRESS

So what I want is to make it a sketch.

Searching on the forum and reading various tutorials (written for different boards and bluetooth bee versions), I came up with this:

#include <SoftwareSerial.h>

const int bluetoothTx = 2;
const int bluetoothRx = 3;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  pinMode(bluetoothTx, OUTPUT);
  pinMode(bluetoothRx, INPUT);
  
  Serial.begin(38400);  
  bluetooth.begin(9600);
  delay(1000);
        
  bluetooth.print("\r\nAT+INIT\r\n");             // inizializzazione SPP
  bluetooth.print("\r\nAT+ROLE=0\r\n");           // imposta come slave  
  bluetooth.print("\r\nAT+IAC=9E8B33\r\n");       // impostazione Inquire Access Code di default
  bluetooth.print("\r\nAT+CLASS=0\r\n");          // accetta in connessione tutte le tipologie di dispositivo
  bluetooth.print("\r\nAT+INQM=1,9,48\r\n");      // inquire mode: RSSI, max 9, timeout 48
  bluetooth.print("\r\nAT+INQ\r\n");              // avvio
  delay(1000);
  
  Serial.print("Bluetooth should be initialized now");
}

void loop()
{ 
  if( bluetooth.available() )
    Serial.print(bluetooth.read());
    
  if( Serial.available() )
     bluetooth.write(Serial.read());    
}

But the module is not initialized and I can't proceed further.

Note that AT commands in Serial.print() are not the same set I use for AT mode: I added AT+ROLE=0 to set the module as Slave, and removed AT+PAIR and AT+LINK because after doing AT+INQ my phone is able to pair and connect to bluetooth module.

What am I doing wrong?

TIA

But the module is not initialized and I can't proceed further.

What response do you get from each of those AT commands? It helps to read the response, and send a new command only when the previous one responded in a positive way. ERROR is rarely positive. OK almost always is.

Nevermind Paul, I just solved: everything was correct, except I had to change in AT mode the bluetooth module baud rate to 9600 since it was at 38400.

I have to thank Ragnorok and the guys from the irc channel who helped me in such a short time! :smiley:

Thanks for your reply anyway.

Sorry, I thought I managed to do this, but I didn't.

So... the code above makes me capable of estabilish a connection from my Android phone.

But I can't send/receive any data from both ends. (Using serial monitor and BlueTerm)

The AT commands wrote in the code are right, I have the proof because when I write them by hand in AT mode I can successfully estabilish a connection and start communicating.

Hence I guess they're not "read" properly.

I'm trying debugging, here's the code modified for this purpose:

#include <SoftwareSerial.h>

const int bluetoothTx = 2;
const int bluetoothRx = 3;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  pinMode(bluetoothTx, OUTPUT);
  pinMode(bluetoothRx, INPUT);
  
  Serial.begin(38400);  
  bluetooth.begin(9600);
  delay(1000);
  
  Serial.println(".: Bluetooth Bee V2 Chat\n");
  
  Serial.println(bluetooth.println("AT\r\n"));
  delay(1000);
  Serial.println(bluetooth.println("AT+UART=9600,1,2\r\n"));    // setting serial port parameters     
  Serial.println(bluetooth.println("AT+INIT\r\n"));             // SPP initialization
  Serial.println(bluetooth.println("AT+ROLE=0\r\n"));           // slave mode  
  Serial.println(bluetooth.println("AT+IAC=9E8B33\r\n"));       // setting default IAC
  Serial.println(bluetooth.println("AT+CLASS=0\r\n"));          // accept all device classes
  Serial.println(bluetooth.println("AT+INQM=1,9,48\r\n"));      // inquire mode: RSSI, max 9, timeout 48
  Serial.println(bluetooth.println("AT+INQ\r\n"));              // inquire
  delay(1000);

  Serial.println("bluetooth available: " + bluetooth.available());
  Serial.println("serial available: " + Serial.available());
  if( bluetooth.available() > 0 ) bluetooth.print(".: Chat now!\n\n");
  else Serial.println(".. Chat failed! :(\n\n");

}

void loop()
{
  if( bluetooth.available() > 0 )
    Serial.print((char)bluetooth.read());
    
  if( Serial.available() > 0 )
    bluetooth.print((char)Serial.read());
}

Results in Serial Monitor:

.: Bluetooth Bee V2 Chat

6
20
11
13
17
14
18
10
bluetooth available: 
serial available: 
.. Chat failed! :(

I'm worried about those available() functions, they're supposed to return an integer, but apparently they return... empty strings?

Paul, where are you buddy!? :smiley:

Anybody!? :smiley: :smiley:

TIA again

  Serial.println(bluetooth.println("AT+UART=9600,1,2\r\n"));    // setting serial port parameters

Why would you want to print the number of characters sent? How is that useful information?

  Serial.println("bluetooth available: " + bluetooth.available());

Why are you trying to ADD an integer value to a string?

Inventing shortcuts, and expecting them to work, is useless.

PaulS:
Why are you trying to ADD an integer value to a string?

I thought I was concatenating, like Java, which is derived from C++... I assumed wrong.

Inventing shortcuts, and expecting them to work, is useless.

I'm sorry, I don't pretend to invent shortcuts, I'm just trying to solve this.

Please forgive me If I am so noob.

Could you suggest me something?

I wrote a debugging version of it:

#include <SoftwareSerial.h>

const int bluetoothTx = 2;
const int bluetoothRx = 3;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

char buffer[18];

// checks the result of AT commands in setup()
void checkOK()
{
  if( bluetooth.available() > 0 ) {
    Serial.print("I entered line 14!");
    int index = 0;
    delay(100);  // let the buffer fill up
    int numChar = bluetooth.available();
    if( numChar > 15 ) numChar = 15;
    while( numChar-- ) buffer[index++] = bluetooth.read();
    
    Serial.print("Result: ");
    Serial.println(buffer);
    
    // Clear the text and serial buffer
    for( int x=0; x<16; x++ ) buffer[x]='\0';
    bluetooth.flush();
  }
  else {
    Serial.print("available: ");
    Serial.println(bluetooth.available());
  }
}

void setup()
{
  pinMode(bluetoothTx, OUTPUT);
  pinMode(bluetoothRx, INPUT);
  
  Serial.begin(38400);
  Serial.flush();
  
  bluetooth.begin(9600);
  bluetooth.flush();
  
  delay(1000);
  
  Serial.println(".: Bluetooth Bee V2 Chat (debugging)\n");

  bluetooth.println("AT+UART=9600,1,2\r\n");    // setting serial port parameters  
  checkOK();  
  bluetooth.println("AT+INIT\r\n");             // SPP initialization
  checkOK();
  bluetooth.println("AT+ROLE=0\r\n");           // slave mode  
  checkOK();
  bluetooth.println("AT+IAC=9E8B33\r\n");       // setting default IAC
  checkOK();
  bluetooth.println("AT+CLASS=0\r\n");          // accept all device classes
  checkOK();
  bluetooth.println("AT+INQM=1,9,48\r\n");      // inquire mode: RSSI, max 9, timeout 48
  checkOK();
  bluetooth.println("AT+INQ\r\n");              // inquire
  checkOK();
  
  delay(1000); 
}

void loop()
{ 
  if( bluetooth.available() )
    Serial.print(bluetooth.read());
    
  if( Serial.available() )
     bluetooth.write(Serial.read());    
}

Serial monitor:

.: Bluetooth Bee V2 Chat (debugging)

available: 0
available: 0
available: 0
available: 0
available: 0
available: 0
available: 0

Both Serial.available() and bluetooth.available() are always equal to zero.

How is that possible?

  Serial.begin(38400);
  Serial.flush();
  
  bluetooth.begin(9600);
  bluetooth.flush();

Open a port. Block until all pending data has been sent. Why? If you don't understand what a function does, ask. Don't just call it in the hopes that it will correct all the worlds ills.

    for( int x=0; x<16; x++ ) buffer[x]='\0';

One stop sign is enough. 16 make it appear that you don't know what you are doing.

  bluetooth.println("AT+UART=9600,1,2\r\n");    // setting serial port parameters  
  checkOK();

Does each AT command really need two carriage returns and two line feeds?

Serial data arrives slowly. I would not expect an instantaneous response.

First, just to let you know this code is adapted from Project 10 of this Cookbook (page 58) they suggested me in the Arduino IRC channel.

PaulS:

  Serial.begin(38400);

Serial.flush();
 
 bluetooth.begin(9600);
 bluetooth.flush();



Open a port. Block until all pending data has been sent. Why? If you don't understand what a function does, ask. Don't just call it in the hopes that it will correct all the worlds ills.

This somehow assure the buffer is cleaned and no inconsistent data is wrote in.

    for( int x=0; x<16; x++ ) buffer[x]='\0';

One stop sign is enough. 16 make it appear that you don't know what you are doing.

That's a NULL char, not a 'stop sign', as far as I know. That FOR loop is only about emptying the array, as you can read from the comment above.

  bluetooth.println("AT+UART=9600,1,2\r\n");    // setting serial port parameters  

checkOK();



Does each AT command really need two carriage returns and two line feeds?

Serial data arrives slowly. I would not expect an instantaneous response.

If you mean that number 2, its not related to "\r\n" but is part of the AT command.

Hence, serial data arrives too slowly. How can I solve it?

And please, don't be rude. I know I am a newbie, but I'm trying my best.

This somehow assure the buffer is cleaned and no inconsistent data is wrote in.

No, it does not. Look up what the flush() function does post 1.0.

That's a NULL char, not a 'stop sign', as far as I know.

It is. It's there to tell string functions that they have reached the end of the valid data in the array, and, therefore, they should stop reading. Hence the analogy to a stop sign.

That FOR loop is only about emptying the array, as you can read from the comment above.

It is not. When the for loop gets done, the array is as full as when it began. It is full of different things, I'll grant you, but the array is NOT empty.

If you mean that number 2, its not related to "\r\n" but is part of the AT command.

The string being sent contains \r\n. The println() method calls the print() method to send the string with the \r\n. Then, it calls print() to send \r\n.

Therefore, using println() to send a string containing \r\n results in \r\n\r\n being the last 4 characters sent.

AT commands being pretty picky, I see that as a possible source of error. That is not the same as probable. Just something to consider.

Hence, serial data arrives too slowly. How can I solve it?

Change the baud rate to speed up the process. Wait for a response, like you do on the forum.

Those are the only options I'm aware of.

And please, don't be rude.

Blunt, forceful, to-the-point I'll admit to. Rude I will not.

Reading Xbee shield schematic and Bee pins configuration I came up with something different.

#include <SoftwareSerial.h>

const int bluetoothRx = 2;
const int bluetoothTx = 3;

SoftwareSerial bluetooth(bluetoothRx, bluetoothTx);

// checks the result of AT commands
int checkOK()
{
  if( bluetooth.available() > 0 )
  {
    char buffer[18];
    int index = 0;
    delay(100);  // let the buffer fill up
    int numChar = bluetooth.available();
    if( numChar > 15 ) numChar = 15;
    while( numChar-- ) buffer[index++] = bluetooth.read();
    
    Serial.print("OK ");
    Serial.println(buffer);

    return 0;
  }
  
  else
  {
    Serial.print("FAILED(");
    Serial.print(bluetooth.available());
    Serial.println(")");
    
    return 1;
  }
}

// initializes BT connection
void initBT()
{
  int error = 0;
  
  Serial.print(".: Bluetooth Bee V2 Chat\n\n");
  Serial.print("Initialization started\n\n");
  
  Serial.print("Setting serial port parameters ");
  bluetooth.print("AT+UART=9600,1,2\r\n");
  error += checkOK();

  Serial.print("SPP initialization ");
  bluetooth.print("AT+INIT\r\n");
  delay(2000);
  error += checkOK();
  
  Serial.print("Setting slave mode ");
  bluetooth.print("AT+ROLE=0\r\n");
  delay(1000); 
  error += checkOK();
  
  Serial.print("Setting default IAC ");
  bluetooth.print("AT+IAC=9E8B33\r\n");
  error += checkOK();

  Serial.print("Accepting all device classes ");
  bluetooth.print("AT+CLASS=0\r\n");
  delay(2000);
  error += checkOK();
  
  Serial.print("Inquire mode: RSSI, max 9, timeout 48 ");
  bluetooth.print("AT+INQM=1,9,48\r\n");
  error += checkOK();
  
  Serial.print("Inquiring... ");
  bluetooth.print("AT+INQ\r\n");
  delay(3000);
  error += checkOK();
  
  if( !error )
    Serial.print("\nInitialization succeeded, you can chat now!\n\n");
  else
    Serial.print("\nInitialization failed\n\n");
}

void setup()
{
  pinMode(bluetoothRx, INPUT);
  pinMode(bluetoothTx, OUTPUT);
  
  Serial.begin(38400);
  bluetooth.begin(9600);  

  initBT();
}

void loop()
{ 
  int data;
  
  if( bluetooth.available() > 0 )
  {
    data = bluetooth.read();
    if( data > 0 ) Serial.print((char)data);
  }

  if( Serial.available() > 0 )
  {
    data = Serial.read();
    if( data > 0 )
    {
      bluetooth.print((char)data);
      Serial.print((char)data);
    }
  }
}

Result in serial monitor:

.: Bluetooth Bee V2 Chat

Initialization started

Setting serial port parameters OK
SPP initialization OK 
Setting slave mode OK 
Setting default IAC OK 
Accepting all device classes OK 
Inquire mode: RSSI, max 9, timeout 48 OK 
Inquiring... OK 

Initialization succeded, you can chat now!

I can connect with another bluetooth device, but I still can't send/receive data.

I managed to be able to send/receive data.

I had to set jumpers to XBee mode on my Xbee shield.

However, the trasfer makes data dirty.

I test connecting BT Bee to my pc and communicating via a serial terminal.

Here's a picture.

And here's the latest code:

#include <SoftwareSerial.h>

const int bluetoothRx = 2;
const int bluetoothTx = 3;

SoftwareSerial bluetooth(bluetoothRx, bluetoothTx);

// checks the result of AT commands
int checkOK()
{
  if( bluetooth.available() > 0 )
  {
    char buffer[18];
    int index = 0;
    delay(100); // let the buffer fill up
    int numChar = bluetooth.available();
    if( numChar > 15 ) numChar = 15;
    while( numChar-- ) buffer[index++] = bluetooth.read();
    
    Serial.print("OK ");
    Serial.println(buffer);

    return 0;
  }
  
  else
  {
    Serial.print("FAILED(");
    Serial.print(bluetooth.available());
    Serial.println(")");
    
    return 1;
  }
}

// initializes BT connection
void initBT()
{
  int error = 0;
  
  Serial.print(".: Bluetooth Bee V2 Communication Test\n\n");
  Serial.print("Initialization started\n\n");
  
  Serial.print("Setting serial port parameters ");
  bluetooth.print("AT+UART=9600,1,2\r\n");
  error += checkOK();

  Serial.print("SPP initialization ");
  bluetooth.print("AT+INIT\r\n");
  delay(2000);
  error += checkOK();
  
  Serial.print("Setting slave mode ");
  bluetooth.print("AT+ROLE=0\r\n");
  delay(1000); 
  error += checkOK();
  
  Serial.print("Setting default IAC ");
  bluetooth.print("AT+IAC=9E8B33\r\n");
  error += checkOK();

  Serial.print("Accepting all device classes ");
  bluetooth.print("AT+CLASS=0\r\n");
  delay(2000);
  error += checkOK();
  
  Serial.print("Inquire mode: RSSI, max 9, timeout 48 ");
  bluetooth.print("AT+INQM=1,9,48\r\n");
  error += checkOK();
  
  Serial.print("Inquiring... ");
  bluetooth.print("AT+INQ\r\n");
  delay(3000);
  error += checkOK();
  
  if( !error ) {
    Serial.print("\nInitialization succeeded!\n");
    Serial.print("-------------------------\n\n");
  }
  else
    Serial.print("\nInitialization failed\n\n");
  
  Serial.flush();
  bluetooth.flush();
  delay(1000);
}

void setup()
{
  pinMode(bluetoothRx, INPUT);
  pinMode(bluetoothTx, OUTPUT);
  
  Serial.begin(9600);
  bluetooth.begin(9600);  

  initBT();
}

void loop()
{ 
  int data;
  
  if( bluetooth.available() > 0 )
  {
    data = bluetooth.read();
    if( data > 0 )
    {
      bluetooth.print((char)data);
      Serial.print((char)data);
    }
  }

  if( Serial.available() > 0 )
  {
    data = Serial.read();
    if( data > 0 )
    {
      bluetooth.print((char)data);
      Serial.print((char)data);
    }
  }
}

How can I solve this?

    delay(100); // let the buffer fill up

Rubbish.

The bluetooth device should respond with something like "OK\r\n" or "ERROR\r\n". You should read the data until the end of packet marker (\r or \n, whichever is last) arrives, not just whatever data crept in while you were snoozing.

You mean Serial.println(buffer) in checkOK() could be the reason why my serial buffer gets dirty while receiving data, later in loop()?

Anyway I'll try tomorrow, it's bed time here.

Rubbish.

Remember the cookbook I mentioned some posts ago?

Do you think people on the irc chan who love it suggested me wrong?

Could you please suggest me a good cookbook/starter book/book for Arduino, then?

Hey mate,

im trying to send/receive data and ive used your code, as a test ive made a new sketched and copied the entire latest code however in the serial monitor i get failed to all initalizations! :expressionless:
BTB is in Normal Mode
Xbee is in XBEE and then USB and RUN mode (using tablet bluetooth serial app then usb to laptop to test using Hyperterminal)

Still all fails...any idea? what was your specific setup?

Seems i cant program the BTB using code, only doing AT commands in serial monitor when the IC is out of the socket - a way i dont like to do

Cooper.