XBee 868 

Example

: Sending & Receiving

This example shows how to send and receive packets using Waspmote XBee 868 API


File:
"WaspXBee868_2_sending_receiving.pde"


Code



/*
 *  ------Waspmote XBee 868 Sending & Receiving Example------
 *
 *  Explanation: This example shows how to send and receive packets
 *  using Waspmote XBee 868 API
 *
 *  This code sends a packet to another node and waits for an answer from
 *  it. When the answer is received it is shown.
 *
 *  Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
 *  http://www.libelium.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Version:                0.1
 *  Design:                 David Gascón
 *  Implementation:    Alberto Bielsa
 */

 packetXBee* paq_sent;
 int8_t state=0;
 long previous=0;
 char*  data="Test message!";

void setup()
{
  // Inits the XBee 868 library
  xbee868.init(XBEE_868,FREQ868M,NORMAL);

  // Powers XBee
  xbee868.ON();

}

void loop()
{
  // Set params to send
  paq_sent=(packetXBee*) calloc(1,sizeof(packetXBee));
  paq_sent->mode=UNICAST;
  paq_sent->MY_known=0;
  paq_sent->packetID=0x52;
  paq_sent->opt=0;
  xbee868.hops=0;
  xbee868.setOriginParams(paq_sent, "5678", MY_TYPE);
  xbee868.setDestinationParams(paq_sent, "0013A200403A0235", data, MAC_TYPE, DATA_ABSOLUTE);
  xbee868.sendXBee(paq_sent);
  if( !xbee868.error_TX )
  {
    XBee.println("ok");
  }
  free(paq_sent);
  paq_sent=NULL;

  // Waiting the answer
  previous=millis();
  while( (millis()-previous) < 20000 )
  {
    if( XBee.available() )
    {
      xbee868.treatData();
      if( !xbee868.error_RX )
      {
        // Writing the parameters of the packet received
        while(xbee868.pos>0)
        {
          XBee.print("Network Address Source: ");
          XBee.print(xbee868.packet_finished[xbee868.pos-1]->naS[0],HEX);
          XBee.print(xbee868.packet_finished[xbee868.pos-1]->naS[1],HEX);
          XBee.println("");
          XBee.print("MAC Address Source: ");
          for(int b=0;b<4;b++)
          {
            XBee.print(xbee868.packet_finished[xbee868.pos-1]->macSH[b],HEX);
          }
          for(int c=0;c<4;c++)
          {
            XBee.print(xbee868.packet_finished[xbee868.pos-1]->macSL[c],HEX);
          }
          XBee.println("");
          XBee.print("Network Address Origin: ");
          XBee.print(xbee868.packet_finished[xbee868.pos-1]->naO[0],HEX);
          XBee.print(xbee868.packet_finished[xbee868.pos-1]->naO[1],HEX);
          XBee.println("");
          XBee.print("MAC Address Origin: ");
          for(int d=0;d<4;d++)
          {
            XBee.print(xbee868.packet_finished[xbee868.pos-1]->macOH[d],HEX);
          }
          for(int e=0;e<4;e++)
          {
            XBee.print(xbee868.packet_finished[xbee868.pos-1]->macOL[e],HEX);
          }
          XBee.println("");
          XBee.print("RSSI: ");
          XBee.print(xbee868.packet_finished[xbee868.pos-1]->RSSI,HEX);
          XBee.println("");
          XBee.print("16B(0) or 64B(1): ");
          XBee.print(xbee868.packet_finished[xbee868.pos-1]->mode,HEX);
          XBee.println("");
          XBee.print("Data: ");
          for(int f=0;f<xbee868.packet_finished[xbee868.pos-1]->data_length;f++)
          {
            XBee.print(xbee868.packet_finished[xbee868.pos-1]->data[f],BYTE);
          }
          XBee.println("");
          XBee.print("PacketID: ");
          XBee.print(xbee868.packet_finished[xbee868.pos-1]->packetID,HEX);
          XBee.println("");
          XBee.print("Type Source ID: ");
          XBee.print(xbee868.packet_finished[xbee868.pos-1]->typeSourceID,HEX);
          XBee.println("");
          XBee.print("Network Identifier Origin: ");
          for(int g=0;g<4;g++)
          {
            XBee.print(xbee868.packet_finished[xbee868.pos-1]->niO[g],BYTE);
          }
          XBee.println("");
          free(xbee868.packet_finished[xbee868.pos-1]);
          xbee868.packet_finished[xbee868.pos-1]=NULL;
          xbee868.pos--;
        }
        previous=millis();
      }
    }
  }

  delay(5000);
}
    

File:
"WaspXBee868_2_sending_receivingB.pde"


Code



/*
 *  ------Waspmote XBee 868 Sending & Receiving Example------
 *
 *  Explanation: This example shows how to send and receive packets
 *  using Waspmote XBee 868 API
 *
 *  This code sends a packet to another node and waits for an answer from
 *  it. When the answer is received it is shown.
 *
 *  This is the code for the receiver.
 *
 *  Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
 *  http://www.libelium.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Version:                0.1
 *  Design:                 David Gascón
 *  Implementation:    Alberto Bielsa
 */

 packetXBee* paq_sent;
 int8_t state=0;
 long previous=0;
 char*  data="Test message!";
 uint8_t destination[8];
 uint8_t i=0;

void setup()
{
  // Inits the XBee 868 library
  xbee868.init(XBEE_868,FREQ2_4G,NORMAL);

  // Powers XBee
  xbee868.ON();
}

void loop()
{
  // Waiting message
  previous=millis();
  while( (millis()-previous) < 20000 )
  {
    if( XBee.available() )
    {
      xbee868.treatData();
      if( !xbee868.error_RX )
      {
         // Sending answer back
         while(xbee868.pos>0)
         {
           if( (xbee868.packet_finished[xbee868.pos-1]->naO[0]==0x56) && (xbee868.packet_finished[xbee868.pos-1]->naO[1]==0x78) )
           {
             paq_sent=(packetXBee*) calloc(1,sizeof(packetXBee));
             paq_sent->mode=UNICAST;
             paq_sent->MY_known=0;
             paq_sent->packetID=0x52;
             paq_sent->opt=0;
             xbee868.hops=0;
             xbee868.setOriginParams(paq_sent, "ACK", NI_TYPE);
             while(i<4)
             {
               destination[i]=xbee868.packet_finished[xbee868.pos-1]->macSH[i];
               i++;
             }
             while(i<8)
             {
               destination[i]=xbee868.packet_finished[xbee868.pos-1]->macSL[i-4];
               i++;
             }
             xbee868.setDestinationParams(paq_sent, destination, data, MAC_TYPE, DATA_ABSOLUTE);
             xbee868.sendXBee(paq_sent);
             if( !xbee868.error_TX )
             {
               XBee.println("ok");
             }
             free(paq_sent);
             paq_sent=NULL;
           }
           free(xbee868.packet_finished[xbee868.pos-1]);
           xbee868.packet_finished[xbee868.pos-1]=NULL;
           xbee868.pos--;
        }
      }
    }
  }

  delay(5000);
}
    

Download code

You can download the code of this example.


© 2009 Libelium Comunicaciones Distribuidas S.L.

| Terms of use