|
| 1 | +/** |
| 2 | + * Copyright (c) 2009 Andrew Rapp. All rights reserved. |
| 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 <SoftwareSerial.h> |
| 22 | + |
| 23 | +/* |
| 24 | +This example is for Series 2 XBee |
| 25 | +Receives a ZB RX packet and prints the packet to softserial |
| 26 | +*/ |
| 27 | + |
| 28 | +XBee xbee = XBee(); |
| 29 | +XBeeResponse response = XBeeResponse(); |
| 30 | +// create reusable response objects for responses we expect to handle |
| 31 | +ZBRxResponse rx = ZBRxResponse(); |
| 32 | +ModemStatusResponse msr = ModemStatusResponse(); |
| 33 | + |
| 34 | +// Define NewSoftSerial TX/RX pins |
| 35 | +// Connect Arduino pin 8 to TX of usb-serial device |
| 36 | +uint8_t ssRX = 8; |
| 37 | +// Connect Arduino pin 9 to RX of usb-serial device |
| 38 | +uint8_t ssTX = 9; |
| 39 | +// Remember to connect all devices to a common Ground: XBee, Arduino and USB-Serial device |
| 40 | +SoftwareSerial nss(ssRX, ssTX); |
| 41 | + |
| 42 | + |
| 43 | +void setup() { |
| 44 | + // start serial |
| 45 | + Serial.begin(9600); |
| 46 | + xbee.setSerial(Serial); |
| 47 | + nss.begin(9600); |
| 48 | + |
| 49 | + nss.println("Starting up!"); |
| 50 | +} |
| 51 | + |
| 52 | +// continuously reads packets, looking for ZB Receive or Modem Status |
| 53 | +void loop() { |
| 54 | + |
| 55 | + xbee.readPacket(); |
| 56 | + |
| 57 | + if (xbee.getResponse().isAvailable()) { |
| 58 | + // got something |
| 59 | + |
| 60 | + if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) { |
| 61 | + // got a zb rx packet |
| 62 | + |
| 63 | + // now fill our zb rx class |
| 64 | + xbee.getResponse().getZBRxResponse(rx); |
| 65 | + |
| 66 | + nss.println("Got an rx packet!"); |
| 67 | + |
| 68 | + if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) { |
| 69 | + // the sender got an ACK |
| 70 | + nss.println("packet acknowledged"); |
| 71 | + } else { |
| 72 | + nss.println("packet not acknowledged"); |
| 73 | + } |
| 74 | + |
| 75 | + nss.print("checksum is "); |
| 76 | + nss.println(rx.getChecksum(), HEX); |
| 77 | + |
| 78 | + nss.print("packet length is "); |
| 79 | + nss.println(rx.getPacketLength(), DEC); |
| 80 | + |
| 81 | + for (int i = 0; i < rx.getDataLength(); i++) { |
| 82 | + nss.print("payload ["); |
| 83 | + nss.print(i, DEC); |
| 84 | + nss.print("] is "); |
| 85 | + nss.println(rx.getData()[i], HEX); |
| 86 | + } |
| 87 | + |
| 88 | + for (int i = 0; i < xbee.getResponse().getFrameDataLength(); i++) { |
| 89 | + nss.print("frame data ["); |
| 90 | + nss.print(i, DEC); |
| 91 | + nss.print("] is "); |
| 92 | + nss.println(xbee.getResponse().getFrameData()[i], HEX); |
| 93 | + } |
| 94 | + } |
| 95 | + } else if (xbee.getResponse().isError()) { |
| 96 | + nss.print("oh no!!! error code:"); |
| 97 | + nss.println(xbee.getResponse().getErrorCode()); |
| 98 | + } |
| 99 | +} |
0 commit comments