Skip to content

Commit 27fcc03

Browse files
Add Echo_Callbacks example
This is an example that receives packets and echoes them back to the sender. It uses the new callbacks to show their usage.
1 parent 470bff4 commit 27fcc03

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* Copyright (c) 2015 Matthijs Kooijman
3+
*
4+
* This file is part of XBee-Arduino.
5+
*
6+
* XBee-Arduino is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* XBee-Arduino is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with XBee-Arduino. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include <XBee.h>
21+
#include <Printers.h>
22+
23+
/*
24+
This example is for Series 1 and 2 XBee (no changes needed for either).
25+
26+
It listens for incoming packets and echoes back any data received back
27+
to the sender. This example shows how to use XBeeWithCallbacks to
28+
concisely express reading of response packets.
29+
30+
This example assumes an Arduino with two serial ports (like the
31+
Leonardo or Mega). Replace Serial and Serial1 below appropriately for
32+
your hardware.
33+
*/
34+
35+
// create the XBee object
36+
XBeeWithCallbacks xbee;
37+
38+
void zbReceive(ZBRxResponse& rx, uintptr_t) {
39+
// Create a reply packet containing the same data
40+
// This directly reuses the rx data array, which is ok since the tx
41+
// packet is sent before any new response is received
42+
ZBTxRequest tx;
43+
tx.setAddress64(rx.getRemoteAddress64());
44+
tx.setAddress16(rx.getRemoteAddress16());
45+
tx.setPayload(rx.getFrameData() + rx.getDataOffset(), rx.getDataLength());
46+
47+
// Send the reply, but do not wait for the tx status reply. If an
48+
// error occurs, the global onTxStatusResponse handler will print an
49+
// error message, but no message is printed on succes.
50+
xbee.send(tx);
51+
Serial.println(F("Sending ZBTxRequest"));
52+
}
53+
54+
void receive16(Rx16Response& rx, uintptr_t) {
55+
// Create a reply packet containing the same data
56+
// This directly reuses the rx data array, which is ok since the tx
57+
// packet is sent before any new response is received
58+
Tx16Request tx;
59+
tx.setAddress16(rx.getRemoteAddress16());
60+
tx.setPayload(rx.getFrameData() + rx.getDataOffset(), rx.getDataLength());
61+
62+
// Send the reply, but do not wait for the tx status reply. If an
63+
// error occurs, the global onTxStatusResponse handler will print an
64+
// error message, but no message is printed on succes.
65+
xbee.send(tx);
66+
Serial.println(F("Sending Tx16Request"));
67+
}
68+
69+
void receive64(Rx64Response& rx, uintptr_t) {
70+
// Create a reply packet containing the same data
71+
// This directly reuses the rx data array, which is ok since the tx
72+
// packet is sent before any new response is received
73+
Tx64Request tx;
74+
tx.setAddress64(rx.getRemoteAddress64());
75+
tx.setPayload(rx.getFrameData() + rx.getDataOffset(), rx.getDataLength());
76+
77+
// Send the reply, but do not wait for the tx status reply. If an
78+
// error occurs, the global onTxStatusResponse handler will print an
79+
// error message, but no message is printed on succes.
80+
xbee.send(tx);
81+
Serial.println(F("Sending Tx64Request"));
82+
}
83+
void setup() {
84+
Serial.begin(9600);
85+
86+
Serial1.begin(9600);
87+
xbee.setSerial(Serial1);
88+
89+
// Make sure that any errors are logged to Serial. The address of
90+
// Serial is first cast to Print*, since that's what the callback
91+
// expects, and then to uintptr_t to fit it inside the data parameter.
92+
xbee.onPacketError(printErrorCb, (uintptr_t)(Print*)&Serial);
93+
xbee.onTxStatusResponse(printErrorCb, (uintptr_t)(Print*)&Serial);
94+
xbee.onZBTxStatusResponse(printErrorCb, (uintptr_t)(Print*)&Serial);
95+
96+
// These are called when an actual packet received
97+
xbee.onZBRxResponse(zbReceive);
98+
xbee.onRx16Response(receive16);
99+
xbee.onRx64Response(receive64);
100+
101+
// Print any unhandled response with proper formatting
102+
xbee.onOtherResponse(printResponseCb, (uintptr_t)(Print*)&Serial);
103+
104+
// Enable this to print the raw bytes for _all_ responses before they
105+
// are handled
106+
//xbee.onResponse(printRawResponseCb, (uintptr_t)(Print*)&Serial);
107+
108+
// Set AO=0 to make sure we get ZBRxResponses, not
109+
// ZBExplicitRxResponses (only supported on series2). This probably
110+
// isn't needed, but nicely shows how to use sendAndWait().
111+
uint8_t value = 0;
112+
AtCommandRequest req((uint8_t*)"AO", &value, sizeof(value));
113+
req.setFrameId(xbee.getNextFrameId());
114+
// Send the command and wait up to 150ms for a response
115+
uint8_t status = xbee.sendAndWait(req, 150);
116+
if (status == 0)
117+
Serial.println(F("Set AO=0"));
118+
else
119+
Serial.println(F("Failed to set AO (this is expected on series1)"));
120+
}
121+
122+
void loop() {
123+
// Continuously let xbee read packets and call callbacks.
124+
xbee.loop();
125+
}
126+

0 commit comments

Comments
 (0)