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:
hbujanda
Date:
Thu Jul 28 10:18:13 2016 +0200
Revision:
9:db657fba2ccf
Parent:
8:8860564edca8
Automatic upload

Who changed what in which revision?

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