-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexmaple.js
54 lines (47 loc) · 1.74 KB
/
exmaple.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) 2015 Randy Westlund, All rights reserved.
// This code is under the BSD-2-Clause license.
'use strict';
var Xbee = require('./digimesh');
// connect to xbee
var xbee = new Xbee({ device: '/dev/ttyU0', baud: 115200 }, function() {
console.log('xbee is ready');
console.log('getting node identifier...');
// ask for node identifier string
xbee.get_ni_string(function(err, data) {
if (err) return console.err(err);
console.log("my NI is '" + data.ni + "'");
console.log('scanning for nodes on the network...');
xbee.discover_nodes(function(err, nodes) {
if (err) return console.err(err);
console.log('%d nodes found:', nodes.length);
console.dir(nodes);
// if we found anyone
if (nodes.length) {
console.log("saying 'hello' to node %s...", nodes[0].addr);
xbee.send_message({
data: new Buffer("hello"),
addr: nodes[0].addr,
broadcast: false,
},
// callback
function(err, data) {
if (err) return console.error(err);
// print the string status message for the status we got back
console.log('delivery status: %s',
xbee.DELIVERY_STATUS_STRINGS[data.status]);
console.dir(data);
console.log('goodbye');
process.exit(0);
});
}
});
});
});
xbee.on('error', function(err) {
console.error(err);
process.exit(1);
});
xbee.on('message_received', function(data) {
console.log('received a message!');
console.dir(data);
});