Arduino Mega and Xbee serial communication

Hi,

I am using a Arduino Mega 2560 and a 2 Xbee series1. 1 Xbee is connected to the arduino mega using a breadboard with RX and TX pins (19, 18). The other Xbee is connected to the laptop directly. The problem is that i can send data from the Xbee connected to the arduino however, i cannot receive any data.

The code im using works with arduino uno with pins (2, 3)

// We'll use SoftwareSerial to communicate with the XBee:
#include <SoftwareSerial.h>
// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial XBee(19, 18); // RX, TX

void setup()
{
  // Set up both ports at 9600 baud. This value is most important
  // for the XBee. Make sure the baud rate matches the config
  // setting of your XBee.
  XBee.begin(9600);
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available())
  { // If data comes in from serial monitor, send it out to XBee
    XBee.write(Serial.read());
  }
  if (XBee.available())
  { // If data comes in from XBee, send it out to serial monitor
    Serial.write(XBee.read());
  }
}

Any help would be appreciated.

Thanks

The Arduino Mega 2560 has hardware serial at 18 and 19 and at 16 and 17 and at 14 and 15. That is one of the main advantages of the Mega.

So what should i do to receive the data on the serial of arduino.

Your current sketch is using the SoftwareSerial library which is conflicting with the hardware serial.

Thanks solved the problem !

shayaan123:
Hi,

I am using a Arduino Mega 2560 and a 2 Xbee series1. 1 Xbee is connected to the arduino mega using a breadboard with RX and TX pins (19, 18). The other Xbee is connected to the laptop directly. The problem is that i can send data from the Xbee connected to the arduino however, i cannot receive any data.

The code im using works with arduino uno with pins (2, 3)

// We'll use SoftwareSerial to communicate with the XBee:

#include <SoftwareSerial.h>
// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial XBee(19, 18); // RX, TX

void setup()
{
  // Set up both ports at 9600 baud. This value is most important
  // for the XBee. Make sure the baud rate matches the config
  // setting of your XBee.
  XBee.begin(9600);
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available())
  { // If data comes in from serial monitor, send it out to XBee
    XBee.write(Serial.read());
  }
  if (XBee.available())
  { // If data comes in from XBee, send it out to serial monitor
    Serial.write(XBee.read());
  }
}




Any help would be appreciated. 

Thanks

How did you solve this?
I am also using an Arduino Mega with pin 18 and 19. Running the exact example code you are using.
But I can see nothing in my ArduinoIDE serial monitor.

hieifcb:
How did you solve this?
I am also using an Arduino Mega with pin 18 and 19. Running the exact example code you are using.
But I can see nothing in my ArduinoIDE serial monitor.

On a Mega pins 18 and 19 are Serial1 so use that in the places where the code uses XBee. For example
Serial1.begin(9600) in place of XBee.begin(9600)

...R

hieifcb:
How did you solve this?
I am also using an Arduino Mega with pin 18 and 19. Running the exact example code you are using.
But I can see nothing in my ArduinoIDE serial monitor.

Instead of using pins 18 and 19, i used pins 10 and 12 for RX and TX respectively. Try changing the RX and TX pins to something similar.
Also make sure your pin connections are proper and fixed as most of my problem lied with the connections of the wire.

Regards,
Shayaan

shayaan123:
i used pins 10 and 12 for RX and TX respectively

They are not HardwareSerial pins on a Mega.

...R

Could you post your resulting sketch to another newbie, I thought I was following along, but couldn't get functioning.

I'm using a Mega with XBee Series 2 on a Sparkfun shield. Aren't some jumpers needed??

I, also, have a general question: Is there a way to run 2 sketches from the same PC showing a serial monitor for each session?

Of all the digging, this thread seems to be the most straight forward>

Thanks

on a Sparkfun shield.

An ethernet shield? A GSM shield? A link would be far more useful than this hand-waving.

Aren't some jumpers needed??

Your question mark key needs some lubrication. It is sticking!!!!!!!!!!!!!

Is there a way to run 2 sketches from the same PC showing a serial monitor for each session?

The PC doesn't run sketches, so, no.

fordbuff39:
I, also, have a general question: Is there a way to run 2 sketches from the same PC showing a serial monitor for each session?

If you mean using 2 different Arduinos the answer is YES.

I thought I was following along, but couldn't get functioning.

Post the code you have tried.

And please use the code button </>so your code looks like thisand is easy to copy to a text editor

...R

The code below is my starting point, hopefully, posted correctly. Should have included is the post: One Ardunio is configured as Admin API 2, a second Ardunio is Router API 2, and third is USB on a Digi XBIB-U as Router API 2.

I want to get the Ardunio(s) going first, the Admin is going into a remote control box to operate the other Ardunio Router in a robot. At some point, I'll get the USB Router involved.

The shield is a Sparkfun XBee shield.

And, yes two Ardunios on the same PC. The intent is to get the conversation going between the Ardunios before putting into the controller and robot.

// We'll use SoftwareSerial to communicate with the XBee:
#include <SoftwareSerial.h>
// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial XBee(19, 18); // RX, TX

void setup()
{
  // Set up both ports at 9600 baud. This value is most important
  // for the XBee. Make sure the baud rate matches the config
  // setting of your XBee.
  XBee.begin(9600);
  Serial1.begin(9600);
}

void loop()
{
  if (Serial1.available())
  { // If data comes in from serial monitor, send it out to XBee
    XBee.write(Serial1.read());
  }
  if (XBee.available())
  { // If data comes in from XBee, send it out to serial monitor
    Serial1.write(XBee.read());
  }
}

Thanks

The code below is my starting point

On the Mega? Why are you using SoftwareSerial to talk to the XBee when the Mega has 4 hardware serial ports?

fordbuff39:
And, yes two Ardunios on the same PC. The intent is to get the conversation going between the Ardunios before putting into the controller and robot.

If you open 2 instances of the Arduino IDE (open them from the operating system) each one can work with one of the Arduinos.

...R

Why are you using SoftwareSerial on a Mega? Correct me if I'm wrong, but doesn't that have multiple rx/tx pins?

Well, I did get my two sessions going and included the XBee-Ardunio library. Selected the Series_2_Tx and Rx as the sketch starting point, included below.

Changed Serial to Serial1, but serial.print for debugging.

Not connecting,

The Tx uses pin5 as the payload, I just want to send strings to the Rx.

Thanks again.

Series_2_Tx:

/**
 * Copyright (c) 2009 Andrew Rapp. All rights reserved.
 *
 * This file is part of XBee-Arduino.
 *
 */

#include <XBee.h>

/*
This example is for Series 2 XBee
 Sends a ZB TX request with the value of analogRead(pin5) and checks the status response for success
*/

// create the XBee object
XBee xbee = XBee();

uint8_t payload[] = { 0, 0 };

// SH + SL Address of receiving XBee
XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x403e0f30);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();

int pin5 = 0;

int statusLed = 13;
int errorLed = 13;

void flashLed(int pin, int times, int wait) {

  for (int i = 0; i < times; i++) {
    digitalWrite(pin, HIGH);
    delay(wait);
    digitalWrite(pin, LOW);

    if (i + 1 < times) {
      delay(wait);
    }
  }
}

void setup() {
  pinMode(statusLed, OUTPUT);
  pinMode(errorLed, OUTPUT);

  Serial.begin(9600);
  Serial1.begin(9600);
  xbee.setSerial(Serial1);
  
  Serial.println("Controller Starting . . . .");
  
}

void loop() {   
  // break down 10-bit reading into two bytes and place in payload
  pin5 = analogRead(5);
  payload[0] = pin5 >> 8 & 0xff;
  payload[1] = pin5 & 0xff;

  xbee.send(zbTx);

  // flash TX indicator
  flashLed(statusLed, 1, 100);

  // after sending a tx request, we expect a status response
  // wait up to half second for the status response
  if (xbee.readPacket(500)) {
    // got a response!

    // should be a znet tx status            	
    if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
      xbee.getResponse().getZBTxStatusResponse(txStatus);

      // get the delivery status, the fifth byte
      if (txStatus.getDeliveryStatus() == SUCCESS) {
        // success.  time to celebrate
        flashLed(statusLed, 5, 50);
      } else {
        // the remote XBee did not receive our packet. is it powered on?
        flashLed(errorLed, 3, 500);
      }
    }
  } else if (xbee.getResponse().isError()) {
    Serial.print("Error reading packet.  Error code: ");  
    Serial.println(xbee.getResponse().getErrorCode());
  } else {
    // local XBee did not provide a timely TX Status Response -- should not happen
    flashLed(errorLed, 2, 50);
  }

  delay(1000);
}

Series_2_Rx:

/**
 * Copyright (c) 2009 Andrew Rapp. All rights reserved.
 *
 * This file is part of XBee-Arduino.
 *
 */
 
#include <XBee.h>

/*
This example is for Series 2 XBee
Receives a ZB RX packet and sets a PWM value based on packet data.
Error led is flashed if an unexpected packet is received
*/

XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle 
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();

int statusLed = 13;
int errorLed = 13;
int dataLed = 13;

void flashLed(int pin, int times, int wait) {
    
    for (int i = 0; i < times; i++) {
      digitalWrite(pin, HIGH);
      delay(wait);
      digitalWrite(pin, LOW);
      
      if (i + 1 < times) {
        delay(wait);
      }
    }
}

void setup() {
  pinMode(statusLed, OUTPUT);
  pinMode(errorLed, OUTPUT);
  pinMode(dataLed,  OUTPUT);
  
  // start serial
  Serial.begin(9600);
  
  Serial1.begin(9600);
  xbee.begin(Serial1);
  
  flashLed(statusLed, 3, 50);
  
  Serial.println("Robot Starting . . . .");

}

// continuously reads packets, looking for ZB Receive or Modem Status
void loop() {
    
    xbee.readPacket();
    
    if (xbee.getResponse().isAvailable()) {
      // got something
      
      if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
        // got a zb rx packet
        
        // now fill our zb rx class
        xbee.getResponse().getZBRxResponse(rx);
            
        if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
            // the sender got an ACK
            flashLed(statusLed, 10, 10);
        } else {
            // we got it (obviously) but sender didn't get an ACK
            flashLed(errorLed, 2, 20);
        }
        // set dataLed PWM to value of the first byte in the data
        analogWrite(dataLed, rx.getData(0));
      } else if (xbee.getResponse().getApiId() == MODEM_STATUS_RESPONSE) {
        xbee.getResponse().getModemStatusResponse(msr);
        // the local XBee sends this response on certain events, like association/dissociation
        
        if (msr.getStatus() == ASSOCIATED) {
          // yay this is great.  flash led
          flashLed(statusLed, 10, 10);
        } else if (msr.getStatus() == DISASSOCIATED) {
          // this is awful.. flash led to show our discontent
          flashLed(errorLed, 10, 10);
        } else {
          // another status
          flashLed(statusLed, 5, 10);
        }
      } else {
      	// not something we were expecting
        flashLed(errorLed, 1, 25);    
      }
    } else if (xbee.getResponse().isError()) {
      Serial.print("Error reading packet.  Error code: ");  
      Serial.println(xbee.getResponse().getErrorCode());
    }
}

I finally get that HW serial thing, here's my code for basic com.

#define hwSerial Serial1 //Just to change the name of Serial1 so I don't get confused, it will be used to connect Xbee
 //Serial1 uses Pins 18 and 19
 // Connect DOUT to pin 19
 // Connect DIN to pin 18

void setup() {
  Serial.begin(9600);
  hwSerial.begin(9600);
  delay(500);
}

void loop() {
  if(Serial.available()) {          // Is serial data available?
    char outgoing = Serial.read();  // Read character, send to XBee
    hwSerial.print(outgoing);
  }
  
  if(hwSerail.available()) {            // Is data available from XBee?
    char incoming = Serial1.read();    // Read character,
    Serial.println(incoming);       //   send to Serial Monitor
  }

  delay(50);
}

This was tested with S1 Xbee, one connected to MEGA 2560, and the other to XCTU

hello I'm also facing the same issue with mega and xbee2, but could not solve till now. please guide me properly to view xbee+xctu message on seriel monitor of arduino.

please guide me properly to view xbee+xctu message on seriel monitor of arduino.

  1. Properly configure each XBee
  2. Properly connect one XBee to the PC.
  3. Properly connect the other XBee to the Arduino.
  4. Write the proper code, compile, link, and upload it to the Arduino.
  5. Open the Serial Monitor application.
  6. Type a message in X-CTU
  7. Read the message in the Serial Monitor application

Do NOT screw up a single step!