May 25

Assembling an XBee API packet in PHP

In a previous post I showed how to parse an XBee API packet in C and PHP, for my project I needed both parsing and assembling of such packets. In case anyone would want to use it, here’s the PHP code for when you want to manipulate DIO pins.

function assemblePacket($module, $pin, $pinVal)
{
  $sum = 0; // checksum variable
  $packet = "<";
 
  if ($pinVal)
  { 
    $pinState = "05";
    $pinchk = 0x05;
  }
  else
  {
    $pinState = "04";
    $pinchk = 0x04;
  }
 
  $packet .= "7E"; // Start byte
  $packet .= "00"; // MSB length
  $packet .= "10"; // LSB length
 
  $packet .= "17"; // Remote AT command request
  $sum += 0x17;
  $packet .= "00"; // No response packet  
  $packet .= "00"; // 64bit address ignored Byte 1
  $packet .= "00"; // 64bit address ignored Byte 2
  $packet .= "00"; // 64bit address ignored Byte 3
  $packet .= "00"; // 64bit address ignored Byte 4
  $packet .= "00"; // 64bit address ignored Byte 5
  $packet .= "00"; // 64bit address ignored Byte 6
  $packet .= "00"; // 64bit address ignored Byte 7
  $packet .= "00"; // 64bit address ignored Byte 8
 
  $packet .= $module; // Module MSB and LSB
  $mbytes = str_split($module, 1);
  $sum += base_convert(getVal($mbytes[1]), 10, 16) + (base_convert(getVal($mbytes[0]), 10, 16) << 4);
  $sum += base_convert(getVal($mbytes[3]), 10, 16) + (base_convert(getVal($mbytes[2]), 10, 16) << 4); 
 
 
  $packet .= "02"; // Apply changes immediately
  $sum += 0x02;
  $packet .= "44"; // Character D in HEX
  $sum += 0x44;
  $packet .= (string)(30 + $pin); // Pin value, 0 in hex is 30
  $sum += dechex(30+$pin)+30;
  $packet .= $pinState; // Pinstate
  $sum += $pinchk;
  $checksum = 0xFF - ( $sum & 0xFF);
  $hack = str_split(dechex($checksum), 1);
  echo $hack[0];
  echo "     ";
  echo $hack[1];
  if(ctype_alpha($hack[0]))
    $packet .= strtoupper($hack[0]);
  else
    $packet .= $hack[0]; 
  if(ctype_alpha($hack[1]))
    $packet .= strtoupper($hack[1]);
  else
    $packet .= $hack[1];         
  $packet .= ">";
  return $packet;  
} 
 
function getVal($c)
{
   if($c >= '0' && $c <= '9')
     return ($c - '0');
   else
     return ($c-'A'+10);
}

You input the destination as a String, the pin as a number and the state as 0 or 1. Example:

$packet = assemblePacket("1337", 2, 1);

This would set pin DIO2 to high for the module with address 1337.

Update: Some things didn’t work before, especially if there were A-F values in the checksum, hence the dirty hack to get it to work. I know it’s not pretty but it works.

May 25

Converting HEX as a String to actual HEX values

Last night I spent quite a bit of time figuring out how to go from a string containing “7E00101700000000000000000013380244320520” which is an XBee API packet to actually writing those hex values to the XBee. The packet has been assembled with PHP and a database backing it.

That data was echo’ed onto an HTML page which an Arduino (with an Ethernet connection) parsed. Basically what I did was reading the string one character at a time, here are bits of the code:

byte incFrame[100];
byte c = 0x0;
 
c = client.read();
incFrame[stringPos] = c;
stringPos++;

As you can see I stored the data in a byte array. But when you want to write the data to the Serial line, you need to perform some operations to make it actually work. A long story short, here’s the solution, I’m sure other people have spent messing around with this problem in the past and will in the future.

byte getVal(char c)
{
   if(c >= '0' && c <= '9')
     return (byte)(c - '0');
   else
     return (byte)(c-'A'+10)
}

This is needed to properly convert the ASCII value to the hex value of each character. Next we put together 2 characters as is done with HEX notation and send the data:

  for(int i = 0; i < stringPos; i+=2)
  {
    byte sendb = getVal(incFrame[i+1]) + (getVal(incFrame[i]) << 4);
    Serial.write(sendb);
  }

And there you have it, a string containing hex values as ascii is properly converted into actual HEX. There are other ways to do it but this is how I did it.

May 06

XBeeP, an XBee Packet Analyzer

I continued working on the XBee api frame (or packet, whatever you want to call it) analyzer and decided I should probably move on to something more suited for a computer program. I chose Java as I’ve had some classes on it in Uni, however the layout manager is still something I dislike as it took me quite a while to get a decent layout working (and it’s still a bit weird). I’m using the RXTX library to communicate with the COM port (Arduino does the same).

XBeeP v0.1

XBeeP v0.1

Here’s a screenshot of version 0.1 which can show you all incoming frames concerning sampling. I’ve used Java so I could make it crossplatform, however I’ll probably switch over to C# as building a decent GUI is easier as well as it has native support for COM ports and unsigned byte which Java does not (luckily a “signdByte & 0xFF” does the trick).

 

 

And lastly a short video of the program in action:

Once the program has some more functionality I’ll make it public. In case you would like to take a peek at the source code let me know.