I have an Uno with a Series 2 XBee, for the most part working well together using SoftwareSerial. However, now and again I get garbage on the RX serial line. For example and expected message from the other side of the XBee is:
2 0 0 -1\n
Every now and again I get a bunch of extra garbage that looks like the image attached to this post, the extended characters don't come out in a copy/paste here.
This wouldn't be so bad if it only happened once every few dozen messages, I could just drop that one and fill in the gap with the previous message until the next message arrives but the problem appears to be magnified when I have a GY-521 6dof sensor using interrupts. With that connected I get more than one "corrupted" message every 10 messages or so. It seems that when I disconnect the interrupt wire the serial noise is reduced greatly but not entirely.
I have a voltage divider on the tx line so the xbee never sees more than 3.3v. I've checked the other side of the xbee connection and I'm seeing zero noise going into the messages being sent so its something happening from that point forward.
Board: Uno
Connections:
XBeeS2 vcc/vref/rx/tx: 3.3/3.3/12/13
GY-521 scl/sda/ad0/int: A5/A4/GND/2(INT 0)
Is there anything I can do to get cleaner output?
This is how I'm reading from the xbee (SoftwareSerial). I've tried a few different ways and I see the same result with both methods.
void processXBeeMessage() {
String foo = xbee.readStringUntil('\n');
Serial.println(foo);
char array[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
foo.toCharArray(array, 32);
/* int x=0;
while( xbee.available() ) {
if( xbee.peek() == '\n' ) {
xbee.read();//clear buffer
array[x++] = '\0'; //end of string
break;
} else {
array[x++] = xbee.read();
}
}*/
Serial.print("XBEE STR: ");
Serial.println(array);
}
