Development

Communication / 868:

» 868 02: Send packets

This program shows how to send packets to a gateway indicating the MAC address of the receiving XBee module

Required Materials

1 x Waspmote PRO
1 x Battery
1 x MiniUSB wire
1 x XBee-868

Notes

* This example belongs to a two-code example. This is the sending part.
* Both XBee modules must be configured with the same network parameters (PANID and encryption mode)

Code

/*  
 *  ------ [868_02] - send packets  -------- 
 *  
 *  Explanation: This program shows how to send packets to a gateway
 *  indicating the MAC address of the receiving XBee module 
 *  
 *  Copyright (C) 2012 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 3 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:    Yuri Carmona
 */
 
#include <WaspXBee868.h>
#include <WaspFrame.h>

//Pointer to an XBee packet structure 
packetXBee* packet; 

// Destination MAC address
char* MAC_ADDRESS="0013A2004047B401";


void setup()
{
  // Init USB port
  USB.ON();
  USB.println(F("868_02 example"));
  
  // Powers XBee
  xbee868.ON();

}


void loop()
{
  ///////////////////////////////////////////
  // 1. Create ASCII frame
  ///////////////////////////////////////////  

  // 1.1. Create new frame
  frame.createFrame(ASCII, "WASPMOTE_XBEE");  
  
  // 1.2. add frame fields
  frame.addSensor(SENSOR_STR, "XBee frame");
  frame.addSensor(SENSOR_BAT, PWR.getBatteryLevel()); 
  
  ///////////////////////////////////////////
  // 2. Send packet
  ///////////////////////////////////////////  
  
  // 2.1. Set parameters to packet:
  packet=(packetXBee*) calloc(1,sizeof(packetXBee)); // Memory allocation
  packet->mode=UNICAST; // Choose transmission mode: UNICAST or BROADCAST  
  
  // 2.2. Set destination XBee parameters to packet
  xbee868.setDestinationParams( packet, MAC_ADDRESS, frame.buffer, frame.length);  
  
  // 2.3. Send XBee packet
  xbee868.sendXBee(packet);
  
  // 2.4. Check TX flag
  if( xbee868.error_TX == 0 )
  { 
    USB.println(F("ok"));
  }
  else
  {
    USB.println(F("error"));
  }
  
  // 2.5. Free variables
  free(packet);
  packet=NULL;

  // Wait for five seconds
  delay(5000);
}


Output

B#
868_02 example
ok
ok
ok
ok
...

Quick Publish: