mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
xbee: Initial implementation
This is a basic serial module that allows access to various XBee devices via a UART port. It was tested with the XBee S6B WiFi Module and the XBee S1 802.14.4 module. Signed-off-by: Jon Trulson <jtrulson@ics.com> Signed-off-by: Abhishek Malik <abhishek.malik@intel.com>
This commit is contained in:
parent
0d2541270e
commit
6e095826d3
@ -147,6 +147,7 @@ add_executable (sm130-example sm130.cxx)
|
||||
add_executable (grovegprs-example grovegprs.cxx)
|
||||
add_executable (lm35-example lm35.cxx)
|
||||
add_executable (micsv89-example micsv89.cxx)
|
||||
add_executable (xbee-example xbee.cxx)
|
||||
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/grove)
|
||||
@ -259,6 +260,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/sm130)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/grovegprs)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/lm35)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/micsv89)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/xbee)
|
||||
|
||||
target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
|
||||
@ -407,3 +409,4 @@ target_link_libraries (sm130-example sm130 ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (grovegprs-example grovegprs ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (lm35-example lm35 ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (micsv89-example micsv89 ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (xbee-example xbee ${CMAKE_THREAD_LIBS_INIT})
|
||||
|
145
examples/c++/xbee.cxx
Normal file
145
examples/c++/xbee.cxx
Normal file
@ -0,0 +1,145 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2015 Intel Corporation.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include "xbee.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace upm;
|
||||
|
||||
void printUsage(char *progname)
|
||||
{
|
||||
cout << "Usage: " << progname << " [AT command]" << endl;
|
||||
cout << endl;
|
||||
|
||||
cout << "If an argument is supplied on the command line, that argument is"
|
||||
<< endl;
|
||||
cout << "sent to the module and the response is printed out." << endl;
|
||||
cout << endl;
|
||||
cout << "If no argument is used, then the firmware revision, serial number"
|
||||
<< endl;
|
||||
cout << "and the current IP address (if set) are queried." << endl;
|
||||
cout << endl;
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
//! [Interesting]
|
||||
|
||||
// simple helper function to send a command and wait for a response
|
||||
void sendCommand(upm::XBee* sensor, string cmd)
|
||||
{
|
||||
// commands need to be terminated with a carriage return
|
||||
cmd += "\r";
|
||||
|
||||
sensor->writeDataStr(cmd);
|
||||
|
||||
string resp;
|
||||
while (sensor->dataAvailable(2000))
|
||||
{
|
||||
resp += sensor->readDataStr(1024);
|
||||
}
|
||||
|
||||
if (resp.empty())
|
||||
{
|
||||
cerr << "Timed out waiting for response" << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
resp = sensor->stringCR2LF(resp);
|
||||
cout << "Returned (" << resp.size() << " bytes): " << endl;
|
||||
cout << resp << endl;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
// Instantiate a XBee Module on UART 0
|
||||
upm::XBee* sensor = new upm::XBee(0);
|
||||
|
||||
// Set the baud rate, 9600 baud is the default.
|
||||
if (sensor->setBaudRate(9600))
|
||||
{
|
||||
cerr << "Failed to set tty baud rate" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
printUsage(argv[0]);
|
||||
|
||||
if (argc > 1)
|
||||
{
|
||||
// enable command mode
|
||||
sensor->commandMode();
|
||||
cout << "Sending command line argument (" << argv[1] << ")..." << endl;
|
||||
sendCommand(sensor, argv[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
// enable command mode
|
||||
sensor->commandMode();
|
||||
// query the verbose firmware revision
|
||||
cout << "Querying verbose firmware revision (ATVL)..." << endl;
|
||||
sendCommand(sensor, "ATVL");
|
||||
// query the number
|
||||
cout << "Querying Serial Number High (ATSH)..." << endl;
|
||||
sendCommand(sensor, "ATSH");
|
||||
cout << "Querying Serial Number Low (ATSL)..." << endl;
|
||||
sendCommand(sensor, "ATSL");
|
||||
|
||||
cout << "Querying address, if set (ATMY)..." << endl;
|
||||
sendCommand(sensor, "ATMY");
|
||||
|
||||
// For the XBee WiFi S6B
|
||||
// A comprehensive list of commands and command modes is
|
||||
// available from the datasheet at:
|
||||
// ftp1.digi.com/support/documentation/90002180_L.pdf
|
||||
|
||||
// For the XBee S1
|
||||
// A comprehensive list of commands and command modes is
|
||||
// available from the datasheet at:
|
||||
// http://www.sparkfun.com/datasheets/Wireless/Zigbee/XBee-Datasheet.pdf
|
||||
|
||||
// For the XBee WiFi module:
|
||||
// An example using AT commands to connect to an AP, with a
|
||||
// private Key using WPA2:
|
||||
|
||||
// Connect to AP with SSID 'mySSID':
|
||||
// ATIDmySSID
|
||||
|
||||
// Provide the private key 'secret':
|
||||
// ATPKsecret
|
||||
|
||||
// Use WPA2 encryption
|
||||
// ATEE2
|
||||
|
||||
}
|
||||
|
||||
|
||||
delete sensor;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//! [Interesting]
|
140
examples/javascript/xbee.js
Normal file
140
examples/javascript/xbee.js
Normal file
@ -0,0 +1,140 @@
|
||||
/*jslint node:true, vars:true, bitwise:true, unparam:true */
|
||||
/*jshint unused:true */
|
||||
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2015 Intel Corporation.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
var sensorObj = require('jsupm_xbee');
|
||||
|
||||
|
||||
/************** Functions **************/
|
||||
function printUsage(progname)
|
||||
{
|
||||
var outputStr = "Usage: " + progname + " [AT command]\n\n" +
|
||||
"If an argument is supplied on the command line, that argument is\n" +
|
||||
"sent to the module and the response is printed out.\n\n" +
|
||||
"If no argument is used, then the firmware revision, serial number\n" +
|
||||
"and the current IP address (if set) are queried.\n\n"
|
||||
console.log(outputStr);
|
||||
}
|
||||
|
||||
// simple helper function to send a command and wait for a response
|
||||
function sendCommand(sensor, cmd)
|
||||
{
|
||||
// commands need to be terminated with a carriage return
|
||||
cmd += "\r";
|
||||
sensor.writeDataStr(cmd);
|
||||
|
||||
var resp = "";
|
||||
while (sensor.dataAvailable(2000))
|
||||
{
|
||||
resp += sensor.readDataStr(1024);
|
||||
}
|
||||
|
||||
if (!resp)
|
||||
{
|
||||
console.log("Timed out waiting for response");
|
||||
}
|
||||
else
|
||||
{
|
||||
resp = sensor.stringCR2LF(resp);
|
||||
console.log("Returned (" + resp.length + " bytes):");
|
||||
console.log(resp);
|
||||
}
|
||||
}
|
||||
|
||||
/************** Main code **************/
|
||||
// Instantiate a XBEE Module on UART 0
|
||||
var sensor = new sensorObj.XBee(0);
|
||||
|
||||
// Set the baud rate, 9600 baud is the default.
|
||||
if (sensor.setBaudRate(9600))
|
||||
{
|
||||
console.log("Failed to set baud rate");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
printUsage(process.argv[1]);
|
||||
|
||||
// Note: in nodeJS, command-line argument 0 is "node".
|
||||
// Command-line argument 1 is "xbee.js"
|
||||
// If you have a third argument, then it's a command
|
||||
if (process.argv.length > 2)
|
||||
{
|
||||
// enable command mode
|
||||
sensor.commandMode();
|
||||
console.log("Sending command line argument (" + process.argv[2] + ")...");
|
||||
sendCommand(sensor, process.argv[2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
// enable command mode
|
||||
sensor.commandMode();
|
||||
// query the verbose firmware revision
|
||||
console.log("Querying verbose firmware revision (ATVL)...");
|
||||
sendCommand(sensor, "ATVL");
|
||||
// query the number
|
||||
console.log("Querying Serial Number High (ATSH)...");
|
||||
sendCommand(sensor, "ATSH");
|
||||
console.log("Querying Serial Number Low (ATSL)...");
|
||||
sendCommand(sensor, "ATSL");
|
||||
|
||||
console.log("Querying address, if set (ATMY)...");
|
||||
sendCommand(sensor, "ATMY");
|
||||
|
||||
// For the XBee WiFi S6B
|
||||
// A comprehensive list of commands and command modes is
|
||||
// available from the datasheet at:
|
||||
// ftp1.digi.com/support/documentation/90002180_L.pdf
|
||||
|
||||
// For the XBee S1
|
||||
// A comprehensive list of commands and command modes is
|
||||
// available from the datasheet at:
|
||||
// http://www.sparkfun.com/datasheets/Wireless/Zigbee/XBee-Datasheet.pdf
|
||||
|
||||
// For the XBee WiFi module:
|
||||
// An example using AT commands to connect to an AP, with a
|
||||
// private Key using WPA2:
|
||||
|
||||
// Connect to AP with SSID 'mySSID':
|
||||
// ATIDmySSID
|
||||
|
||||
// Provide the private key 'secret':
|
||||
// ATPKsecret
|
||||
|
||||
// Use WPA2 encryption
|
||||
// ATEE2
|
||||
}
|
||||
|
||||
|
||||
/************** Exit code **************/
|
||||
process.on('SIGINT', function()
|
||||
{
|
||||
sensor = null;
|
||||
sensorObj.cleanUp();
|
||||
sensorObj = null;
|
||||
console.log("Exiting...");
|
||||
process.exit(0);
|
||||
});
|
117
examples/python/xbee.py
Normal file
117
examples/python/xbee.py
Normal file
@ -0,0 +1,117 @@
|
||||
#!/usr/bin/python
|
||||
# Author: Jon Trulson <jtrulson@ics.com>
|
||||
# Copyright (c) 2015 Intel Corporation.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining
|
||||
# a copy of this software and associated documentation files (the
|
||||
# "Software"), to deal in the Software without restriction, including
|
||||
# without limitation the rights to use, copy, modify, merge, publish,
|
||||
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||
# permit persons to whom the Software is furnished to do so, subject to
|
||||
# the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be
|
||||
# included in all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
import time, sys, signal, atexit
|
||||
import pyupm_xbee as sensorObj
|
||||
|
||||
# Instantiate a XBee Module on UART 0
|
||||
sensor = sensorObj.XBee(0)
|
||||
|
||||
## Exit handlers ##
|
||||
# This stops python from printing a stacktrace when you hit control-C
|
||||
def SIGINTHandler(signum, frame):
|
||||
raise SystemExit
|
||||
|
||||
# This function lets you run code on exit
|
||||
def exitHandler():
|
||||
print "Exiting"
|
||||
sys.exit(0)
|
||||
|
||||
# Register exit handlers
|
||||
atexit.register(exitHandler)
|
||||
signal.signal(signal.SIGINT, SIGINTHandler)
|
||||
|
||||
# Set the baud rate, 9600 baud is the default.
|
||||
if (sensor.setBaudRate(9600)):
|
||||
print "Failed to set baud rate"
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
usageStr = ("Usage:\n"
|
||||
"If an argument is supplied on the command line, that argument is\n"
|
||||
"sent to the module and the response is printed out.\n\n"
|
||||
"If no argument is used, then the firmware revision, serial number\n"
|
||||
"and the current IP address (if set) are queried.\n\n")
|
||||
print usageStr
|
||||
|
||||
# simple helper function to send a command and wait for a response
|
||||
def sendCommand(sensor, cmd):
|
||||
# commands need to be terminated with a carriage return
|
||||
cmd += "\r"
|
||||
sensor.writeDataStr(cmd)
|
||||
|
||||
resp = ""
|
||||
while sensor.dataAvailable(2000):
|
||||
resp += sensor.readDataStr(1024)
|
||||
|
||||
if not resp:
|
||||
print "Timed out waiting for response"
|
||||
else:
|
||||
resp = sensor.stringCR2LF(resp)
|
||||
print "Returned (", len(resp), "bytes):"
|
||||
print resp
|
||||
|
||||
|
||||
if (len(sys.argv) > 1):
|
||||
# enable command mode
|
||||
sensor.commandMode()
|
||||
print "Sending command line argument (" + sys.argv[1] + ")..."
|
||||
sendCommand(sensor, sys.argv[1])
|
||||
else:
|
||||
# enable command mode
|
||||
sensor.commandMode()
|
||||
# query the verbose firmware revision
|
||||
print "Querying verbose firmware revision (ATVL)..."
|
||||
sendCommand(sensor, "ATVL")
|
||||
# query the number
|
||||
print "Querying Serial Number High (ATSH)..."
|
||||
sendCommand(sensor, "ATSH")
|
||||
print "Querying Serial Number Low (ATSL)..."
|
||||
sendCommand(sensor, "ATSL")
|
||||
|
||||
print "Querying address, if set (ATMY)..."
|
||||
sendCommand(sensor, "ATMY");
|
||||
|
||||
# For the XBee WiFi S6B
|
||||
# A comprehensive list of commands and command modes is
|
||||
# available from the datasheet at:
|
||||
# ftp1.digi.com/support/documentation/90002180_L.pdf
|
||||
|
||||
# For the XBee S1
|
||||
# A comprehensive list of commands and command modes is
|
||||
# available from the datasheet at:
|
||||
# http://www.sparkfun.com/datasheets/Wireless/Zigbee/XBee-Datasheet.pdf
|
||||
|
||||
# For the XBee WiFi module:
|
||||
# An example using AT commands to connect to an AP, with a
|
||||
# private Key using WPA2:
|
||||
|
||||
# Connect to AP with SSID 'mySSID':
|
||||
# ATIDmySSID
|
||||
|
||||
# Provide the private key 'secret':
|
||||
# ATPKsecret
|
||||
|
||||
# Use WPA2 encryption
|
||||
# ATEE2
|
||||
|
5
src/xbee/CMakeLists.txt
Normal file
5
src/xbee/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
||||
set (libname "xbee")
|
||||
set (libdescription "upm XBee serial module")
|
||||
set (module_src ${libname}.cxx)
|
||||
set (module_h ${libname}.h)
|
||||
upm_module_init()
|
11
src/xbee/javaupm_xbee.i
Normal file
11
src/xbee/javaupm_xbee.i
Normal file
@ -0,0 +1,11 @@
|
||||
%module javaupm_xbee
|
||||
%include "../upm.i"
|
||||
%include "carrays.i"
|
||||
%include "std_string.i"
|
||||
|
||||
%{
|
||||
#include "xbee.h"
|
||||
%}
|
||||
|
||||
%include "xbee.h"
|
||||
%array_class(char, charArray);
|
11
src/xbee/jsupm_xbee.i
Normal file
11
src/xbee/jsupm_xbee.i
Normal file
@ -0,0 +1,11 @@
|
||||
%module jsupm_xbee
|
||||
%include "../upm.i"
|
||||
%include "carrays.i"
|
||||
%include "std_string.i"
|
||||
|
||||
%{
|
||||
#include "xbee.h"
|
||||
%}
|
||||
|
||||
%include "xbee.h"
|
||||
%array_class(char, charArray);
|
12
src/xbee/pyupm_xbee.i
Normal file
12
src/xbee/pyupm_xbee.i
Normal file
@ -0,0 +1,12 @@
|
||||
%module pyupm_xbee
|
||||
%include "../upm.i"
|
||||
%include "carrays.i"
|
||||
%include "std_string.i"
|
||||
|
||||
%feature("autodoc", "3");
|
||||
|
||||
%{
|
||||
#include "xbee.h"
|
||||
%}
|
||||
%include "xbee.h"
|
||||
%array_class(char, charArray);
|
102
src/xbee/xbee.cxx
Normal file
102
src/xbee/xbee.cxx
Normal file
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2015 Intel Corporation.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <time.h>
|
||||
|
||||
#include "xbee.h"
|
||||
|
||||
using namespace upm;
|
||||
using namespace std;
|
||||
|
||||
static const int maxBuffer = 1024;
|
||||
|
||||
XBee::XBee(int uart) :
|
||||
m_uart(uart)
|
||||
{
|
||||
}
|
||||
|
||||
XBee::~XBee()
|
||||
{
|
||||
}
|
||||
|
||||
bool XBee::dataAvailable(unsigned int millis)
|
||||
{
|
||||
return m_uart.dataAvailable(millis);
|
||||
}
|
||||
|
||||
int XBee::readData(char *buffer, unsigned int len)
|
||||
{
|
||||
return m_uart.read(buffer, len);
|
||||
}
|
||||
|
||||
std::string XBee::readDataStr(int len)
|
||||
{
|
||||
return m_uart.readStr(len);
|
||||
}
|
||||
|
||||
int XBee::writeData(char *buffer, unsigned int len)
|
||||
{
|
||||
m_uart.flush();
|
||||
return m_uart.write(buffer, len);
|
||||
}
|
||||
|
||||
int XBee::writeDataStr(std::string data)
|
||||
{
|
||||
m_uart.flush();
|
||||
return m_uart.writeStr(data);
|
||||
}
|
||||
|
||||
mraa::Result XBee::setBaudRate(int baud)
|
||||
{
|
||||
return m_uart.setBaudRate(baud);
|
||||
}
|
||||
|
||||
bool XBee::commandMode(std::string cmdChars, int guardTimeMS)
|
||||
{
|
||||
|
||||
usleep(guardTimeMS * 1000);
|
||||
|
||||
writeDataStr(cmdChars);
|
||||
|
||||
usleep(guardTimeMS * 1000);
|
||||
|
||||
string resp;
|
||||
if (dataAvailable(1000))
|
||||
resp = readDataStr(maxBuffer);
|
||||
|
||||
if (resp.find("OK"))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
string XBee::stringCR2LF(string str)
|
||||
{
|
||||
for (int i=0; i<str.size(); i++)
|
||||
if (str[i] == '\r')
|
||||
str[i] = '\n';
|
||||
|
||||
return str;
|
||||
}
|
188
src/xbee/xbee.h
Normal file
188
src/xbee/xbee.h
Normal file
@ -0,0 +1,188 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2015 Intel Corporation.
|
||||
*
|
||||
* Thanks to Adafruit for supplying a google translated version of the
|
||||
* Chinese datasheet and some clues in their code.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <mraa/common.hpp>
|
||||
#include <mraa/uart.hpp>
|
||||
|
||||
#define XBEE_DEFAULT_UART 0
|
||||
|
||||
namespace upm {
|
||||
/**
|
||||
* @brief XBee modules
|
||||
* @defgroup xbee libupm-xbee
|
||||
* @ingroup sparkfun uart wifi
|
||||
*/
|
||||
|
||||
/**
|
||||
* @library xbee
|
||||
* @sensor xbee
|
||||
* @comname XBee modules
|
||||
* @type wifi
|
||||
* @man sparkfun
|
||||
* @con uart
|
||||
* @web https://www.sparkfun.com/products/11215
|
||||
*
|
||||
* @brief API for the XBee modules
|
||||
*
|
||||
* This is a generic UART driver for use with Digi XBee modules.
|
||||
* It was tested with the XBee S1 802.15.4 module and the XBee S6B
|
||||
* WiFi module.
|
||||
*
|
||||
* It provides basic UART support for sending and receiving data
|
||||
* to and from the device. It is controlled by an AT or API
|
||||
* command set.
|
||||
*
|
||||
* It is connected at 9600 baud by default.
|
||||
*
|
||||
* These devices are typically configured using Digi's X-CTU
|
||||
* windows software, however it is possible of course to configure
|
||||
* them manually using AT commands. See the examples.
|
||||
*
|
||||
* @snippet xbee.cxx Interesting
|
||||
*/
|
||||
|
||||
class XBee {
|
||||
public:
|
||||
|
||||
/**
|
||||
* XBee object constructor
|
||||
*
|
||||
* @param uart Default UART to use (0 or 1). Default is 0.
|
||||
*/
|
||||
XBee(int uart=XBEE_DEFAULT_UART);
|
||||
|
||||
/**
|
||||
* XBee object destructor
|
||||
*/
|
||||
~XBee();
|
||||
|
||||
/**
|
||||
* Checks to see if there is data available for reading
|
||||
*
|
||||
* @param millis Number of milliseconds to wait; 0 means no waiting
|
||||
* @return true if there is data available for reading
|
||||
*/
|
||||
bool dataAvailable(unsigned int millis);
|
||||
|
||||
/**
|
||||
* Reads any available data into a user-supplied buffer. Note: the
|
||||
* call blocks until data is available for reading. Use
|
||||
* dataAvailable() to determine whether there is data available
|
||||
* beforehand, to avoid blocking.
|
||||
*
|
||||
* @param buffer Buffer to hold the data read
|
||||
* @param len Length of the buffer
|
||||
* @return Number of bytes read
|
||||
*/
|
||||
int readData(char *buffer, unsigned int len);
|
||||
|
||||
/**
|
||||
* Reads any available data and returns it in a std::string. Note:
|
||||
* the call blocks until data is available for reading. Use
|
||||
* dataAvailable() to determine whether there is data available
|
||||
* beforehand, to avoid blocking.
|
||||
*
|
||||
* @param len Maximum length of the data to be returned
|
||||
* @return string containing the data read
|
||||
*/
|
||||
std::string readDataStr(int len);
|
||||
|
||||
/**
|
||||
* Writes the data in the buffer to the device. If you are
|
||||
* writing an AT command, be sure to terminate it with a carriage
|
||||
* return (\r)
|
||||
*
|
||||
* @param buffer Buffer to hold the data to write
|
||||
* @param len Length of the buffer
|
||||
* @return Number of bytes written
|
||||
*/
|
||||
int writeData(char *buffer, unsigned len);
|
||||
|
||||
/**
|
||||
* Writes the std:string data to the device. If you are writing an
|
||||
* AT command, be sure to terminate it with a carriage return (\r)
|
||||
*
|
||||
* @param data Buffer to write to the device
|
||||
* @return Number of bytes written
|
||||
*/
|
||||
int writeDataStr(std::string data);
|
||||
|
||||
/**
|
||||
* Sets the baud rate for the device. The default is 9600.
|
||||
*
|
||||
* @param baud Desired baud rate
|
||||
* @return true if successful
|
||||
*/
|
||||
mraa::Result setBaudRate(int baud=9600);
|
||||
|
||||
/**
|
||||
* Attempts to enter AT Command Mode. When Idle, data sent to the
|
||||
* device (assuming it is not in API mode) is silently transmitted
|
||||
* to the configured destination. Running this command attempts
|
||||
* to place the device into command mode, allowing you to send AT
|
||||
* commands. Note, after a configurable period of inactivity, the
|
||||
* device will exit command mode automatically (default 10
|
||||
* seconds).
|
||||
*
|
||||
* Both the cmdChars (+++) and the Guard Time can be configured
|
||||
* on the device to different values using AT configuration
|
||||
* commands.
|
||||
*
|
||||
* @param cmdChars The command mode characters, default "+++"
|
||||
* @param guardTimeMS The number of milliseconds to wait before and
|
||||
* after sending the command characters. Default is 1000 (1 second).
|
||||
* @return true if successful (received an "OK"), false otherwise
|
||||
*/
|
||||
bool commandMode(std::string cmdChars="+++", int guardTimeMS=1000);
|
||||
|
||||
/**
|
||||
* This is a convenience method that converts each CR (\r) in a
|
||||
* string to a LF (\n) and returns it. This is useful for
|
||||
* outputting the response to an AT command for instance, which is
|
||||
* typically CR terminated.
|
||||
*
|
||||
* @param str The string to convert
|
||||
* @return The converted string
|
||||
*/
|
||||
std::string stringCR2LF(std::string str);
|
||||
|
||||
protected:
|
||||
mraa::Uart m_uart;
|
||||
|
||||
private:
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user