tre...@gmail.com
unread,May 18, 2013, 11:41:12 AM5/18/13Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to xbee...@googlegroups.com
Well, since i had no answer to my previous cuestion, i tried to use two arduinos (one UNO rev3 and one Leonardo) to communicate and debug, and it seems that the problem is on my way of using the XBee-Arduino library. I'm using the version 0.4 of the library. I'll check the 0.3 version to test, maybe i'll post later on this.
I have setup the Series2_Rx on the coordinator, and Series2_Tx on the receiving end, and i always receive getApiId() == 95. To debug the output, i've used an Arduino Leonardo, using Serial1 for XBee and Serial for usb debugging purposes.
What i'm missing? My example just extends the Series2_Tx to put a bigger payload (18 bytes). For transmitting, i'm using an Arduino UNO rev3. I have both XBee series 2 modules in API mode AP=2, and properly configured with XCTU (basically the same for coordinator and endpoint, same PANID, each one's SH, SL, and NI, plus the endpoint has JV=1).
This is the code for the sender side, since the receiver is the same as in the example but with the debugging over Leonardo's usb, any hint is more than appreciated!
#include <XBee.h>
const int XBEE_BAUDRATE = 9600;
const unsigned long XBEE_TX_PERIOD = 5000UL;
const unsigned long XBEE_TX_TIMEOUT = 1000UL;
const unsigned long XBEE_RX_TIMEOUT = 1000UL;
// Communications protocol constants
const byte MAX_PAYLOAD_LEN = 18;
// Indexes for data inside a packet (payload side)
const byte PKT_TYPE = 0x00;
const byte PKT_NODE = 0x01;
const byte PKT_DATA = 0x02;
// Node types
const byte NODE_COORDINATOR = 0x00;
const byte NODE_ROUTER = 0x01;
const byte NODE_ENDPOINT = 0x02;
// Types of packets
const byte PKT_TYP_REQ_SENS_DATA = 0x10;
const byte PKT_TYP_RES_SENS_DATA = 0x11;
XBee xbee = XBee();
XBeeResponse xbeeResponse = XBeeResponse();
ZBRxResponse zbRxResponse = ZBRxResponse();
ModemStatusResponse zbMSR = ModemStatusResponse();
// SH + SL Address of receiving XBee (coordinator address)
int coordSH = 0x0013a200;
int coordSL = 0x4092d6bb;
XBeeAddress64 zbCoordAddr64 = XBeeAddress64( coordSH, coordSL );
uint8_t zbPayload[MAX_PAYLOAD_LEN]; // = { 0, 0 };
ZBTxRequest zbTxRequest = ZBTxRequest(); // = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse zbTxStatus = ZBTxStatusResponse();
// Time interval tracking
unsigned long xbee_localMillis;
unsigned long xbee_commPeriod;
void setup()
{
Serial.begin( XBEE_BAUDRATE );
xbee.begin( Serial );
xbee_commPeriod = XBEE_TX_PERIOD;
// Give some time for XBee endpoint to associate
delay( 2000 );
}
void loop()
{
// Xbee process
xbee_Process( millis() );
}
void xbee_Process( unsigned long pCurMillis )
{
// Receive data from Zigbee network
xbee_ReceiveData();
// Send sensor data over Zigbee network: non-blocking process
if( pCurMillis - xbee_localMillis >= xbee_commPeriod ) // Time for a transmission?
{
xbee_SendData();
// Debugging purposes
//xbee_ledState = !xbee_ledState;
//digitalWrite( XBEE_LED_PIN, xbee_ledState );
xbee_localMillis = pCurMillis;
}
}
void xbee_ReceiveData()
{
xbee.readPacket( XBEE_RX_TIMEOUT );
if( xbee.getResponse().isAvailable() )
{
if( xbee.getResponse().getApiId() == ZB_RX_RESPONSE )
{
// Got a ZB Rx Packet! Now fill our ZB Rx class
xbee.getResponse().getZBRxResponse( zbRxResponse );
if( zbRxResponse.getOption() == ZB_PACKET_ACKNOWLEDGED )
{
// The sender got an ACK
}
else
{
// Sender got a response, but didn't get an ACK
}
// TODO: Check our protocol/////////////////////////////////////////
if( zbRxResponse.getData( 0 ) == 0xAA )
{
//flashLed( PIN_LED, 6, 250 );
}
if( zbRxResponse.getData( 1 ) == 0xBB )
{
//flashLed( PIN_LED, 6, 750 );
}
/////////////////////////////////////////////////////////////////////
}
else if( xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE)
{
// Got a response (ACK) for a packet we sent!
xbee.getResponse().getZBTxStatusResponse( zbTxStatus );
// Get the delivery status, the fifth byte
//int discoveryStatus = zbTxStatus.getDiscoveryStatus();
if( zbTxStatus.getDeliveryStatus() == SUCCESS )
{
// Successful sending! Yeehay!
}
else
{
// Error: the remote XBee did not receive our packet
}
}
else if( xbee.getResponse().getApiId() == MODEM_STATUS_RESPONSE )
{
// The local XBee sends this response on certain events,
// like association/dissociation
xbee.getResponse().getModemStatusResponse( zbMSR );
if( zbMSR.getStatus() == ASSOCIATED )
{
// Sender got an 'Associated' response
}
else if( zbMSR.getStatus() == DISASSOCIATED )
{
// Sender got a 'Dissasociated' response
}
else
{
// Sender got another status response (unknown/unhandled)
}
}
else
{
// Error: sender got an unexpected response
}
}
else if( xbee.getResponse().isError() )
{
//nss.print( "Error reading packet. Error code: " );
//nss.println( xbee.getResponse().getErrorCode() );
}
}
// Here must be the meat...
void xbee_SendData()
{
// Prepare payload
xbee_PreparePayload();
// Prepare ZB Tx Request packet
zbTxRequest.setAddress64( zbCoordAddr64 );
zbTxRequest.setAddress16( ZB_BROADCAST_ADDRESS );// 0xFFFE );
zbTxRequest.setPayload( zbPayload );
zbTxRequest.setPayloadLength( sizeof( zbPayload ) );
//zbTxRequest.setPayloadLength( MAX_PAYLOAD_LEN );
// Prepare ZB Tx Request packet
//zbTxRequest = ZBTxRequest( zbCcoordAddr64, zbPayload, sizeof( zbPayload ) );
xbee.send( zbTxRequest );
}
void xbee_PreparePayload()
{
byte cnt;
zbPayload[PKT_TYPE] = PKT_TYP_RES_SENS_DATA;
zbPayload[PKT_NODE] = NODE_ENDPOINT;
cnt = PKT_DATA;
zbPayload[cnt++] = sensor1.b[0];
zbPayload[cnt++] = sensor1.b[1];
zbPayload[cnt++] = sensor1.b[2];
zbPayload[cnt++] = sensor1.b[3];
zbPayload[cnt++] = sensor2.b[0];
zbPayload[cnt++] = sensor2.b[1];
zbPayload[cnt++] = sensor2.b[2];
zbPayload[cnt++] = sensor2.b[3];
zbPayload[cnt++] = 0;//sensor3.b[0];
zbPayload[cnt++] = 0;//sensor3.b[1];
zbPayload[cnt++] = 0;//sensor3.b[2];
zbPayload[cnt++] = 0;//sensor3.b[3];
zbPayload[cnt++] = sensor4.b[0];
zbPayload[cnt++] = sensor4.b[1];
zbPayload[cnt++] = sensor4.b[2];
zbPayload[cnt] = sensor4.b[3];
}
For sure i'm missing something, but i cannot see what/where :(