Go Down

Topic: Yun + Sparkfun XBee Shield (Read 7658 times) previous topic - next topic

skr1

Hi,

is it possible to use the Yun with the Sparkfun XBee Shield?
Can someone explain me the difference between the classes Serial and Serial1 on the Yun?

I have created a sketch for testing, but it is not working. In my test scenario LED 13 should be ON if an XBee frame is received.
This is my sketch using the latest XBee library 0.4 beta:

Code: [Select]

#include <XBee.h>

XBee _xbee = XBee();

void setup()
{
  pinMode(13,OUTPUT);
  digitalWrite(13, LOW);

  Serial.begin(9600);
  _xbee.begin(Serial);
}

void loop()
{
  processXBeePackets(); 
}

void processXBeePackets()
{
  _xbee.readPacket();

  if (_xbee.getResponse().isAvailable())
  {
    digitalWrite(13, HIGH);
  }
}


Thanks in advance!
Sebastian

Kryptman

Ok, first the easy part:

The Yun is similar to the Leonardo in that it does not have a dedicated chip to handle USB communication. 

You can check the Leonardo's product page for details: 
Quote
Since the boards do not have a dedicated chip to handle serial communication, it means that the serial port is virtual -- it's a software routine, both on your operating system, and on the board itself. Just as your computer creates an instance of the serial port driver when you plug in any Arduino, the Leonardo/Micro creates a serial instance whenever it runs its bootloader. The board is an instance of USB's Connected Device Class (CDC) driver.


This means that "Serial" isn't an actual UART you can use for communication with other hardware. 

This brings us to Serial1.  Serial1 is a hardware UART you can use, connected to pins D0 and D1.  You could use this to communicate with your XBee shield, altough I wouldn't recommend it, as those pins are used for bridge communications (Between the "Arduino" and the "Linux").  If you want to used these pins anyways and not use Linio, then just get a Leonardo.  Eats less power and it's cheaper.

Now what you will want to do is use the softwareserial library and create yourself a third serial port.  Check the documentation for softwareserial in the reference, altough something like this should work:

Code: [Select]
#include <SoftwareSerial.h>
#include <XBee.h>



XBee _xbee = XBee();
SoftwareSerial mySerial(8, 9); // RX, TX


void setup()
{
  pinMode(13,OUTPUT);
  digitalWrite(13, LOW);

  Serial.begin(9600);
  _xbee.begin(mySerial);
}

void loop()
{
  processXBeePackets(); 
}

void processXBeePackets()
{
  _xbee.readPacket();

  if (_xbee.getResponse().isAvailable())
  {
    digitalWrite(13, HIGH);
  }
}


You will have to reconfigure your shield to used pins 8 and 9 for UART communications, though, but that ought not to be a problem.

Hope this helps,

Albert

skr1

Hi Albert,

thank you very much for your explanation!
Now it is clear why my sample is not working.
On the XBee shield is a switch for switching the serial communication between UART and DLINE. AFAIK the UART switch position is using the digital IOs 0 and 1 and the DLINE switch position is using the digital IOs 2 and 3.
Is it possible to simply use the digital IOs 2 and 3 instead of 8 and 9? Or does it have a special reason why you recommend 8 and 9?

Thanks!
Sebastian

Kryptman

Yes, no, maybe, there is a special reason for recommending pins 8 and 9.

According to the documentation for the software serial library
Quote
Not all pins on the Leonardo support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).


Since the Yun is a Leonardo, the same limitations apply.  Furthermore, you can't use pin 7 either (handshaking between Leonardo <-> Linio).  Theoretically though, you could use any digital pin for TX and one of the above for RX.  So (D3,D8), (D4, D8), (D4, D10) would all be valid combinations, altough I like to keep corresponding pins together.  That's why 8 and 9.

Note that the Yun does NOT have SPI on pins 14, 15, and 16, so you could use those as well...

So there is a special reason for using pins 8 and above, but other than that you're free...

skr1

Hi Albert,

thanks a lot for your help!
I will try the pins 8 and 9 on the weekend!

Bye,
Sebastian

skr1

Hi Albert,

it works like a charm!  :D
I have set the switch on the XBee shield to DLINE. This is using the digital pins 2 and 3 by default. I have bended the two pins so that they have no contact to the Yun board and have connected these two pins with wires to the pins 8 and 9.
See the attached picture.
I hope this post helps other people.

Bye,
Sebastian

skr1

Hi,

this is an update for everyone who is using the Yun with the Sparkfun XBee shield.
After my solution was running a while, I have observed periodic hangs if I use the SoftwareSerial library. You can read more about this in this thread: http://forum.arduino.cc/index.php?topic=196606.msg1455320#msg1455320
So, I decided to use the AltSoftSerial library and modified my wiring so that I am using pin 13 for RX and 5 for TX. I have attached a picture so that everyone can see how the Sparkfun XBee shield is now connected for using the AltSoftSerial library.

Bye,
Sebastian

sanne1234

Hi,

I have the same problem. I use the Arduino Yun, a ArduinoXBee v1.1 shield and a Series 1 XBEE. I tried the setup with 13 and 5 for RX and TX. That didn't work. Now I use the setup with 8 and 9 for RX and TX. I also bent the two pins as the pictures show.

I tried it with the code above and sent to it with the XCTU software. I also tried sending via the Arduino and receiving with the XCTU.

I can send between two XBees if they are both in explorers connected to the computer and the Yun seems to work fine.

Anybody with an idea what can be wrong?

ShapeShifter

Please post your code, that is essential. (Unless it is EXACTLY the same code as above, in which case say so, but if you made ANY changes, please post it.)

The code above uses the XBee library, which requires that the XBee modules be set up in AP mode. If you look at the documentation for the XBee library, it describes the required setup. Have you done this?

What version of the Arduino IDE are you using? There are some improvements to Software Serial starting with version 1.6.1. If you are using an earlier version, upgrading to the latest version may help.

sanne1234

Hi,

Thanks for your reply

Yes I copied the code of Kryptman and used it without changing anything. I tried it with XBees in API1, API2 and AT, just to be sure.

I am using the IDE version 1.6.1 and tried it both in Ubuntu and in Windows.

Any other ideas?

ShapeShifter

You didn't answer this question:

The code above uses the XBee library, which requires that the XBee modules be set up in AP mode. If you look at the documentation for the XBee library, it describes the required setup. Have you done this?
The answer to that is critical, especially because of this comment of yours:

I can send between two XBees if they are both in explorers connected to the computer and the Yun seems to work fine.
Unless you are actually creating API packets during this test, this test doesn't mean anything. In fact, if you can use the terminal window and type back and forth between two XBees, it means that you don't have the XBees set up in API mode, and the code you are using on the Yun doesn't have a chance of working properly.

Go Up