Skip to content

Instantly share code, notes, and snippets.

@mozz100
Last active April 16, 2016 21:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mozz100/7649780 to your computer and use it in GitHub Desktop.
Save mozz100/7649780 to your computer and use it in GitHub Desktop.
Node.js, xbee and promises
var SerialPort = require('serialport').SerialPort;
var xbee_api = require('xbee-api');
var C = xbee_api.constants;
var Q = require('q');
// following settings work for me on my Raspberry pi, your config may differ!
var xbeeAPI = new xbee_api.XBeeAPI({
api_mode: 1
});
var serialport = new SerialPort("/dev/ttyAMA0", {
baudrate: 9600,
parser: xbeeAPI.rawParser()
});
// How long are we prepared to wait for a response to our command?
var maxWait = 5000; // ms
function xbeeCommand(frame) {
// set frame id
frame.id = xbeeAPI.nextFrameId();
// We're going to return a promise
var deferred = Q.defer();
var callback = function(receivedFrame) {
if (receivedFrame.id == frame.id) {
// This is our frame's response. Resolve the promise.
deferred.resolve(receivedFrame);
}
};
// Attach callback so we're waiting for the response
xbeeAPI.on("frame_object", callback);
// Clear up: remove listener after the timeout and a bit, it's no longer needed
setTimeout(function() {
xbeeAPI.removeListener("frame_object", callback);
}, maxWait + 1000);
// Pass the bytes down the serial port
serialport.write(xbeeAPI.buildFrame(frame));
// Return our promise with a timeout
return deferred.promise.timeout(maxWait);
}
xbeeCommand({
type: C.FRAME_TYPE.REMOTE_AT_COMMAND_REQUEST,
destination64: "0013a2004086a04a",
command: "DB",
commandParameter: [],
}).then(function(f) {
console.log("Command successful:", f);
}).catch(function(e) {
console.log("Command failed:", e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment