General

Xbee Shield + BTBv2 - HOW TO PROGRAM AT COMMANDS IN SKETCH?

userHead cooper 2013-07-19 22:02:09 11112 Views19 Replies
Hey guys,

Ive been looking at code all over the internet on sending commands to the BTB.
Ive got a UNO R3 -> Xbee Sheild -> BTBv2 and ive used code from...
http://forum.arduino.cc/index.php?topic=172580.0

Such code like:

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();

I just cannot understand how to program (at commands) to the BTB within a sketch.
Been working on this for week+ and no progress.!

Please help! TIA!!

Cooper.
2013-08-06 22:16:54 Hey Cooper,

Is that working? any output?

userHeadPic Jose
2013-08-06 22:16:54 Hey Cooper,

Is that working? any output?

userHeadPic Jose
2013-07-30 02:22:04 Does this code work for you ?
Code: Select all
#include <SoftwareSerial.h>
int led = 13;

SoftwareSerial mySerial(10, 11); // RX, TX

void setup()  
{
  Serial.begin(38400);
  pinMode(led, OUTPUT);   
  while (!Serial) {
    ; 
  }
  mySerial.begin(38400);
  mySerial.println("Hello, world?");
}

void loop() 
{
  Serial.println("AT");

  while(Serial.available()){
    char var;
    var = Serial.read();
    if(var == 'O'){
      var = Serial.read();
      if(var == 'K') {
        digitalWrite(led, HIGH);
        mySerial.println("ok le"); // output values of ATcommands
        delay(400);
      }
    }
    else digitalWrite(led,LOW);
  }
  delay(1000);
}
It should blink the LED briefly when OK is received, or output something on pins 10, 11 if you have a serial to USB converter and a few wires.
userHeadPic Jose
2013-07-30 02:22:04 Does this code work for you ?
Code: Select all
#include <SoftwareSerial.h>
int led = 13;

SoftwareSerial mySerial(10, 11); // RX, TX

void setup()  
{
  Serial.begin(38400);
  pinMode(led, OUTPUT);   
  while (!Serial) {
    ; 
  }
  mySerial.begin(38400);
  mySerial.println("Hello, world?");
}

void loop() 
{
  Serial.println("AT");

  while(Serial.available()){
    char var;
    var = Serial.read();
    if(var == 'O'){
      var = Serial.read();
      if(var == 'K') {
        digitalWrite(led, HIGH);
        mySerial.println("ok le"); // output values of ATcommands
        delay(400);
      }
    }
    else digitalWrite(led,LOW);
  }
  delay(1000);
}
It should blink the LED briefly when OK is received, or output something on pins 10, 11 if you have a serial to USB converter and a few wires.
userHeadPic Jose
2013-07-28 23:44:35 Doesnt seem to work, would you have sample code that you know works that i can try? userHeadPic cooper
2013-07-28 23:44:35 Doesnt seem to work, would you have sample code that you know works that i can try? userHeadPic cooper
2013-07-27 06:42:19 Ok thanks Jose, ill give it a go and let you know asap :) cheers userHeadPic cooper
2013-07-27 06:42:19 Ok thanks Jose, ill give it a go and let you know asap :) cheers userHeadPic cooper
2013-07-26 04:07:33 Howdy,

I see you are using the same serial port for both software serial and hardware serial. You can just use one of these. I suppose you are planning on changing it on the future, but I don't the reason that you would use it right now on the same port. This could be causing issue with the commands.

For example, initBT() is sending all in one line commands, and debugging text:
  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");

My suggestion is that you delete all the debugging prints, and use your LED for some status information. You could do blinks with different speed for different messages, or add other LED color to other pins.

I think something like:
Serial.println(yourATcommand);
Should be enough, then repeat per at command your application requires, with some delay.

userHeadPic Jose
2013-07-26 04:07:33 Howdy,

I see you are using the same serial port for both software serial and hardware serial. You can just use one of these. I suppose you are planning on changing it on the future, but I don't the reason that you would use it right now on the same port. This could be causing issue with the commands.

For example, initBT() is sending all in one line commands, and debugging text:
  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");

My suggestion is that you delete all the debugging prints, and use your LED for some status information. You could do blinks with different speed for different messages, or add other LED color to other pins.

I think something like:
Serial.println(yourATcommand);
Should be enough, then repeat per at command your application requires, with some delay.

userHeadPic Jose
2013-07-25 17:12:51 Hey mate,

Ye the sheild does have the 2 switches (USB/XBEE  RUN/PRGM).
Code: Select all
//----------------------------------------------------------//
// Michael Cooper
// 6/7/2013
//
// This code lets you to turn on relays
// using words from a serial.read and blueterm app
// which can be then used for the android to send to the arduino

//Includes ------------------------------------------------//
#include <SoftwareSerial.h>

//Definitions ---------------------------------------------//
#define LED_PIN 13
#define BOOT_PIN 12
#define LOCK_PIN 11
#define UNLOCK_PIN 10
#define ENGINE_PIN 9
#define RxD 0
#define TxD 1

//Initialisations
SoftwareSerial bluetooth(RxD,TxD);


//Functions
//Setup Functions-----------------------------------------//
void setup()
{
  //Set pin I/O
  pinMode(LED_PIN, OUTPUT);
  pinMode(BOOT_PIN, OUTPUT);
  pinMode(LOCK_PIN, OUTPUT);
  pinMode(UNLOCK_PIN,OUTPUT);
  pinMode(ENGINE_PIN, OUTPUT);
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  
  //Set baud rate
  Serial.begin(9600);
  bluetooth.begin(9600);  
  Serial.println("Listening - awaiting instructions (BOOT, LOCK, UNLOCK, ENGINE, LED)\n");   //initial serial prompt text

  initBT();
}

//Main --------------------------------------------------// 
void loop() 
{ 
  char inSerial[6];   //inilise array
  int i=0; 
  delay(3000);

  if (Serial.available() > 0) //if characters are available to be read
  {             
    while (Serial.available() > 0) {
      inSerial[i]=Serial.read(); //read data  
      i++;      
    }
    inSerial[i]='\0';        //terminate string
    Check_Protocol(inSerial); //run commanding function
  }    
};

//Checks results 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;
  }
}

//Bluetooth Setup------------------------------------------// 
//code from http://forum.arduino.cc/index.php?topic=172580.0
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);
}


//Car Commands---------------------------------------------// 
void Check_Protocol(char inStr[])
{   
  Serial.print("Command: ");
  Serial.println(inStr);

  if(!strcmp(inStr,"BOOT")) 
  {
    Serial.print("Boot relay activated\n");
    digitalWrite(BOOT_PIN,HIGH);
    delay(500);
    digitalWrite(BOOT_PIN,LOW);     
    delay(500); 
    Serial.print("Boot relay de-activated\n");
  }

  else if(!strcmp(inStr,"LOCK")) 
  {
    Serial.print("Lock relay activated\n");
    digitalWrite(LOCK_PIN,HIGH);
    delay(1250);
    digitalWrite(LOCK_PIN,LOW);      
    delay(500);
    Serial.print("Lock relay de-activated\n");
  }

  else if(!strcmp(inStr,"UNLOCK")) 
  {
    Serial.print("Unlock relay activated\n");
    digitalWrite(UNLOCK_PIN,HIGH);
    delay(1250);
    digitalWrite(UNLOCK_PIN,LOW);  
    delay(500);    
    Serial.print("Unlock relay de-activated\n");
  }
  else if(!strcmp(inStr,"ENGINE")) 
  {
    Serial.print("Engine relay activated\n");
    digitalWrite(ENGINE_PIN,HIGH);
    delay(3500);
    digitalWrite(ENGINE_PIN,LOW);      
    delay(500);
    Serial.print("Engine relay de-activated\n");
  }     
  else if(!strcmp(inStr,"LED")) 
  {
    for (int thisPin = 0; thisPin < 5; thisPin++)  //run 5 times
    { 
      Serial.print("Test LED activated\n");
      digitalWrite(LED_PIN,HIGH);
      delay(700);
      Serial.print("Test LED de-activated\n");
      digitalWrite(LED_PIN,LOW);
      delay(700); 
    }
  }
  else
  {
    Serial.print("Invalid Command\n");
  }
}   


userHeadPic cooper
2013-07-25 17:12:51 Hey mate,

Ye the sheild does have the 2 switches (USB/XBEE  RUN/PRGM).
Code: Select all
//----------------------------------------------------------//
// Michael Cooper
// 6/7/2013
//
// This code lets you to turn on relays
// using words from a serial.read and blueterm app
// which can be then used for the android to send to the arduino

//Includes ------------------------------------------------//
#include <SoftwareSerial.h>

//Definitions ---------------------------------------------//
#define LED_PIN 13
#define BOOT_PIN 12
#define LOCK_PIN 11
#define UNLOCK_PIN 10
#define ENGINE_PIN 9
#define RxD 0
#define TxD 1

//Initialisations
SoftwareSerial bluetooth(RxD,TxD);


//Functions
//Setup Functions-----------------------------------------//
void setup()
{
  //Set pin I/O
  pinMode(LED_PIN, OUTPUT);
  pinMode(BOOT_PIN, OUTPUT);
  pinMode(LOCK_PIN, OUTPUT);
  pinMode(UNLOCK_PIN,OUTPUT);
  pinMode(ENGINE_PIN, OUTPUT);
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  
  //Set baud rate
  Serial.begin(9600);
  bluetooth.begin(9600);  
  Serial.println("Listening - awaiting instructions (BOOT, LOCK, UNLOCK, ENGINE, LED)\n");   //initial serial prompt text

  initBT();
}

//Main --------------------------------------------------// 
void loop() 
{ 
  char inSerial[6];   //inilise array
  int i=0; 
  delay(3000);

  if (Serial.available() > 0) //if characters are available to be read
  {             
    while (Serial.available() > 0) {
      inSerial[i]=Serial.read(); //read data  
      i++;      
    }
    inSerial[i]='\0';        //terminate string
    Check_Protocol(inSerial); //run commanding function
  }    
};

//Checks results 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;
  }
}

//Bluetooth Setup------------------------------------------// 
//code from http://forum.arduino.cc/index.php?topic=172580.0
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);
}


//Car Commands---------------------------------------------// 
void Check_Protocol(char inStr[])
{   
  Serial.print("Command: ");
  Serial.println(inStr);

  if(!strcmp(inStr,"BOOT")) 
  {
    Serial.print("Boot relay activated\n");
    digitalWrite(BOOT_PIN,HIGH);
    delay(500);
    digitalWrite(BOOT_PIN,LOW);     
    delay(500); 
    Serial.print("Boot relay de-activated\n");
  }

  else if(!strcmp(inStr,"LOCK")) 
  {
    Serial.print("Lock relay activated\n");
    digitalWrite(LOCK_PIN,HIGH);
    delay(1250);
    digitalWrite(LOCK_PIN,LOW);      
    delay(500);
    Serial.print("Lock relay de-activated\n");
  }

  else if(!strcmp(inStr,"UNLOCK")) 
  {
    Serial.print("Unlock relay activated\n");
    digitalWrite(UNLOCK_PIN,HIGH);
    delay(1250);
    digitalWrite(UNLOCK_PIN,LOW);  
    delay(500);    
    Serial.print("Unlock relay de-activated\n");
  }
  else if(!strcmp(inStr,"ENGINE")) 
  {
    Serial.print("Engine relay activated\n");
    digitalWrite(ENGINE_PIN,HIGH);
    delay(3500);
    digitalWrite(ENGINE_PIN,LOW);      
    delay(500);
    Serial.print("Engine relay de-activated\n");
  }     
  else if(!strcmp(inStr,"LED")) 
  {
    for (int thisPin = 0; thisPin < 5; thisPin++)  //run 5 times
    { 
      Serial.print("Test LED activated\n");
      digitalWrite(LED_PIN,HIGH);
      delay(700);
      Serial.print("Test LED de-activated\n");
      digitalWrite(LED_PIN,LOW);
      delay(700); 
    }
  }
  else
  {
    Serial.print("Invalid Command\n");
  }
}   


userHeadPic cooper
2013-07-25 16:44:36 Hi!

I think I had too less coffee when I first read that, I thought you were using xbee and BTB on uno at the same time.. ok, well got it now.

Are you using the latest revision of the Xbee shield? the one with 2 different switches?

If so, you should give it a go with AT commands first, to get an idea how the environment works. That way you can parse the OK responses or errors from the AT commands on your sketch. If your shield does not have that, you will need a USB to serial converter to do this approach.

Can you paste your code so far? I can give it a try and see if i can help

Cheers
userHeadPic Jose
2013-07-25 16:44:36 Hi!

I think I had too less coffee when I first read that, I thought you were using xbee and BTB on uno at the same time.. ok, well got it now.

Are you using the latest revision of the Xbee shield? the one with 2 different switches?

If so, you should give it a go with AT commands first, to get an idea how the environment works. That way you can parse the OK responses or errors from the AT commands on your sketch. If your shield does not have that, you will need a USB to serial converter to do this approach.

Can you paste your code so far? I can give it a try and see if i can help

Cheers
userHeadPic Jose
2013-07-25 04:40:22 Sure mate,

Not sure what to really explain as the Uno R3 is the unit im using, the Xbee shield is attached to that and attached to the Xbee is the BTBV2.

Everything is connected, the xbee is connected direcly to the BTBv2 as the shield is made for it, just push the BTB onto the sheild and push that whole unit onto the R3.

Cheers.
userHeadPic cooper
2013-07-25 04:40:22 Sure mate,

Not sure what to really explain as the Uno R3 is the unit im using, the Xbee shield is attached to that and attached to the Xbee is the BTBV2.

Everything is connected, the xbee is connected direcly to the BTBv2 as the shield is made for it, just push the BTB onto the sheild and push that whole unit onto the R3.

Cheers.
userHeadPic cooper
2013-07-20 01:09:12 Hi Cooper,

Can you explain your hardware set up?
Is the xbee directly connected to the Bluetooth Bee v2?

Cheers^^
userHeadPic Jose
2013-07-20 01:09:12 Hi Cooper,

Can you explain your hardware set up?
Is the xbee directly connected to the Bluetooth Bee v2?

Cheers^^
userHeadPic Jose
2013-07-19 22:02:09 Hey guys,

Ive been looking at code all over the internet on sending commands to the BTB.
Ive got a UNO R3 -> Xbee Sheild -> BTBv2 and ive used code from...
http://forum.arduino.cc/index.php?topic=172580.0

Such code like:

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();

I just cannot understand how to program (at commands) to the BTB within a sketch.
Been working on this for week+ and no progress.!

Please help! TIA!!

Cooper.
userHeadPic cooper