I'm trying to read the payload sent from an Arduino Fio to an Arduino Uno using two Series 1 XBees. The XBee library makes it seem simple enough to use getData(0), but for some reason it seems to send me the API ID (126) instead of the first item in the payload (0 or 1 depending on button press). The RSSI value seems to be accurate, but not getData(). I've stared at this for a while and can't figure out what I'm doing wrong.
I've attached my XBee configurations and here's my code:
Arduino Fio (Tx):
/*******************
**** XBee Setup ****
*******************/
#include <XBee.h>
XBee xbee = XBee();
unsigned long start = millis();
//Create reusable response objects for responses we expect to handle
Rx16Response rx16 = Rx16Response();
// allocate two bytes for to hold a 10-bit analog reading
TxStatusResponse txStatus = TxStatusResponse();
uint8_t payload[] = { 0, 0 };
/*********************
**** Switch Setup ****
*********************/
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
int lastButtonState = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP);
//serial
Serial.begin(57600);
xbee.setSerial(Serial);
flashLed(ledPin, 2, 50);
}
void loop() {
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState != lastButtonState){
payload[0] = 0x1;
Tx16Request tx = Tx16Request(0x1874, payload, sizeof(payload));
xbee.send(tx);
digitalWrite(ledPin, HIGH);
}
else {
payload[0] = 0x0;
Tx16Request tx = Tx16Request(0x1874, payload, sizeof(payload));
xbee.send(tx);
digitalWrite(ledPin, LOW);
}
delay(1000);
}
void flashLed(int pin, int times, int wait) {
for (int i = 0; i < times; i++) {
digitalWrite(pin, HIGH);
delay(wait);
digitalWrite(pin, LOW);
if (i + 1 < times) {
delay(wait);
}
}
}
Arduino Uno (Rx):
/**
* Copyright (c) 2009 Andrew Rapp. All rights reserved.
*
* This file is part of XBee-Arduino.
*
* XBee-Arduino is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* XBee-Arduino is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBee-Arduino. If not, see <http://www.gnu.org/licenses/>.
*/
#include <XBee.h>
#include <SoftwareSerial.h>
/*
This example is for Series 1 XBee (802.15.4)
Receives either a RX16 or RX64 packet and sets a PWM value based on packet data.
Error led is flashed if an unexpected packet is received
*/
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
SoftwareSerial xbeeSerial(2, 3);
// create reusable response objects for responses we expect to handle
Rx16Response rx16 = Rx16Response();
Rx64Response rx64 = Rx64Response();
int statusLed = 13;
int errorLed = 12;
int dataLed = 10;
uint8_t rssi = 0;
uint8_t data = 0;
void flashLed(int pin, int times, int wait) {
for (int i = 0; i < times; i++) {
digitalWrite(pin, HIGH);
delay(wait);
digitalWrite(pin, LOW);
if (i + 1 < times) {
delay(wait);
}
}
}
void setup() {
pinMode(statusLed, OUTPUT);
pinMode(errorLed, OUTPUT);
pinMode(dataLed, OUTPUT);
// start serial
Serial.begin(57600);
xbee.setSerial(Serial);
flashLed(statusLed, 3, 50);
}
// continuously reads packets, looking for RX16 or RX64
void loop() {
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
if (xbee.getResponse().getApiId() == RX_16_RESPONSE || xbee.getResponse().getApiId() == RX_64_RESPONSE) {
// got a rx packet
if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
xbee.getResponse().getRx16Response(rx16);
rssi = rx16.getRssi();
data = rx16.getData(0);
Serial.print("RSSI: ");
Serial.print(rssi);
Serial.println();
Serial.print("Data: ");
Serial.print(data);
Serial.println();
} else {
xbee.getResponse().getRx64Response(rx64);
rssi = rx64.getRssi();
data = rx64.getData(0);
}
// TODO check option, rssi bytes
flashLed(statusLed, 1, 10);
// set dataLed PWM to value of the first byte in the data
analogWrite(dataLed, data);
} else {
// not something we were expecting
flashLed(errorLed, 1, 25);
}
} else if (xbee.getResponse().isError()) {
//nss.print("Error reading packet. Error code: ");
//nss.println(xbee.getResponse().getErrorCode());
// or flash error led
}
}
Rx.txt (2.56 KB)
Tx.txt (2.56 KB)