I want to get the MAC address of xbee, but I'm not succeeding.
I have the following code.
uint8_t myaddress[10];
uint8_t shCmd[] = {'S','H'};
uint8_t slCmd[] = {'S','L'};
AtCommandRequest atRequestSH = AtCommandRequest(shCmd);
AtCommandRequest atRequestSL = AtCommandRequest(slCmd);
AtCommandResponse atResponse = AtCommandResponse();
void getMyAddress(){
xbee.send(atRequestSH);
if(xbee.readPacket(5000)){
if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
xbee.getResponse().getAtCommandResponse(atResponse);
if (atResponse.isOk()){
for(int i = 0; i < atResponse.getValueLength(); i++){
myaddress[i] = atResponse.getValue()[i];
}
}
}
}
delay(1000);
xbee.send(atRequestSL);
if(xbee.readPacket(5000)){
if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
xbee.getResponse().getAtCommandResponse(atResponse);
if (atResponse.isOk()){
for(int i = 0; i < atResponse.getValueLength(); i++){
myaddress[i+6] = atResponse.getValue()[i];
}
}
}
}
}
I hoped that the myaddress
array were 10 values, because the Xbee MAC address contains 64 bytes.
But the array contains only 8 values, for example:
Original Xbee Address is 0013a200408a31bb
Result function getMyAddress
is 013a20408a31bb
My function loses two zeros.
I print the MAC address with the following code:
for(int i=0; i < 10; i++)
Serial.print(myaddress[i], HEX);
Any ideas?
The MAC address is 64 bits, which is 8 bytes (64 bits / (8 bits/byte)). ATSH
and ATSL
both respond with a 4-byte value. So you should define my address
as 8 bytes, and copy ATSL
to myaddress[i+4]
.
Note that you can use memcpy()
instead of looping through the bytes:
memcpy( &myaddress[i+4], atResponse.getValue(), 4);
I'm not familiar with the Arudino's Serial.print()
, but if it doesn't support printing a hex byte with leading zero, you can print the MAC with:
for (int i = 0; i < 8; i++) {
if (myaddress[i] < 0x10) Serial.print( "0");
Serial.print( myaddress[i], HEX);
}