problems writing byte array to serial for ZigBee communication.

Hey guys.

I'm having a hard time getting the correct byte values to show-up through ZigBee communication.
But I believe it is a problem with my code, and not a problem with ZigBee communication.

here is my very simple Xbee setup for testing:
Coordinator:
API mode.
Xbee (series2) connected to my computer through serial and using the XCTU software to monitor the "receive packets".
(no arduino)

Router:
AT mode.
Xbee (series2) connected to a (battery powered) Arduino Fio (Fio uses the ATmega328P micro).

  • running the following code:
// declare global variables:

// temp variables:
int temp = 1020;
byte tempHigh = 0;
byte tempLow = 0;

// voltage variables:
int voltage = 50;
byte voltageHigh = 0;
byte voltageLow = 0;

// declare an array that will store the Byte values.
byte fioData[] = {tempHigh, tempLow, voltageHigh, voltageLow};

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  // send the "temp" value into the intToBytes function.
  intToBytes(temp, tempHigh, tempLow);
  
  /*
  //debuging:
  Serial.print("temp: ");
  Serial.println(temp);
  Serial.print("high: ");
  Serial.println(tempHigh);
  Serial.print("low: ");
  Serial.println(tempLow);
  */
  
  // send the "voltage" value into the intToBytes function.
  intToBytes(voltage, voltageHigh, voltageLow);
  
  /*
  //debugging:
  Serial.print("voltage: ");
  Serial.println(voltage);
  Serial.print("high: ");
  Serial.println(voltageHigh);
  Serial.print("low: ");
  Serial.println(voltageLow);
  */
  
  // write the array of Bytes to serial:
  Serial.write(fioData, sizeof(fioData));
  
  // wait 2 seconds:
  delay(2000);
}

// create a function that converts an int into two bytes,
// HIGH byte and LOW byte:
void intToBytes(int unsignedIntVariable, byte &highValue, byte &lowValue)
{
  highValue = highByte(unsignedIntVariable);
  lowValue = lowByte(unsignedIntVariable);
  // credit: 
  // http://forum.arduino.cc/index.php/topic,44487.0.html
  // also arduino cookbook 3.14
}

The GOAL:

  • take a 10-bit analog reading value (0-1023) "int", and convert that into high and low "bytes".
  • pass those "bytes" through Xbee's.

Why?:

just another way of sending data through xbees.

  • by sending an array of bytes through the xbee's and then re-assembling those bytes into ints on the other end, I will avoid having to read HEX values (xbee's communicate w/ HEX, but I don't want to be reading HEX in my code). Hence sending an array of bytes.

The GOOD:

Xbees seem to be configured correctly:

  • packets come in every 2 seconds as desired.

The global variables seem to be updating properly:

  • un-commenting the debugging part of the code, and just running the Fio connected to my computer (w/o Xbee): While monitoring the serial it seems the 4 global variables that need to be changed: tempHigh, tempLow, voltageHigh, voltageLow are all updated properly.

The PROBLEM:

According to the Coordinator (reading the receive-packets through the XCTU), my "payload" (the data I am interested in sending) is only registering as whatever value I declared in the "global variables" section of my code (zero's in this case). And since those global variables seem to be updated properly when de-bugging, I am thinking there is an issue with sending bytes. - (Serial.write) - I can't seem to figure out what the issue is though. Any ideas?

Thanks

  Serial.write(fioData, sizeof(fioData));

fioData is not bound to those variables. The values in those variables are copied ONCE into fioData. Of course, you never get anything except the initial values (all 0).

Thanks Paul.
I did not think about the fact that the array does not update.
(doh!)

I'm updating the global variables in the loop,
but not the array.

To fix that,
I declared the array within the loop, and now it updates each time through.

new code:

// declare global variables:

// temp variables:
int temp = 1020;
byte tempHigh = 0;
byte tempLow = 0;

// voltage variables:
int voltage = 50;
byte voltageHigh = 0;
byte voltageLow = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  // send the "temp" value into the intToBytes function.
  intToBytes(temp, tempHigh, tempLow);
  
  /*
  //debuging:
  Serial.print("temp: ");
  Serial.println(temp);
  Serial.print("high: ");
  Serial.println(tempHigh);
  Serial.print("low: ");
  Serial.println(tempLow);
  */
  
  // send the "voltage" value into the intToBytes function.
  intToBytes(voltage, voltageHigh, voltageLow);
  
  /*
  //debugging:
  Serial.print("voltage: ");
  Serial.println(voltage);
  Serial.print("high: ");
  Serial.println(voltageHigh);
  Serial.print("low: ");
  Serial.println(voltageLow);
  */
  
  // declare an array that will store the Byte values.
  byte fioData[] = {tempHigh, tempLow, voltageHigh, voltageLow};
  
  // write the array of Bytes to serial:
  Serial.write(fioData, sizeof(fioData));
  
  // wait 2 seconds:
  delay(2000);
  
  voltage++;
}

// create a function that converts an int into two bytes,
// HIGH byte and LOW byte:
void intToBytes(int unsignedIntVariable, byte &highValue, byte &lowValue)
{
  highValue = highByte(unsignedIntVariable);
  lowValue = lowByte(unsignedIntVariable);
  // credit: 
  // http://forum.arduino.cc/index.php/topic,44487.0.html
  // also arduino cookbook 3.14
}

I also added the line:

voltage++;

in order to change the global "voltage" variable to test that the array is updating each time through the loop.
(which it is!)

Thanks again.

-Josh!

The global variables tempHigh, tempLow, voltageHigh, and voltageLow are not needed. The intToBytes() function can be passed the element of the array that you want to write to.