C++ interface to XBee

Hi,

I want to wirelessly control my lilypad motors. Someone suggested I should look at XBee as an intefacing device, and it seems that XBee is a good choice. But, before I get one I have one question.

Are their C/C++ libraries that can directlly send and receive command/data to/from an XBee module? My system requires me to control the motors wirelessly and also get data from some sensors wirelessly.

The software to control the motors and sensors is a C++ program that runs on a computer/laptop. What I mean is something like this only in C/C++.

http://code.google.com/p/xbee-api/

I have googled enough but without success. I have seen Java, Python, Arduino libraries, but nothing in C/C++. My only other option seems to be to use an Arduino board as a bridge. Send commands to the board, the board (using the XBee API) sends commands to the XBee attached to the sensors and relays the response back to the computer.

I am desparately hoping to avoid that. Any help is greatly appreciated.

regards,

A

Many of the XBee module’s firmware does support “transparent mode”. With this serial data put into one module comes out the other module.

The most popular modules for this are the Series 1 with 802.15.4 protocol firmware in “AT” command mode.

What are you using for your lilypad control? a PC Com port?

If so then the correct XBee modules, with the appropriate level translators, can be substituted for the RS232 cable.

There would be no need for any special code, C or other.

Not sure but this appears to be in C++.

http://code.google.com/p/xbee-arduino/

Author’s comments also say

It should be easy to port this library to any microcontroller that supports C++ and serial available/read/write/flush. The only other dependency is the millis() function for milliseconds.

Thanks Waltr. I have a slight problem with using the AT mode. I have multiple such motors, each connected to an XBee module. Depending on some sensor input I want to set a particular motor on/off.

To achieve this goal, I plan to connect the motor to one of the the output pins of the XBee. Thus, by setting that particular pin to HIGH or LOW I can control the motor.

The software that reads the sensor data and determines which motors to start/stop is written in C++. Therefore, my current planned control flow is like this:

C++ code reads sensor data and decides what motor to start/stop. It makes an API call to send this command to an XBee explorer module. This explorer module sends the appropriate command to the appropriate XBee module.

What you are suggesting is that instead of using the API mode, I use the transparent mode in communicating with the XBee explorer module?

In light of all this new information, do you think AT mode will work? I am new to XBees, so if you have a better solution or see a problem with the current one please let me know.

regards,

A

Still not completely sure of how your sensor/control functions.

I assumed that maybe you already had a processor connected to the lilypad and was reading sensors and sending motor commands via a serial port.

To clarify: “Transparent mode” just passes serial data into one XBee/explorer module across the air to another XBee/explorer module across the air to another as if instead of the two XBee/explorer modules the serial in/out points were connected by wires.

The software that reads the sensor data and determines which motors to start/stop is written in C++. Therefore, my current planned control flow is like this:

Where does this C++ code reside? In a PC? or a micro-controller?

This is where I am confused about what you want to do.

I think you have a PC running the C++ code that gets sensor data then turns motors on or off and you want to do this wireless.

Can you clarify.

As to XBee C code here are a few more examples and other info:

http://code.google.com/p/libxbee/wiki/s … n_advanced

http://forums.trossenrobotics.com/tutor … sics-3259/

http://www.jsjf.demon.co.uk/xbee/xbee.html

http://svn.gna.org/viewcvs/aeropic/trun … gestart=50 (bottom of list)

There is another site with C code but I can’t find it at the moment.

Sorry I did not clarify that. The C++ code resides on a PC. The sensors send their data to this C++ code. Based on that data I want to control the motors (from the PC).

The motors are idependently powered and are on a far off site. So, I need a wireless module such as XBee to control them.

Hope this clarifies the confusion. Thanks for the links. I will take a look.

regards,

A

Ok, that’s a little clearer.

So to restate:

You have remote lilypad circuits that includes an XBee lilypad.

The sensors can be connected to the XBee (through the lilypad board) input pins. The motor control circuits on the lilypads connect to the XBee (through the lilypad board) output pins. This XBee can be in ‘AT’ command mode.

On the PC side: An XBee/explorer module connects to a PC Com/USB port. This XBee needs to be in API mode. The C code needs routines that send and receive API frames to/from the XBee/explorer module as only API frames can send commands to read/write pins on a remote XBee.

This would work. Have you written the C code that you are currently running on the PC? If so then adding routines (with a little help from the links posted in previously) should not be that hard.

Here is the link I couldn’t find earlier:

http://www.circuitsathome.com/category/wireless

The code there is written for a PIC but its in C and wouldn’t be hard to port to a PC.

Thanks. I think I am getting a hang of these. Hopefully, its not too complicated.

Just one question: Why should the XBees connected to the motors be in the AT mode?

regards,

A

Because they’re easier to use in AT mode :stuck_out_tongue:

To have a ‘central’ XBee you need to reflash it’s firmware into Coordinator API mode. The normal XBee’s look for a Coordinator on startup within a specified address range and connect themselves to it (can use defaults, or change the addresses on all of them).

In AT mode you just send out text through the UART and the XBee sends it to the coordinator.

In API mode you have to send multiple bytes, who the message is for, how long the message is, option bytes and a calculated checksum at the end (among other things).

The Coordinator, which is connected to your PC, will require API code to package up a message and send it to the required EndPoint XBee. It’ll also require API coding for the incoming messages to unwrap the messages to determine who they’re from (which motor) etc

This is how I use them anyway, just because it’s easier, but either would work (both in API mode). You can have a coordinator in AT mode too I believe, but then you have to enter command mode to set the address you want to send to, and i’m not sure how you determine who the message was from when receiving either, so for the coordinator it’s easier to use in API mode.

Ok. So what you are saying is that the difference between the AT and API mode is just how one extracts/send information from/to an XBee module. In either case the data sent through “the air” is the same.

Another quick question. I was going throught the XBee manual and wanted to configure the XBees. I have most of the informtation I need. However, I cannot find a comprehensie set of AT commands or API interface. Digi’s site is hopeless in this matter :(. If they have such a document its really hard to find. Does anyone know of a document/website with a comprehensive set of commands or API interface? Google isnt helpful either.

regards,

A

All the AT commands for XBees can be found in the product manuals on the Digi site - or if you’re struggling on that site - use Sparkfun’s. Just find the applicable module first.

Every one of the Xbee modules they sell has a link to the Product Manual for that module. The AT command reference is detailed extensively at the end of the manual.

The API is also described there. It’s a good way to start since every other working example probably referenced this document when they started. Have you tried using waltr’s links - or doing a search of the Sparkfun forum.

aundh:
Ok. So what you are saying is that the difference between the AT Digi’s site is hopeless in this matter :(. If they have such a document its really hard to find. Does anyone know of a document/website with a comprehensive set of commands or API interface? Google isnt helpful either.

regards,

A

Simply read the OEM manual at

http://www.digi.com/products/wireless/p … e.jsp#docs

http://ftp1.digi.com/support/documentat … 0982_B.pdf

Digi’s website is not entirely hopeless, as said.

Since there are a couple of different hardware versions and each could have different firmware be sure you are using the correct document. Also when asking questions please specify which module/firmware type you are using. Each has details that differ. We can then point you to a specific page.

Digi’s documents can be confusing when you begin to learn but they do contain everything you need to know.

aundh:
Ok. So what you are saying is that the difference between the AT and API mode is just how one extracts/send information from/to an XBee module. In either case the data sent through “the air” is the same.

Yes, that is correct. And depending on the firmware used in some cases it’s easier to use one mode or the other, ie EndPoint mode I find it much easier to just use the AT mode, whereas for Coordinators it’s easier to use API mode (as you set the ‘to’ address when you send the message, rather than having to enter command mode, which takes at least two seconds (by default, can probably be less if you lower the ‘guard’ times), setting the address, exiting command mode and then sending the data).