A library allowing basic functions of the XBEE pro to be used. Currently supported are: Enter/exit config mode, reading device serial number, setting encryption key, writing settings to non volatile memory and sending data strings.

Dependents:   Seeed_XBee_Shield Seeed_XBee_Shield-2

Fork of xbee_lib by Tristan Hughes

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers xbee.cpp Source File

xbee.cpp

00001 /* Copyright (c) 2012 Tristan Hughes, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or 
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018  
00019  #include "xbee.h"
00020 
00021 
00022 xbee::xbee(PinName tx, PinName rx, PinName reset)
00023 {
00024     _tx = tx;
00025     _rx = rx;
00026     _reset = reset;
00027 }
00028 
00029 xbee::~xbee()
00030 {
00031 }
00032 
00033 int xbee::ConfigMode()
00034 {
00035     int a = 0;
00036     Serial DATA(_tx,_rx);
00037     wait(2);
00038     DATA.printf("+++");
00039     while (a != 75) {
00040         if (DATA.readable()) {
00041             a = DATA.getc();
00042         }
00043     }
00044     wait(1);
00045     printf("config mode\n");
00046     return 1;
00047 }
00048 
00049 int xbee::GetSerial(int *serial_no)
00050 {
00051     int sh1,sh2,sh3,sl1,sl2,sl3,sl4;
00052     Serial DATA(_tx,_rx);
00053     wait_ms(50);
00054     DATA.printf("ATSL \r");
00055     DATA.scanf ("%2x%2x%2x%2x",&sl1,&sl2,&sl3,&sl4);
00056     wait_ms(500);
00057     DATA.printf("ATSH \r");
00058     DATA.scanf ("%2x%2x%2x",&sh1,&sh2,&sh3);
00059 
00060     serial_no[0] = sh1;
00061     serial_no[1] = sh2;
00062     serial_no[2] = sh3;
00063     serial_no[3] = sl1;
00064     serial_no[4] = sl2;
00065     serial_no[5] = sl3;
00066     serial_no[6] = sl4;
00067 
00068     return 1;
00069 }
00070 
00071 int xbee::SetKey(char* key)
00072 {
00073     Serial DATA(_tx,_rx);
00074     DATA.printf("ATEE 1 \r");
00075 
00076     DATA.scanf ("%*s");
00077     wait_ms(1);
00078     DATA.printf("ATKY %s \r",key);
00079     DATA.scanf ("%*s");
00080     return 1;
00081 }
00082 
00083 int xbee::WriteSettings()
00084 {
00085     Serial DATA(_tx,_rx);
00086     wait_ms(5);
00087     DATA.printf("ATWR \r");
00088     DATA.scanf ("%*s");
00089     return 1;
00090 }
00091 
00092 int xbee::ExitConfigMode()
00093 {
00094     Serial DATA(_tx,_rx);
00095     wait_ms(5);
00096     DATA.printf("ATCN \r");
00097     DATA.scanf ("%*s");
00098     return 1;
00099 }
00100 
00101 int xbee::SendData(char *data_buf)
00102 {
00103     Serial DATA(_tx,_rx);
00104     DATA.printf("%s",data_buf);
00105     return 1;
00106 }
00107 
00108 void xbee::RecieveData(char *data_buf, int numchar)
00109 {
00110     int count=0;
00111     if(numchar == 0) {
00112         numchar = sizeof(data_buf);
00113     }
00114     Serial DATA(_tx,_rx);
00115     while(numchar!=count) {
00116         if(DATA.readable()) {
00117             *data_buf = DATA.getc();
00118             data_buf+=1;
00119             count++;
00120         }
00121 
00122     }
00123 }
00124 
00125 int xbee::SetPanId(int pan_id)
00126 {
00127     Serial DATA(_tx,_rx);
00128     wait_ms(5);
00129     DATA.printf("ATID %i\r",pan_id);
00130     DATA.scanf ("%*s");
00131     return 1;
00132 }
00133 
00134 void xbee::Reset()
00135 {
00136     DigitalOut rst(_reset);
00137     rst = 0;
00138     wait_ms(10);
00139     rst = 1;
00140     wait_ms(1);
00141 }
00142 
00143