xbee-arduino library getData() returns API ID

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)

Why are you storing one byte in payload, and then telling the function t send two bytes?

PaulS:
Why are you storing one byte in payload, and then telling the function t send two bytes?

Good question! I didn't even notice that. I blame sleep deprivation.

I was able to solve my own problem actually! Since I'm sending from an Arduino Fio and not really worried about receiving anything back, I can just send whatever I feel like using Serial.print(<>) with Arduino Fio in AT mode and the Arduino Uno in API mode.

sorry...
how to print data in serial monitor without ack (xbee s2) ? i want to print data only without pin ack in serial monitor

this is my sketch :

#include <Adafruit_Sensor.h> // memasukkan library Adafruit Sensor
#include <XBee.h> // memasukkan library XBee
#include <DHT.h> // memasukkan library Sensor DHT

#define DHTPIN A5 // mendefinisikan pin data sensor
#define DHTTYPE DHT11 // mendefinisikan tipe dari DHT
// mendefinisikan variable
#define MQTTSN_TYPE_PUBLISH 0x0C
#define MQTTSN_TYPE_PUBACK 0x0D
#define MQTTSN_TOPIC_TYPE_PREDEFINED 0b01
#define MQTTSN_FLAG_QOS_1 0b01
#define MQTTSN_RC_ACCEPTED 0x00
#define MQTTSN_TOPIC_ID_SUHU 1
#define MQTTSN_TOPIC_ID_LEMBAB 2
#define MQTTSN_PROTOCOL_ID 0x01
#define MQTTSN_TYPE_CONNECT 0x04
#define MQTTSN_TYPE_CONNACK 0x05
// deklarasi variable
int t = 0;
int con = 1;

DHT dht(DHTPIN, DHTTYPE); // membuat objek, pin dalam arduino untuk DHT

XBee xbee = XBee(); // membuat objek XBee
// deklarasi variable dalam bentuk array diisi default 0
uint8_t payload[8] = {0,0,0,0,0,0,0,0};
uint8_t payloadterima[8] = {0,0,0,0,0,0,0,0};
uint8_t payloadconnect[8] = {0,0,0,0,0,0,0,0};
uint8_t payloadconnack[8] = {0,0,0,0,0,0,0,0};

// Alamat penerima XBee
XBeeAddress64 addr64 = XBeeAddress64(0x0013A200, 0x40B3EC8A);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
// deklarasi penerimaan XBee
ZBRxResponse zbRx = ZBRxResponse();

void setup() {
dht.begin(); // sensor DHT mulai
Serial.begin(9600); // baudrate
xbee.setSerial(Serial); // XBee di atur serial
}

void loop() {
if (con==1){
conect(); // pemanggilan fungsi conect
delay(1000); // delay
}else if (con==2){
publish1(); // pemanggilan fungsi publish1
delay(1000); // delay
publish(); // pemanggilan fungsi publish
delay(1000); // delay
}
}
// fungsi publish
void publish(){

float h = dht.readHumidity(); // sensing kelembapan disimpan dalam variable h dalam bentuk float
// penambahan variable t
t+=1;
t-+1;
// Deklarasi variable
byte msg_typ = MQTTSN_TYPE_PUBLISH;
byte qos = MQTTSN_FLAG_QOS_1;
byte topic_typ = MQTTSN_TOPIC_TYPE_PREDEFINED;
byte msg_id = t;
int topic_id = MQTTSN_TOPIC_ID_LEMBAB;
int data = h;
payload[0] = qos;
payload[1] = topic_id;
payload[2] = msg_id;
payload[3] = msg_typ;
payload[4] = data ;
payload[5] = topic_typ;
payload[6] = 0; // payload yang tidak terpakai
payload[7] = 0;
// XBee mengirim paket
xbee.send(zbTx);
if (xbee.readPacket(500)) {
if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
xbee.getResponse().getZBTxStatusResponse(txStatus);
if (txStatus.getDeliveryStatus() == SUCCESS) {
}
}
}if (xbee.getResponse().isError()) {
}

// XBee menerima paket
xbee.readPacket();
if (xbee.getResponse().isAvailable()){
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE){
xbee.getResponse().getZBRxResponse(zbRx);

for(int i = 0; i < zbRx.getDataLength(); i++){ // looping data i
payloadterima = zbRx.getData(i);

  • Serial.print ('\n');*
    // Serial.print (i);
    // Serial.print("||");
    // Serial.print (payloadterima );
    // Serial.println ('\n');
    }
    }
    }
    }
    * // fungsi publish1*
    void publish1 () {
    * float s = dht.readTemperature(); // Sensing temperatur disimpan dalam variable s dalam bentuk float*
    * t+=1;*
    * t-+1;*
    * // deklarasi variable*
    * byte msg_typ = MQTTSN_TYPE_PUBLISH;
    byte qos = MQTTSN_FLAG_QOS_1;
    byte topic_typ = MQTTSN_TOPIC_TYPE_PREDEFINED;
    byte msg_id = t;
    int topic_id = MQTTSN_TOPIC_ID_SUHU;
    _
    int data = s;_
    _
    payload[0] = qos;_
    payload[1] = topic_id;
    payload[2] = msg_id;
    payload[3] = msg_typ;
    _
    payload[4] = data ;_
    payload[5] = topic_typ;
    _
    payload[6] = 0; // payload yang tidak terpakai diisi 0*_
    * payload[7] = 0;*
    * // XBee mengirim paket*
    * xbee.send(zbTx); *
    * if (xbee.readPacket(500)) {*
    * if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
    _
    xbee.getResponse().getZBTxStatusResponse(txStatus);_
    _
    if (txStatus.getDeliveryStatus() == SUCCESS) {_
    _
    }_
    _
    }_
    _
    }_
    _
    // XBee menerima paket*_
    xbee.readPacket();
    if (xbee.getResponse().isAvailable()){
    * if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE){
    _
    xbee.getResponse().getZBRxResponse(zbRx);*_

for(int i = 0; i < zbRx.getDataLength(); i++){ // looping data i
_ payloadterima = zbRx.getData(i);
// Serial.print (i);
// Serial.print("||");
// Serial.print (payloadterima );
// Serial.println ('\n');
* }
}
}
}
// fungsi conect*

void conect(){
* // deklarasi variable*
* byte msg_typ = MQTTSN_TYPE_CONNECT;
byte protocol_id = MQTTSN_PROTOCOL_ID;
int client_id = 1;
payload[0] = msg_typ;
payload[1] = protocol_id;
payload[2] = client_id;*

* payload[3] = 0; // payload yang tidak terpakai diisi 0*
* payload[4] = 0;
payload[5] = 0;
payload[6] = 0;
payload[7] = 0;
// XBee mengirim paket*

xbee.send(zbTx);
* if (xbee.readPacket(500)) {_
if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
_ xbee.getResponse().getZBTxStatusResponse(txStatus);
if (txStatus.getDeliveryStatus() == SUCCESS) {
}
}
}
Serial.println("Connect"); // print connect*_

* // XBee menerima paket*
* xbee.readPacket();*
if (xbee.getResponse().isAvailable()){
* if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE){
_ xbee.getResponse().getZBRxResponse(zbRx);*_

for(int i = 0; i < zbRx.getDataLength(); i++){ // looping data i
_ payloadconnack = zbRx.getData(i);
// Serial.print (i);
// Serial.print("=");
// Serial.print (payloadconnack );
// Serial.println ('\n');
* }
} // Pemfilteran*

* if (payloadconnack[0] == MQTTSN_TYPE_CONNACK){
if (payloadconnack[1] == MQTTSN_RC_ACCEPTED){*

* Serial.println("Connack"); // print connack*
* con =2; // Eksekusi kondisi 2*
* }
}
}
}*_

Aka_emilicious:
Good question! I didn't even notice that. I blame sleep deprivation.

I was able to solve my own problem actually! Since I'm sending from an Arduino Fio and not really worried about receiving anything back, I can just send whatever I feel like using Serial.print(<>) with Arduino Fio in AT mode and the Arduino Uno in API mode.

HI MATE, WHAT DID YOU DO ?