USB and XBee Communication

Hey Guys,

Wanted to see if anyone knows anything about using USB and XBee simultaneously. I know that you cannot use them at the same time as there is only one Serial port, but i was wondering about using a transistor as a switch so that I can turn a digital pin HIGH which would connect the XBee and Recieve Pin, and turn the pin LOW which would disconnect the XBee from the Receiver pin allowing communication between the computer and Arduino.

Is this possible? Has anyone tried it? If this is not the best solution, what is?

Thanks!

What comes in over-the-air, the XBee puts on the RX pin. What the Arduino writes to the TX pin, the XBee will broadcast.

This does not preclude you from using XBee and USB at the same time. As long as you know where the data is coming from (XBee or PC), you can use both at the same time.

No but the thing is I want the computer to send a command to the Arduino while the TX/RX pins are connected to XBee.

Is there a way I can know whether I am connected to USB or XBee?

If your get your e-mail, do you have any trouble identifying who the message is from? No, of course not, because the message is tagged with who it's from. There's a real good reason for that, and a hint about what you have to do.

The PC that is sending messages needs to tag its messages, as does whatever the other XBee is attached to.

But how do I write to the Arduino serially while the TX and RX Pins are connected to the XBee? There is only one serial port and if it is taken up by the XBee how can I send commands from the PC?

Unlike when a PC connects to a serial port, the Arduino end of the serial port IS the TX/RX pins. Any number of things can read the state of the RX pin and set the state of the TX pin.

When the Arduino reads data from the RX pin, it puts in an a buffer, for the Serial.available() and Serial.read() functions to operate on.

When the Arduino sends data to the serial port, using Serial.write() or Serial.print(ln)(), the TX pin is toggled HIGH and LOW at specific times. This toggling is read by the device on the other end of the serial port.

The XBee also monitors the TX pin. When it sees the TX pin toggle, it broadcasts that data over the air.

When the XBee receives data over the air, it toggles the RX pin in the same manner that the PC does.

So, the Arduino can get data, on the RX pin, from the PC and the XBee at the same time, and can send data to the PC and XBee at the same time.

The thing that you can't do is send data to the XBee OR the PC. You send data to the XBee AND the PC.

Just like talking in a room full of people, you have to identify who the message is for, and ensure that all listeners respond only to messages send specifically to them.

"PC: Here is the data you requested"
"XB: Broadcast this"
"PX: Hey, everybody, listen up"

The PC should ignore messages sent to the XB only. The XB doesn't process messages it receives, it just broadcasts them, so the receiver needs to know whether to do something with the message, or not.

You could, as in my third line, send messages that both are to process.

I can turn a digital pin HIGH which would connect the XBee and Recieve Pin, and turn the pin LOW which would disconnect the XBee from the Receiver pin allowing communication between the computer and Arduino.

You could use an analogue MUX for this, but it would a lot easier just to use software serial for the XBee.


Rob

Here is the project that I am doing:

I have a device that needs to connect to the Arduino via its USB Port to a computer. The computer sends a command via a terminal to the Arduino. Once the data has been received by the Arduino, the Arduino needs to be able to use the XBee's to send and receive commands only. It will not use USB again until it has been reset.

Currently, if I plug in the XBee to the Arduino's TX/RX pins, I cannot send any messages to the Arduino via HyperTerm or the Arduino IDE's Serial Monitor.

Should this library fix my problems? : NewSoftSerial | Arduiniana

Any ideas?

Should this library fix my problems? : NewSoftSerial | Arduiniana

Yes

Any ideas?

As I said

but it would a lot easier just to use software serial for the XBee.

Just hook up the Xbee to two digital pins, say 2 and 3, and talk to it using SoftwareSerial or NewSoftwareSerial.


Rob

Graynomad, when you say:

Just hook up the Xbee to two digital pins, say 2 and 3, and talk to it using SoftwareSerial or NewSoftwareSerial.

do you mean some Xbee pins should be physically hooked up to Arduino pins 2 and 3? Or this can be done by changing Xbee configuration?