XBee and Arduinos

424 views
Skip to first unread message

tre...@gmail.com

unread,
May 18, 2013, 11:41:12 AM5/18/13
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 :(




tre...@gmail.com

unread,
May 18, 2013, 11:46:20 AM5/18/13
to xbee...@googlegroups.com
I forgot to mention i'm using the ZNet 2.5 API latest firmware with the Xbee modules (endpoint and coordinator). Should i use the Zigbee one?

tre...@gmail.com

unread,
May 18, 2013, 12:13:37 PM5/18/13
to xbee...@googlegroups.com
Damn, i've tried the XBee-Arduino v0.3 library, i had to change a little bit the XBee.cpp to make it compile for Serial1 in Leonardo, as follows:

#if defined(USBCON)
   _serial = &Serial1;
#else
   _serial = &Serial;
#endif

But now i cannot even communicate! I start the Leonardo, and watch my "Init debugging" message, then i
start the UNO, but nothing arrives to Leonardo! Not even the getApiId() == 95!!

Going back to v0.4 to continue testing... I wasn't able to make my program in java with XBee-Api, tried
with two Arduinos, but no luck :(

The strange thing is, just as soon as i plug-on the Arduino UNO (the sender) the RSSI led brights, but after
some seconds it turns off. Maybe i'm having a bad pointer issue?

For the sake of my poor life, please help!

Scott216

unread,
Jul 2, 2014, 1:33:36 PM7/2/14
to xbee...@googlegroups.com
Did you ever get this working?  I wonder if it would work if you didn't use a Leonardo and just tried two UNOs.  

Juan Fabián

unread,
Jul 18, 2014, 6:29:36 AM7/18/14
to xbee...@googlegroups.com
Hello Scott,

thanks for writing. That project was finished about a year ago, and i don't rememeber that much, sorry. Sadly i couldn't test with two UNO, so i finally didn't use the library. The last thing i remember was to upload the Zigbee firmware (not the ZNet one, as someone suggested), and tried to make a few tewaks, but i didn't have any debugging tool so it was difficult for me to dig into the issue. If i recall correctly, i finally had to use a big chunk of polling code to make the thing work, more or less (it didn't work as smooth as i wanted but finally the customer -an artist- was happy enough with the result).

Anyways, thanks for writing. I've just looked at the googlegroup and i can see this is still a "weird" thing. Sadly i cannot make any further test right now. In January i'll end with my current job, so maybe it's a good time to bring the project back.

Have a nice day!

Fabi


--
You received this message because you are subscribed to a topic in the Google Groups "xbee-api" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/xbee-api/qhl-6Q-2PNo/unsubscribe.

To unsubscribe from this group and all its topics, send an email to xbee-api+u...@googlegroups.com.
To post to this group, send email to xbee...@googlegroups.com.
Visit this group at http://groups.google.com/group/xbee-api.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages