xbee and arduino both writing to Serial?

I'm putting together a sensor net using arduino uno and xbees. I have a "base station" with arduino and an xbee connected to a Mac over USB. The "router" xbees keep sending messages with measurements to the base station and these messages show up on "Serial" (and I can parse them on the arduino or the Mac). Now, I also want to use the base station arduino to measure other things directly and send those measurements to the computer over Serial. However, if both the base xbee and the base arduino keep writing to the Serial (and I want to read the result on the Mac), I assume the messages could get arbitrarily interleaved.

Any thoughts if this can be done?


Yes it can be done. How the data gets routed to the Mac via serial is up to you to handle in code. If you are merely displaying it on Mac's equivalent of HyperTerminal then just include Text to explain what the data is. If you are handling the data collection on the Mac through a program, send header info with the data packet. This will let your program know what it is receiving.

Hope that makes sense.

Hmm, maybe I'm missing something. When the base station xbee receives a message from a sensing xbee, it writes that message to Serial and that message shows up on my Mac (in the terminal or in an application that's reading from that terminal). I can write more stuff to Serial, but I don't think I can prevent that message showing up on the Mac. Ideally, I would intercept that message on the Arduino and send a different message to the Mac.

It seems to me that the "Serial" is shared by the Mac, Arduino and xbee -- all of them can read/write to it.

Does this make sense?

Yep. I see your problem. If you are using an UNO, it only has one hardware serial port. Depending on how you configure the Xbees, they expect a result from the Receiving unit. This is the standard config. So your TX and RX on your base station would be needed by the XBee. You don't want to also send that raw data straight to the MAC.

A mega has more ports (I think two) but still might not be enough.

Any pin can be configured as an RX/TX pin using software serial. There is a standard library for it. Do a forum or google search for it and you will find a lot more information. So what I would do is use softserial for the Xbees and use the hardware to talk to the MAC. This way you handle the data in the Arduino and only send what you want to the MAC when you want it.

ok, that makes sense. thanks!

more ports (I think two)


Have you got the xbee connected directly to pine 0 and 1? If so that's a bit dodgy, you shouldn't connect outputs together without some careful thought.


I do this regularly, with the XBee in API mode. Just debug messages actually, but because the debug messages never look like a valid frame to the XBee, it just ignores them. The XBee traffic of course shows up as well, as gobbledegook, but it’s easy enough to tell one from the other, and like I said, it’s just for debugging purposes anyway.