XBee API data packet too long?

Hi everybody, sorry for my english.....

I'm writing a code to communicate an arduino with a pc using the XBee protoshield and the XBIB-DEV REV. 4 connected to the pc. On my arduino code I'm using the Xbee.h library, and my XBees are using the API modo sending TX 16 and receiving RX 16 packets. The packet that i send through the XBIB-DEV from the PC to the arduino is the following:

0x 7E 00 11 01 01 00 04 01 00 00 00 00 00 00 00 00 00 00 00 00 F8

and the payload data is:

0x 01 00 00 00 00 00 00 00 00 00 00 00 00

I'm trying to do different stuff with different payloads using a switch case structure, something like this:

      switch (data){

      case 0x01000000000000000000000000: 
      xbee.send(tx);
      flashLed(statusLed, 1, 100);
      Serial.print(" Send 1 ");
      break;
      
      case 0x02000000000000000000000000: 
      xbee.send(tx);
      flashLed(statusLed, 1, 100);
      Serial.print(" Send 2 ");
      break;
      
      case 0x03000000000000000000000000:
      xbee.send(tx);
      flashLed(statusLed, 1, 100);
      Serial.print(" send 3 ");
      break;        
      }

but when I try to compile I get these errors:

 error: previously used here
 error: duplicate case value

data is defined as int16_t data = 0;

I think that the data value is too big and the arduino can't handle it, is that correct? if that the case (or if not) what can I do to handle these long packets? How can I read the data and then split the packet so the arduino only use the part that I want (in my example just reading the first byte 0x01, 0x02, etc) ?

Please help, any advice would be useful!!! =(

Hard to tell from partial code.

Switch/case compares the value of a variable (data in this example) to the values given by the case statements. A value like 0x01000000000000000000000000 is too long for any of the native data types. So what type is the variable data?

[quote author=Jack Christensen link=topic=196248.msg1448450#msg1448450 date=1383135538] So what type is the variable data? [/quote]

data is defined as

int16_t data = 0;

Hard to tell from partial code.

my code is too long and you may get lost reading it :S , but if you need it I can post it (:

as i thought the variable is too long :( do you have any advice for handling long packets?

thanks for the reply!

AhMedina: data is defined as

int16_t data = 0;

Opps I may have missed that first time around. So a 16-bit data type, i.e. two bytes, and 0x01000000000000000000000000 is 13 bytes if I counted all them zeroes correctly.

See any issues there?

[quote author=Jack Christensen link=topic=196248.msg1448748#msg1448748 date=1383150823] See any issues there? [/quote]

Yes, but I don't know how to solve that :/ ...may be read the packet and then split it in 2byte parts and only take the needed part, but I really don't know how I can do that :blush:

or maybe changing the definition of "data"?

While we don't relish wading through thousands of lines of code, it would be better if we could see it. Sometimes when I'm having trouble, I will try to skinny the code down to the minimum that still exhibits the issue. Makes it easier on the people trying to help but also on myself.

Heres my Skinny code:

#include "System_Defs.h"
#include <XBee.h>
#include <Sensirion.h>
int16_t statusLed = 9;
int16_t errorLed = 10;

int16_t salValue0 = LOW;
int16_t salValue1 = LOW;
int16_t salValue2 = LOW;
int16_t salValue3 = LOW;

//TASKs
int medicion_id;
unsigned long medicion_var;

// XBee payload
uint8_t payload[32] = {0};

// with Series 1 you can use either 16-bit or 64-bit addressing
// 16-bit addressing: Enter address of remote XBee, typically the coordinator
Tx16Request tx = Tx16Request(0x0001, payload, sizeof(payload));
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle 
Rx16Response rx16 = Rx16Response();
Rx64Response rx64 = Rx64Response();

//variables
boolean Manual = true;
boolean Auto = true;
int16_t Comando = 0;
int16_t PAtm = 0; 
int16_t Ilum = 0;
int16_t TemperaturaPila1 = 0;
int16_t HumedadRelativaPila1 = 0;
int16_t TemperaturaPila2 = 0;
int16_t HumedadRelativaPila2 = 0;
int16_t TemperaturaPila3 = 0; 
int16_t HumedadRelativaPila3 = 0;
int16_t TemperaturaPila4 = 0;
int16_t HumedadRelativaPila4 = 0;
float precipitacion = 0;

//SHT
const int16_t dataPin  =  2;
const int16_t clockPin =  3;
int16_t option = 0;
int16_t data = 0;
int16_t DataComando = data >> 0xff;
float temperature;
float humidity;
float dewpoint;

Sensirion tempSensor = Sensirion(dataPin, clockPin);

void flashLed(int16_t pin, int16_t times, int16_t wait) {

  for (int16_t i = 0; i < times; i++) {
    digitalWrite(pin, HIGH);
    delay(wait);
    digitalWrite(pin, LOW);

    if (i + 1 < times) {
      delay(wait);
    }
  }
}

//Task: collect variable values and then send a packet to the pc’s XBee
void envio_medicion(int id, void * tptr) {

  Serial.print(" Task ");
  //SHT75 measures
  tempSensor.measure(&temperature, &humidity, &dewpoint);
  delay (10);
                                        //envio de datos por el Xbee
                                        payload[0] = Manual;
                                        payload[1] = Comando;
                                        payload[2] = salValue0;                                                    
                                        payload[3] = (int16_t) temperature >> 8 & 0xff; //cast a temperature para
                                        payload[4] = (int16_t) temperature & 0xff; //que pueda ser manipulada
                                        payload[5] = (int16_t) humidity >> 8 & 0xff; //cast a humedad para
                                        payload[6] = (int16_t) humidity & 0xff;//que pueda ser manipulada
                                        payload[7] = (int16_t) PAtm >> 8 & 0xff;
                                        payload[8] = (int16_t) PAtm & 0xff;
                                        payload[9] = (int16_t) precipitacion >> 8 & 0xff;
                                        payload[10] = (int16_t) precipitacion & 0xff;
                                        payload[11] = (int16_t) Ilum >> 8 & 0xff;
                                        payload[12] = (int16_t) Ilum & 0xff;
                                        payload[13] = (int16_t) TemperaturaPila1 >> 8 & 0xff;
                                        payload[14] = (int16_t) TemperaturaPila1 & 0xff;
                                        payload[15] = (int16_t) HumedadRelativaPila1 >> 8 & 0xff;
                                        payload[16] = (int16_t) HumedadRelativaPila1 & 0xff;
                                        payload[17] = (int16_t) TemperaturaPila2 >> 8 & 0xff;
                                        payload[18] = (int16_t) TemperaturaPila2 & 0xff;
                                        payload[19] = (int16_t) HumedadRelativaPila2 >> 8 & 0xff;
                                        payload[20] = (int16_t) HumedadRelativaPila2 & 0xff;
                                        payload[22] = (int16_t) TemperaturaPila3 >> 8 & 0xff;
                                        payload[22] = (int16_t) TemperaturaPila3 & 0xff;
                                        payload[23] = (int16_t) HumedadRelativaPila3 >> 8 & 0xff;
                                        payload[24] = (int16_t) HumedadRelativaPila3 & 0xff;
                                        payload[25] = (int16_t) TemperaturaPila4 >> 8 & 0xff;
                                        payload[26] = (int16_t) TemperaturaPila4 & 0xff;
                                        payload[27] = (int16_t) HumedadRelativaPila4 >> 8 & 0xff;
                                        payload[28] = (int16_t) HumedadRelativaPila4 & 0xff;
                                        delay (20);
                                        xbee.send(tx);
                                        // flash TX indicator
                                        flashLed(statusLed, 1, 100);  
}


void setup() {

  pinMode(statusLed, OUTPUT);
  pinMode(errorLed, OUTPUT);

 // start serial
  Serial.begin(9600);

  xbee.begin(9600);
  flashLed(statusLed, 3, 50);

 //Task
  medicion_id = createTask(envio_medicion, 10000, TASK_ENABLE, &medicion_var); 

}
  
// 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);
        option = rx16.getOption();
        data = rx16.getData(0);
      } 
      else {
        xbee.getResponse().getRx64Response(rx64);
        option = rx64.getOption();
        data = rx64.getData(0);
      }

      // check option, rssi bytes    
      flashLed(statusLed, 1, 10);

/////Operation modes
      switch (data){
      ///Read commands 
      case 0x01000000000000000000000000: 
      Serial.print(" send 1 ");
      xbee.send(tx);
      flashLed(statusLed, 1, 100);
      break;
      
      case 0x02000000000000000000000000: 
      xbee.send(tx);
      flashLed(statusLed, 1, 100);
      Serial.print(" send 2 ");
      break;
      
      case 0x03000000000000000000000000:
      xbee.send(tx);
      flashLed(statusLed, 1, 100);
      Serial.print(" send  3 ");
      break;
 }

      Serial.print(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
  }

}

Maybe some variables definitions do not appear, that’s because I have erased parts of the code that has nothing to do with my problem.

Hi [quote author=Jack Christensen link=topic=196248.msg1448775#msg1448775 date=1383152112] [/quote]

I've solved the problem!!

I only needed to read the library documentation :P

https://code.google.com/p/xbee-arduino/wiki/DevelopersGuide

By default, the library allocates an array of size 110 bytes for receiving the packet.

So I've changed my code to:

      if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
        xbee.getResponse().getRx16Response(rx16);
        option = rx16.getOption();
        data = rx16.getData(0);
        data2 = rx16.getData(12); 
      }

and now in my swith case structure I'm using "data2" instead of data y problem solved!!! :D Thanks a lot for your replys, it helps me to understand the problem :)

Hey! Way to go!