Why is the Serial Monitor printing out sent data package + garbage?

Hi,

I'm transmitting/receiving data packages and the Serial Monitor is Printing out the message (Hi), plus a bunch of garbage. See attached Picture. Why is this happening?

#include <XBee.h>
#include <Servo.h>

XBee xbee = XBee();
Rx16Response rx16 = Rx16Response();
Servo myservo;

uint8_t payload[] = { 'H', 'i' };

Tx16Request tx16 = Tx16Request(0xFFFF, payload, sizeof(payload));


void setup() {
   Serial.begin(9600);
   xbee.begin(Serial);
   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
   myservo.write(45);  // set servo to starting position
}

void loop() {
  for (int i = 0; i<100; i++){
    for (int angle = 45; angle < 135; angle=angle+10){
      myservo.write(angle);
      delay(15);    // wait for servo to write
      xbee.send(tx16);
      xbee.readPacket(300);
      if (xbee.getResponse().isAvailable()){
        if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
          xbee.getResponse().getRx16Response(rx16);
          Serial.print(angle);
          Serial.print(" ");
          Serial.println(rx16.getRssi());
        }
      }
    }
  }
}

Are you using Serial to talk to the XBee OR the PC? Both is the wrong answer. So is yes.

I'd guess mis-matched data rate, or many non-printable characters.

CrossRoads:
I'd guess mis-matched data rate, or many non-printable characters.

But I'm not even telling it to print the transmitted payload, so why is it?
Even if I delete all Serial.prints it still prints out this garbage

epaulsson:
But I'm not even telling it to print the transmitted payload, so why is it?

Did you read Reply #1 ?

...R

PaulS:
Are you using Serial to talk to the XBee OR the PC? Both is the wrong answer. So is yes.

I'm using Serial to talk to XBee. I have two XBees and two Arduinos communicating with each other.
One is transmitting a data package to the other one which sifts out the RSSI value and sends it back.

If I delete all Serial.prints in my code it still prints this out:
The payload is now "Y".

PAYLOADy.JPG

epaulsson:
If I delete all Serial.prints in my code it still prints this out:
The payload is now "Y".

That looks like the gibberish you see with a mismatched baud rate...

epaulsson:
I'm using Serial to talk to XBee.

Then you have two things connected to Serial - the Xbee and the Serial Monitor - is it any wonder that stuff is appearing on the Serial Monitor?

Use Serial for the Serial Monitor and use SoftwareSerial to create a separate serial connection for the Xbee.

...R

If you are connected via USB to the PC, with the Serial Monitor open, then anytime there is activity on D1, the Serial monitor will see it and attempt to display it.
If you are using D1 to send to the Xbee, the Serial Monitor will see it and attempt to display it.
If you talk to the Xbee on one data rate and the Serial Monitor is set to a different data rate, it can look like your pictures.
If you are sending other than 'a' or 'A' (i.e., characters) to the Xbee, it can look like that.

So: What pins do you have connected to your Xbee? I can't tell from the code you posted.
I would guess that these lines would indicate 0 & 1 since you don't seem to set up any software serial for it:

#include <XBee.h>

XBee xbee = XBee();

   Serial.begin(9600);
   xbee.begin(Serial);

CrossRoads:
...anytime there is activity on D1, the Serial monitor will see it and attempt to display it.

Okay, I didn't know the Serial monitor tried to display the activity on D1 if there wasn't a Serial.print, my bad.

CrossRoads:
So: What pins do you have connected to your Xbee? I can't tell from the code you posted.
I would guess that these lines would indicate 0 & 1 since you don't seem to set up any software serial for it:

Yeah, I have an XBee shield, so I am using 0 and 1. But the data rate is the same for both, 9600, isn't it? Or am I missing something here? Sorry, I'm a rookie, thanks for your help =)

We don't know what your serial monitor rate is set to, what is in the lower right corner?

And there is still the matter of non-printable characters.
Look here and see if the data you are expecting from the Xbee falls outside the range that represents a-z, A_Z, 0-9, CR, LF, and stuff like ,<.>//;:'"[{]}!@#$%^&*()-_=+|`~

CrossRoads:
We don't know what your serial monitor rate is set to, what is in the lower right corner?

And there is still the matter of non-printable characters.
Look here and see if the data you are expecting from the Xbee falls outside the range that represents a-z, A_Z, 0-9, CR, LF, and stuff like ,<.>//;:'"[{]}!@#$%^&*()-_=+|`~

It says 9600 baud in the lower right corner of the serial monitor. But, I'm just transmitting the letter 'Y' (and receiving the RSSI (int) from the other XBee). But it's printing everytime I do this:

xbee.send(tx16);

and tx16 is this:

uint8_t payload[] = {'Y'};
Tx16Request tx16 = Tx16Request(0xFFFF, payload, sizeof(payload));

I don't know what all actually goes out with the 'Y', I've never used an Xbee, but apparently it's a lot more than just 0x59
www.asciitable.com, which is the HEX data for a Y.

You could try a different monitor program on your PC, see what the data coming out is.
I've heard Putty is good, I used to use HyperTerminal when it came with Windows, I don't know what else there is.
I have a Saleae 8 logic analyzer, it also functions as a protocol analyzer, I use that when I really want to get serious about looking at data.

I think you are getting bus contention on the serial data lines with your xbee shield.

Here is my schematic of an (generic) xbee shield; note the switch prevents contention between the Arduino USB transceiver, the AVR and the xbee shield. It would be two transmit lines tied together arm wrestling.

The xbee shields are tricky; to talk to an xbee module and configure it, you have to pull the '328 too.

XBee_Shield_V30_geeetech.PDF (44.6 KB)