Bluetooth and Xbee working together

Hey

Im doing a school project where i have an arduino mega, with a bees shield (Bees Shield - Seeed Wiki). On this shield i have the bluetooth bee (Bluetooth Bee - Seeed Wiki) and a XBee 2.5. They are plugged directly onto the bees shield, with no other wiring.

I have gotten the xbee to work on 9600 baud rate, and the bluetooth to work on a 38400 baud rate, but thats seperatly. How do get them to work, so that i can connect to the bluetooth with my phone, and then have the xbee send commands?

I have written some code, where i tried, but can you help me out to see what the problem might be? When i use this code none of them work, i cant see it on bluetooth, and the xbee won't receive commands.

//long DATARATE = 38400;
const int ledPin = 13; // the pin that the LED is attached to
int incomingByte1;      // a variable to read incoming serial data into
int incomingByte2;

void setup()
{
  Serial1.begin(9600);
  Serial2.begin(38400);
  
  pinMode(ledPin, OUTPUT);
  
  //------BLUETOOTH SETUP-------
  Serial2.print("\r\n+STWMOD=0\r\n");     // set to slave
  delay(1000);
  Serial2.print("\r\n+STNA=HomeAutoDroid\r\n");     // DSC = digital setting circles
  delay(1000);
  Serial2.print("\r\n+STAUTO=0\r\n");     // don't permit auto-connect
  delay(1000);
  Serial2.print("\r\n+STOAUT=1\r\n");     // existing default
  delay(1000);
  Serial2.print("\r\n +STPIN=0000\r\n");  // existing default
  delay(2000);  // required

  // initiate BTBee connection
  Serial2.print("\r\n+INQ=1\r\n");
  delay(2000);   // wait for pairing
  //----------------------------
}

void loop()
{
  if (Serial2.available() > 0) {
    incomingByte2 = Serial.read();
    
    if (incomingByte2 == 'X') {
      Serial.println("-----------Menu------------");
      Serial.println(" T - TEST    ");
      Serial.println(" T - TEST   ");
      Serial.println(" H - TEST        ");
      Serial.println(" D - TEST        ");
      Serial.println("---------------------------");
    }
    if (incomingByte2 == 'H') {
      digitalWrite(ledPin, HIGH);
      Serial.println("LED ON");
    } 
    if (incomingByte2 == 'L') {
      digitalWrite(ledPin, LOW);
      Serial.println("LED OFF");
    }
    
  }
  
  if (Serial1.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte1 = Serial.read();
    
    
    if (incomingByte1 == 'X') {
      Serial.println("-----------Menu------------");
      Serial.println(" T - TEST    ");
      Serial.println(" T - TEST   ");
      Serial.println(" H - TEST        ");
      Serial.println(" D - TEST        ");
      Serial.println("---------------------------");
    }
    if (incomingByte1 == 'H') {
      digitalWrite(ledPin, HIGH);
      Serial.println("LED ON");
    } 
    if (incomingByte1 == 'L') {
      digitalWrite(ledPin, LOW);
      Serial.println("LED OFF");
    }

  }
  
   
  }

First, what is the XBee talking to?

PaulS:
First, what is the XBee talking to?

Well i have the Arduino Mega with the bluetooth bee and the xbee.
And then i have a 2nd Arduino with an xbee shield on it and an xbee of course.

So it goes like this: Phone (Bluetooth) -> Arduino Mega (Bluetooth) -> same Arduino Mega (XBee) -> Arduino Uno (XBee).

Are the jumpers on that board set correctly so that the bluetooth device IS connected to Serial2? Are the jumpers on that board set correctly so that the XBee is connected to Serial1? That board is not designed for a Mega, so you must be running wires to the Serial1 and Serial2 pins.

What serial output are you seeing?

PaulS:
Are the jumpers on that board set correctly so that the bluetooth device IS connected to Serial2? Are the jumpers on that board set correctly so that the XBee is connected to Serial1? That board is not designed for a Mega, so you must be running wires to the Serial1 and Serial2 pins.

What serial output are you seeing?

i tried to run 2 wires from the 2 holes where it says "Bee 2 RX and TX", i ran them to the TX1 and RX2.

I just took a picture of the arduino mega with the bees shield, bluetooth bee and xbee.

Black wire goes to TX, and red wire goes to RX

http://boinq.net/amega.jpg

I just took a picture of the arduino mega with the bees shield, bluetooth bee and xbee.

I can't see that picture while at work. When I get home, I'll look at it.

PaulS:

I just took a picture of the arduino mega with the bees shield, bluetooth bee and xbee.

I can't see that picture while at work. When I get home, I'll look at it.

sounds good, i appreciate it :slight_smile:

I can't see through the board. I can't see where the other ends of the black and red wires go. There should be 4 of them, since the blue tooth and the XBee are bidirectional devices.

PaulS:
I can't see through the board. I can't see where the other ends of the black and red wires go. There should be 4 of them, since the blue tooth and the XBee are bidirectional devices.

The wires are coming from the 2 solderings just above the atmega/usb switch on the shield, from the RX and TX for bee2

The jumpers are for bee1 are in port 11 and 12.

The wires are coming from the 2 solderings just above the atmega/usb switch on the shield, from the RX and TX for bee2

I can't see the picture again. There need to be 4 wires - two for the bluetooth device and two for the XBee. The 2 from the blue tooth device go to TXn/RXn, and the two from the XBee go to TXm/RXm. Use Serialn to talk to the bluetooth, and Serialm to talk to the XBee.

PaulS:

The wires are coming from the 2 solderings just above the atmega/usb switch on the shield, from the RX and TX for bee2

I can't see the picture again. There need to be 4 wires - two for the bluetooth device and two for the XBee. The 2 from the blue tooth device go to TXn/RXn, and the two from the XBee go to TXm/RXm. Use Serialn to talk to the bluetooth, and Serialm to talk to the XBee.

Serialm and serialn? like the serial1 and serial2 in my code?
Can you tell me what is needed in the code to have multiple serial in- and outputs? should i define tx1, rx1, tx2 and rx2 to the ports they are in?

Serialm and serialn? like the serial1 and serial2 in my code?

Yes.

Can you tell me what is needed in the code to have multiple serial in- and outputs? should i define tx1, rx1, tx2 and rx2 to the ports they are in?

Yes, no, and maybe. As of the last picture you posted, you don't have the hardware connected properly, so no amount of software diddling is going to work.

PaulS:

Serialm and serialn? like the serial1 and serial2 in my code?

Yes.

Can you tell me what is needed in the code to have multiple serial in- and outputs? should i define tx1, rx1, tx2 and rx2 to the ports they are in?

Yes, no, and maybe. As of the last picture you posted, you don’t have the hardware connected properly, so no amount of software diddling is going to work.

Now that the bluetooth bee is wired to rx2 and tx2. I need to write the code, so that it can read bluetooth bee on 38400 baudrate and the xbee on baudrate 9600.

I need to send these codes through the serial on baudrate 38400

//------BLUETOOTH SETUP-------
  Serial2.print("\r\n+STWMOD=0\r\n");     // set to slave
  delay(1000);
  Serial2.print("\r\n+STNA=HomeAutoDroid\r\n");     // DSC = digital setting circles
  delay(1000);
  Serial2.print("\r\n+STAUTO=0\r\n");     // don't permit auto-connect
  delay(1000);
  Serial2.print("\r\n+STOAUT=1\r\n");     // existing default
  delay(1000);
  Serial2.print("\r\n +STPIN=0000\r\n");  // existing default
  delay(2000);  // required

  // initiate BTBee connection
  Serial2.print("\r\n+INQ=1\r\n");
  delay(2000);   // wait for pairing
  //----------------------------

i made some code where i can get it to connect to my bluetooth bee, but it wont read my commands:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(51, 50); // RX, TX

char inChar = 0;
int  LED = 13; 

void setup()  
{
  Serial.begin(9600);
  Serial.println("Goodnight moon!");

//------BLUETOOTH SETUP-------
  Serial.print("\r\n+STWMOD=0\r\n");     // set to slave
  delay(1000);
  Serial.print("\r\n+STNA=HomeAutoDroid\r\n");     // DSC = digital setting circles
  delay(1000);
  Serial.print("\r\n+STAUTO=0\r\n");     // don't permit auto-connect
  delay(1000);
  Serial.print("\r\n+STOAUT=1\r\n");     // existing default
  delay(1000);
  Serial.print("\r\n +STPIN=0000\r\n");  // existing default
  delay(2000);  // required

  // initiate BTBee connection
  Serial.print("\r\n+INQ=1\r\n");
  delay(2000);   // wait for pairing
  //----------------------------

  // set the data rate fovr the SoftwareSerial port
  mySerial.begin(38400);
  
  pinMode(LED, OUTPUT);
}

void loop() // run over and over
{
  if (mySerial.available()) {
    mySerial.write(Serial.read());
    inChar = mySerial.read();

    if (inChar == 'a') {
      Serial.print("connected");  // test return connection
    }
    
    if (inChar == 'h') {
      digitalWrite(LED, HIGH);   // on
    }

    if (inChar == 'l') {
      digitalWrite(LED, LOW);    // off
    }
    
  }
  if (Serial.available()) {
    
  }
}
SoftwareSerial mySerial(51, 50); // RX, TX

Why? You've got a Mega with 4 hardware serial ports.

PaulS:

SoftwareSerial mySerial(51, 50); // RX, TX

Why? You’ve got a Mega with 4 hardware serial ports.

I have been trying a million different things and NOTHING works :frowning:

here’s my latest attempt:

#include <SoftwareSerial.h>

//SoftwareSerial mySerial(51, 50); // RX, TX

char inChar = 0;
int  LED = 13; 

void setup()  
{
  Serial.begin(9600);
  Serial3.begin(38400);
  Serial.println("Goodnight moon!");

//------BLUETOOTH SETUP-------
  Serial.print("\r\n+STWMOD=0\r\n");     // set to slave
  delay(1000);
  Serial.print("\r\n+STNA=HomeAutoDroid\r\n");     // DSC = digital setting circles
  delay(1000);
  Serial.print("\r\n+STAUTO=0\r\n");     // don't permit auto-connect
  delay(1000);
  Serial.print("\r\n+STOAUT=1\r\n");     // existing default
  delay(1000);
  Serial.print("\r\n +STPIN=0000\r\n");  // existing default
  delay(2000);  // required

  // initiate BTBee connection
  Serial.print("\r\n+INQ=1\r\n");
  delay(2000);   // wait for pairing
  //----------------------------

  // set the data rate fovr the SoftwareSerial port
  //mySerial.begin(38400);
  
  pinMode(LED, OUTPUT);
}

void loop() // run over and over
{
  /*if (mySerial.available()) {
    mySerial.write(Serial.read());
    inChar = mySerial.read();

    if (inChar == 'a') {
      Serial.print("connected");  // test return connection
    }
    
    if (inChar == 'h') {
      digitalWrite(LED, HIGH);   // on
    }

    if (inChar == 'l') {
      digitalWrite(LED, LOW);    // off
    }
    
  }
  if (Serial.available()) {
    
  }*/
}
//------BLUETOOTH SETUP-------
  Serial.print("\r\n+STWMOD=0\r\n");     // set to slave
  delay(1000);
  Serial.print("\r\n+STNA=HomeAutoDroid\r\n");     // DSC = digital setting circles
  delay(1000);
  Serial.print("\r\n+STAUTO=0\r\n");     // don't permit auto-connect
  delay(1000);
  Serial.print("\r\n+STOAUT=1\r\n");     // existing default
  delay(1000);
  Serial.print("\r\n +STPIN=0000\r\n");  // existing default
  delay(2000);  // required

What is the Serial Monitor supposed to do with this crap? Why are you not sending this to the bluetooth device on Serial3 (if the device is connected to TX3/RX3)?