802.15.4 Send Data example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Description

This example shows how to send data to a remote XBee802 module. The application creates a message for a remote XBee module. This library encodes and sends it to the local XBee module through the serial port. Then the local XBee module sends the packet to the remote XBee module through the air.

The example shows how to send both unicast and broadcast messages.

See Sending data to another module chapter for more information.

Common Setup

Make sure you have a valid Example Common Setup

Example Setup

XBee modules

To evaluate the 16-bit addressing mode you have to configure the local XBee module and the remote XBee module with unique values... for example 0x1111 for the local and 0x2222 for the remote.

Application

Broadcast messages doesn't require any configuration, but for the unicast messages you have to configure:

  • The remote device 64-bit address by customizing the REMOTE_NODE_ADDR64_MSB and REMOTE_NODE_ADDR64_LSB defines with the remote XBee module 64-bit address.
  • The remote device 16-bit address by customizing the REMOTE_NODE_ADDR16 define with the remote XBee module 16-bit address.

Running the example

Build and deploy the example to the mbed module.
Reset the mbed module so the example starts. You should see the example debug information through the debug interface configured in the 'Local Setup' chapter. The application will first send a broadcast message and finally a couple of unicast messages to the configured remote XBee module (first using 64-bit addressing mode and then using 16-bit addressing mode).

Verify that the remote XBee module is receiving the frames by accessing the "Console" tab of the X-CTU. You should see there the broadcast and unicast messages.

Committer:
spastor
Date:
Fri May 08 11:51:57 2015 +0200
Revision:
2:60581ca6978e
Parent:
0:a8cde1e2de5b
Automatic upload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hbujanda 0:a8cde1e2de5b 1 /**
hbujanda 0:a8cde1e2de5b 2 * Copyright (c) 2015 Digi International Inc.,
hbujanda 0:a8cde1e2de5b 3 * All rights not expressly granted are reserved.
hbujanda 0:a8cde1e2de5b 4 *
hbujanda 0:a8cde1e2de5b 5 * This Source Code Form is subject to the terms of the Mozilla Public
hbujanda 0:a8cde1e2de5b 6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
hbujanda 0:a8cde1e2de5b 7 * You can obtain one at http://mozilla.org/MPL/2.0/.
hbujanda 0:a8cde1e2de5b 8 *
hbujanda 0:a8cde1e2de5b 9 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
hbujanda 0:a8cde1e2de5b 10 * =======================================================================
hbujanda 0:a8cde1e2de5b 11 */
hbujanda 0:a8cde1e2de5b 12
hbujanda 0:a8cde1e2de5b 13 #include "mbed.h"
hbujanda 0:a8cde1e2de5b 14 #include "XBeeLib.h"
hbujanda 0:a8cde1e2de5b 15 #if defined(ENABLE_LOGGING)
hbujanda 0:a8cde1e2de5b 16 #include "DigiLoggerMbedSerial.h"
hbujanda 0:a8cde1e2de5b 17 using namespace DigiLog;
hbujanda 0:a8cde1e2de5b 18 #endif
hbujanda 0:a8cde1e2de5b 19
hbujanda 0:a8cde1e2de5b 20 // TODO Replace with the MSB of the remote module's 64-bit address (SH parameter)
hbujanda 0:a8cde1e2de5b 21 #define REMOTE_NODE_ADDR64_MSB ((uint32_t)0x0013A200)
hbujanda 0:a8cde1e2de5b 22 // TODO Replace with the LSB of the remote module's 64-bit address (SL parameter)
hbujanda 0:a8cde1e2de5b 23 #define REMOTE_NODE_ADDR64_LSB ((uint32_t)0x40D2B03E)
hbujanda 0:a8cde1e2de5b 24 // TODO Replace with the remote module's 16-bit address (MY parameter)
hbujanda 0:a8cde1e2de5b 25 #define REMOTE_NODE_ADDR16 ((uint16_t)0x1111)
hbujanda 0:a8cde1e2de5b 26
spastor 2:60581ca6978e 27 #define REMOTE_NODE_ADDR64 UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB)
spastor 2:60581ca6978e 28
hbujanda 0:a8cde1e2de5b 29 using namespace XBeeLib;
hbujanda 0:a8cde1e2de5b 30
hbujanda 0:a8cde1e2de5b 31 Serial *log_serial;
hbujanda 0:a8cde1e2de5b 32
hbujanda 0:a8cde1e2de5b 33 static void send_broadcast_data(XBee802& xbee)
hbujanda 0:a8cde1e2de5b 34 {
hbujanda 0:a8cde1e2de5b 35 const char data[] = "send_broadcast_data";
hbujanda 0:a8cde1e2de5b 36 const uint16_t data_len = strlen(data);
hbujanda 0:a8cde1e2de5b 37
hbujanda 0:a8cde1e2de5b 38 const TxStatus txStatus = xbee.send_data_broadcast((const uint8_t *)data, data_len);
hbujanda 0:a8cde1e2de5b 39
hbujanda 0:a8cde1e2de5b 40 if (txStatus == TxStatusSuccess)
hbujanda 0:a8cde1e2de5b 41 log_serial->printf("send_broadcast_data OK\r\n");
hbujanda 0:a8cde1e2de5b 42 else
hbujanda 0:a8cde1e2de5b 43 log_serial->printf("send_broadcast_data failed with %d\r\n", (int) txStatus);
hbujanda 0:a8cde1e2de5b 44 }
hbujanda 0:a8cde1e2de5b 45
hbujanda 0:a8cde1e2de5b 46 static void send_data_to_remote_node(XBee802& xbee, const RemoteXBee802& RemoteDevice)
hbujanda 0:a8cde1e2de5b 47 {
hbujanda 0:a8cde1e2de5b 48 const char data[] = "send_data_to_remote_node";
hbujanda 0:a8cde1e2de5b 49 const uint16_t data_len = strlen(data);
hbujanda 0:a8cde1e2de5b 50
hbujanda 0:a8cde1e2de5b 51 const TxStatus txStatus = xbee.send_data(RemoteDevice, (const uint8_t *)data, data_len);
hbujanda 0:a8cde1e2de5b 52
hbujanda 0:a8cde1e2de5b 53 if (txStatus == TxStatusSuccess)
hbujanda 0:a8cde1e2de5b 54 log_serial->printf("send_data_to_remote_node OK\r\n");
hbujanda 0:a8cde1e2de5b 55 else
hbujanda 0:a8cde1e2de5b 56 log_serial->printf("send_data_to_remote_node failed with %d\r\n", (int) txStatus);
hbujanda 0:a8cde1e2de5b 57 }
hbujanda 0:a8cde1e2de5b 58
hbujanda 0:a8cde1e2de5b 59 int main()
hbujanda 0:a8cde1e2de5b 60 {
hbujanda 0:a8cde1e2de5b 61 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
hbujanda 0:a8cde1e2de5b 62 log_serial->baud(9600);
hbujanda 0:a8cde1e2de5b 63 log_serial->printf("Sample application to demo how to send unicast and broadcast data with the XBee802\r\n\r\n");
hbujanda 0:a8cde1e2de5b 64 log_serial->printf(XB_LIB_BANNER);
hbujanda 0:a8cde1e2de5b 65
hbujanda 0:a8cde1e2de5b 66 #if defined(ENABLE_LOGGING)
hbujanda 0:a8cde1e2de5b 67 new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
hbujanda 0:a8cde1e2de5b 68 #endif
hbujanda 0:a8cde1e2de5b 69
hbujanda 0:a8cde1e2de5b 70 XBee802 xbee = XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
hbujanda 0:a8cde1e2de5b 71
hbujanda 0:a8cde1e2de5b 72 RadioStatus radioStatus = xbee.init();
hbujanda 0:a8cde1e2de5b 73 MBED_ASSERT(radioStatus == Success);
hbujanda 0:a8cde1e2de5b 74
spastor 2:60581ca6978e 75 const RemoteXBee802 remoteDevice64b = RemoteXBee802(REMOTE_NODE_ADDR64);
hbujanda 0:a8cde1e2de5b 76 const RemoteXBee802 remoteDevice16b = RemoteXBee802(REMOTE_NODE_ADDR16);
hbujanda 0:a8cde1e2de5b 77
hbujanda 0:a8cde1e2de5b 78 send_broadcast_data(xbee);
hbujanda 0:a8cde1e2de5b 79 send_data_to_remote_node(xbee, remoteDevice64b);
hbujanda 0:a8cde1e2de5b 80 send_data_to_remote_node(xbee, remoteDevice16b);
hbujanda 0:a8cde1e2de5b 81
hbujanda 0:a8cde1e2de5b 82 delete(log_serial);
hbujanda 0:a8cde1e2de5b 83 }