XBee: Difference between revisions

From embeddedTS Manuals
No edit summary
m (FTP links auto-updated (http://code.google.com/p/xbee-api/ →‎ https://github.com/andrewrapp/xbee-api, http://code.google.com/p/xbee-api/ →‎ https://github.com/andrewrapp/xbee-api))
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Infobox
{{Infobox
|title        = XBee
|title        = XBEE
|image        = [[File:Xbee.jpg|200px]]
|image        = [[File:Xbee.jpg|200px]]
|titlestyle  =  
|titlestyle  =  
Line 10: Line 10:
|header2      = Documentation
|header2      = Documentation
|data3        = [http://www.digi.com/pdf/ds_xbeemultipointmodules.pdf Datasheet]  
|data3        = [http://www.digi.com/pdf/ds_xbeemultipointmodules.pdf Datasheet]  
|data4        = [http://ftp1.digi.com/support/documentation/90000982_B.pdf Product Manual]
|data4        = [https://www.digi.com/resources/documentation/digidocs/pdfs/90000982.pdf Product Manual]
|data5        = [http://ftp1.digi.com/support/documentation/xbee_rpsma_mechanical_drawing.pdf Mechanical Drawing]
|data5        = [http://ftp1.digi.com/support/documentation/xbee_rpsma_mechanical_drawing.pdf Mechanical Drawing]
|data6        = [http://www.digi.com/products/wireless-wired-embedded-solutions/zigbee-rf-modules/point-multipoint-rfmodules/xbee-series1-module.jsp Digi International]
|data6        = [http://www.digi.com/products/wireless-wired-embedded-solutions/zigbee-rf-modules/point-multipoint-rfmodules/xbee-series1-module.jsp Digi International]
Line 16: Line 16:


= Overview =
= Overview =
The XBee is a 802.15.4 compliant radio module.  It connects to the boards using a UART, and several DIO pins. See your SBC manual for more information on the UART and DIO connection.
Many of our boards support the XBEE radios which can be used for long distance communication and mesh networking using the  [http://en.wikipedia.org/wiki/ZigBee ZigBee protocols].


= Getting Started =
= Getting Started =
Digi offers the best introductions to this product.  Many of these tutorials or samples are aimed at different platforms, but the UART information will still be relevant.
Digi offers the best introductions to these products:


[http://www.digi.com/products/wireless-wired-embedded-solutions/zigbee-rf-modules/point-multipoint-rfmodules/xbee-series1-module.jsp XBee Learn More]
[http://www.digi.com/products/wireless-wired-embedded-solutions/zigbee-rf-modules/point-multipoint-rfmodules/xbee-series1-module.jsp XBee Learn More]
[http://www.digi.com/support/kbase/kbaseresultdetl.jsp?id=2188 XBee 802.15.4 Digital Input/Output Line Passing]
For programming with their series the Linux community has created libraries that make this very convenient. 


[http://www.digi.com/support/kbase/kbaseresultdetl.jsp?id=2188 XBee 802.15.4 Digital Input/Output Line Passing]
* [https://code.google.com/archive/p/libxbee/ libxbee] is a C/C++ API for series 1, 2, and 5 XBEE modules in API mode.
* [http://code.google.com/p/python-xbee/ python-xbee] is a python module for API and command mode
* [https://github.com/andrewrapp/xbee-api xbee-api] is a java API for Series 1 and 2 in API mode.


= Programming Example =
= Programming Example =
This source example assumes the baud rate is already set up to 9600 (required by command mode)
This source example assumes the baud rate is already set up to 9600 and that you are using a series 1 XBEE module.  Keep in mind that you can reprogram the baud rate that the XBEE module will use.  First set up the UART before running your application:
<source lang=bash>
eval $(xuartctl --server --port 3 --speed 9600 2>&1); ln -s $ttyname /dev/ttyxuart3
# Now that the xuart device is created you can start your application:
myapp &
</source>
 
This is an example application that will check for the "OK" return from +++.  For real usage of the XBEE you will want to use one of the APIs listed above.


<source lang=c>
<source lang=c>
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <fcntl.h>
#include <termios.h>
#include <termios.h>
#include <stdio.h>
#include <strings.h>
#define XBEEDEVICE "/dev/ttyxuart3"


int main(int argc, char **argv)
int main(int argc, char **argv)
{
{
        int fd, c, res;
        struct termios oldtio, newtio;
         char buf[255] = {0};
         char buf[255] = {0};
         int xbeefd, res;
          
 
        fd = open(XBEEDEVICE, O_RDWR | O_NOCTTY );  
         if(argc < 2)
         if(fd <0)  
         {
         {
                 printf("You must specify the COM device.\n  Example: %s /dev/pts/0\n", argv[0]);
                 perror(XBEEDEVICE);  
                 return 1;
                 return -1;
         }
         }
       
        tcgetattr(fd, &oldtio); /* save current port settings */
       
        bzero(&newtio, sizeof(newtio));
        newtio.c_cflag = CRTSCTS | CS8 | CLOCAL | CREAD;
        newtio.c_iflag = IGNPAR;
        newtio.c_oflag = 0;


         xbeefd = open(argv[1], O_RDWR | O_RSYNC | O_DSYNC | O_SYNC);  
         /* set input mode (non-canonical, no echo,...) */
         if(xbeefd == -1)
        newtio.c_lflag = 0;
         {
       
                perror("Couldn't open the COM device\n");
         newtio.c_cc[VTIME]    = 0;  /* inter-character timer unused */
                return 1;
        newtio.c_cc[VMIN]    = 2;  /* blocking read until 2 chars received */
        }
          
        tcflush(fd, TCIFLUSH);
        tcsetattr(fd, TCSANOW, &newtio);


         // Sending +++ without 1 second puts the Xbee into command mode
         // Sending +++ within 1 second sets the XBee into command mode
         write(xbeefd, "+++", 3);
         write(fd, "+++", 3);


         // Returns 'OK' on command mode
         res = read(fd, buf, 255);
        read(xbeefd, &buf, 254);
        buf[res] = 0;


         if(buf[0] == 'O' && buf[1] == 'K')
         if(0 == strncmp(buf, "OK", 2))
         {
         {
                 printf("XBee detected\n");
                 printf("XBee Detected\n");
         }  
         }
         else  
         else
         {
         {
                 printf("Could not find the XBee\n");
                 printf("Could not find XBee\n");
         }
         }


 
         tcsetattr(fd, TCSANOW, &oldtio);
         printf("\n\nbuf: %s\n\n", &buf[0]);
 
        // Exit command mode
        write(xbeefd, "ATCN", 4);


         return 0;
         return 0;
}
}
</source>
</source>

Latest revision as of 18:28, 3 February 2021

XBEE
Xbee.jpg
Released Mar. 2010
Documentation
Datasheet
Product Manual
Mechanical Drawing
Digi International

Overview

Many of our boards support the XBEE radios which can be used for long distance communication and mesh networking using the ZigBee protocols.

Getting Started

Digi offers the best introductions to these products:

XBee Learn More XBee 802.15.4 Digital Input/Output Line Passing

For programming with their series the Linux community has created libraries that make this very convenient.

  • libxbee is a C/C++ API for series 1, 2, and 5 XBEE modules in API mode.
  • python-xbee is a python module for API and command mode
  • xbee-api is a java API for Series 1 and 2 in API mode.

Programming Example

This source example assumes the baud rate is already set up to 9600 and that you are using a series 1 XBEE module. Keep in mind that you can reprogram the baud rate that the XBEE module will use. First set up the UART before running your application:

eval $(xuartctl --server --port 3 --speed 9600 2>&1); ln -s $ttyname /dev/ttyxuart3
 
# Now that the xuart device is created you can start your application:
myapp &

This is an example application that will check for the "OK" return from +++. For real usage of the XBEE you will want to use one of the APIs listed above.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <strings.h>

#define XBEEDEVICE "/dev/ttyxuart3"

int main(int argc, char **argv)
{
        int fd, c, res;
        struct termios oldtio, newtio;
        char buf[255] = {0};
        
        fd = open(XBEEDEVICE, O_RDWR | O_NOCTTY ); 
        if(fd <0) 
        {
                perror(XBEEDEVICE); 
                return -1;
        }
        
        tcgetattr(fd, &oldtio); /* save current port settings */
        
        bzero(&newtio, sizeof(newtio));
        newtio.c_cflag = CRTSCTS | CS8 | CLOCAL | CREAD;
        newtio.c_iflag = IGNPAR;
        newtio.c_oflag = 0;

        /* set input mode (non-canonical, no echo,...) */
        newtio.c_lflag = 0;
         
        newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
        newtio.c_cc[VMIN]     = 2;   /* blocking read until 2 chars received */
        
        tcflush(fd, TCIFLUSH);
        tcsetattr(fd, TCSANOW, &newtio);

        // Sending +++ within 1 second sets the XBee into command mode
        write(fd, "+++", 3);

        res = read(fd, buf, 255);
        buf[res] = 0;

        if(0 == strncmp(buf, "OK", 2))
        {
                printf("XBee Detected\n");
        }
        else
        {
                printf("Could not find XBee\n");
        }

        tcsetattr(fd, TCSANOW, &oldtio);

        return 0;
}