[SOLVED] Leonardo with Sainsmart Xbee shield recieves but cannot send?

Hey guys, I can't believe how far I've come in the past two weeks, from making an LED blink to setting up a simple serial wireless network! The arduino is my first foray into micro-controllers and is really a thing of beauty!

Back on point, I am currently working without sleep trying to find out why my new toy isn't working like I think it should. I am currently having trouble getting my arduino leonardo with an xbee shield and an xbee module (pro series 1) to send data via it's xbee to another xbee connected via usb breakout board to my computer.

The thing that is driving me up a wall is that it receives data via the xbee absolutely fine! Since I'm using a shield I don't think it's a circuit problem, so I suspect I must have fudged some code or missed a setting in the xbee somewhere. Speaking of code:

//Declare Variables
char inByteXbee = 0;
char outByteXbee = 0;
//Begin Serial
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
while (!Serial) {
}
}

void loop() {
//Check for serial connection
if (Serial1.available() > 0) {
// store incoming data from PC Xbee
inByteXbee = Serial1.read();
// Process incoming data from PC Xbee
outByteXbee = inByteXbee + 1;
// Send outgoing data to Serial console and PC Xbee
Serial.print(outByteXbee);
Serial1.print(outByteXbee);
}
}
//Fin

So from my code you can tell that the last step of the process should be to print outByteXbee to Serial1, the leonardo's xbee. This is what is not working. Serial.print works fine, I can view it in the serial console, but Serial1.print does nothing. I initally tried it without printing to, or even begining, usb serial but when I got no ouput I added that in for debugging. I have also tried disconnecting the usb cable and running off a 9v to rule out serial interference or something. I have swapped the Xbees and it works exactly the same, data flows from pc to leonardo but no data flows back via xbee only via usb.

I also promise that I googled my brains out before I posted here, I just can't come up with the magic mix of keywords that will lead me to a solution! If anyone has any input as to why the code above wouldn't work, or why a very simple xbee setup would only work in one direction, please share!

edit: Also, prior to attempting xbee's I used wires to serial connect my leonardo to an uno, that worked fine for transmit and receive.

so I suspect I must have fudged some code or missed a setting in the xbee somewhere.

But your not going to tell us what you set when configuring the XBees?

but Serial1.print does nothing.

What are you expecting it to do? How do you KNOW it does nothing? What XBee shield are you using?

or why a very simple xbee setup would only work in one direction, please share!

Have you confirmed that the XBees can talk to each other? If you set one as Mary, and and told it to talk to Tom, and set the other one as Tom, and told it only to talk to Joe, then, of course they would exhibit the symptoms you are seeing.

Is that the entire code? I was not aware that the Leonardo had a second hardware serial port.

Try to replace the Serial1.print() by this line:

Serial1.write(outByteXbee);

The print does convert the byte to it's ASCII representation which is probably not what you expect.

I was not aware that the Leonardo had a second hardware serial port.

The Leonardo has one serial port but Serial is the USB serial emulation while Serial1 is the hardware serial interface.

Thanks, pylon! That is a nice feature. That is not shown in the product page for the Leonardo, but I am not surprised. The Arduino docs/reference pages have been ignored for a while. They are really behind the power curve.

And that reminds me, I have to bump my change request posts again. :roll_eyes:

PaulS:

so I suspect I must have fudged some code or missed a setting in the xbee somewhere.

But your not going to tell us what you set when configuring the XBees?

but Serial1.print does nothing.

What are you expecting it to do? How do you KNOW it does nothing? What XBee shield are you using?

or why a very simple xbee setup would only work in one direction, please share!

Have you confirmed that the XBees can talk to each other? If you set one as Mary, and and told it to talk to Tom, and set the other one as Tom, and told it only to talk to Joe, then, of course they would exhibit the symptoms you are seeing.

Thanks for the responses guys!
@Pauls

  1. I only set the baudrate (9600), and made sure the destination addressing were both 0's (which I read means they are in broadcast mode and should send their output to any available xbees?).
  2. I am trying to print to the Serial1 interface. With a serial console open on my PC monitoring an xbee connected via usb on com3, I see no output from the Leonardo xbee.
  3. Since I can swap them and they each receive while on the Leonardo but neither seem to send data while on the leonardo, I am more suspicious of my code.

@pylon I guess I am confused why it would work fine printing to Serial but not to Serial1, is it an xbee limitation? In any case I will definitely try Serial1.write and get back to you guys.

@pylon Serial1.write seems to give the same results. Thanks for the suggestion though.

You might have to describe your setup in a bit more detail. I got that your running the posted sketch on the Leonardo having an original Arduino Wireless SD Shield (the former version was called XBee Shield) with an XBee Series 1 on it. On the PC you have connected the Leonardo by USB and an XBee Series 1 also by USB, running two serial terminal programs, one connected to the port of the Leonardo and one to the port of the XBee. If you send some character using the XBee terminal you get the character code (increased by 1) on the terminal of the Leonardo but not on the XBee terminal. Is that correct?

Please post a picture of the jumper settings of your shield.

pylon:
You might have to describe your setup in a bit more detail. I got that your running the posted sketch on the Leonardo having an original Arduino Wireless SD Shield (the former version was called XBee Shield) with an XBee Series 1 on it. On the PC you have connected the Leonardo by USB and an XBee Series 1 also by USB, running two serial terminal programs, one connected to the port of the Leonardo and one to the port of the XBee. If you send some character using the XBee terminal you get the character code (increased by 1) on the terminal of the Leonardo but not on the XBee terminal. Is that correct?

Please post a picture of the jumper settings of your shield.

@pylon Thanks again for looking into this. I think it is actually an "original" off brand xbee shield. Here is a link:

Do you think it could just be a crappy shield? That would make sense I guess. My jumpers are both currently set to the xbee side, not the usb side. The Leonardo seems to program fine without changing the jumpers.

Your deduction of my PC setup is correct, I have both the transmitting xbee and the leonardo connected via USB, with a serial console open to each. If I send an ascii character (example "a") the Leonardo receives it wirelessly, increments it (example "b") and my expectation is that it should then send that character to both USB and xbee serial consoles. However only the USB console shows the expected output ("b").

Hi Guys,
I just wanted to post back here, I made my own interface with a regulated xbee explorer and it is working fine now. Seems my shield is either junk or incompatible.

Right now I just have 5v, ground, and tx/rx connected directly to the xbee. Someone here suggested this may not be the best way to connect to it because the shield uses the icsp pins. Anyone know if that's true? Can xbee's take advantage of the ICSP pins?

Hello zypherion,
Which xbee explorer did you get that worked? A sparkfun one? I am getting the same issue with the sainsmart usb breakout / DFrobot xbee breakout, xbee, ubuntu minicom or putty, and leonardo (well, a DFrobot romeo controller board to be more precise). I can write to the arduino but cannot read using the xbee Serial1 (the normal Serial / arduino serial monitor works fine). That is, with the following code, I can turn the LED13 on and off fine by entering '"a" or "b" in minicom/putty, "hello" shows up in the arduino serial monitor, but nothing shows up in minicom/putty.

//////////////////////////////////////////////////////////////////
//http://bildr.org/2011/04/arduino-xbee-wireless/
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Monitors the serial port to see if there is data available.
//'a' turns on the LED on pin 13. 'b' turns it off.
//////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(9600);
  Serial1.begin(115200);    	//initialize serial
  pinMode(13, OUTPUT);   	//set pin 13 as output
  delay(1000);
    Serial.println("beginning!");
  while (!Serial) { }
}

void loop() {
  while(Serial1.available()){  //is there anything to read?
	char getData = Serial1.read();  //if yes, read it
      //Serial.println("read a char");


	if(getData == 'a'){  	 
  	  digitalWrite(13, HIGH);
          Serial1.println("HIGH");
          Serial1.write("hello");
          Serial.write("hello");
	}else if(getData == 'b'){
  	  digitalWrite(13, LOW);
          Serial1.println("LOW");
	}

        
  }