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.

Revision:
0:a8cde1e2de5b
Child:
2:60581ca6978e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Apr 29 17:58:22 2015 +0200
@@ -0,0 +1,81 @@
+/**
+ * Copyright (c) 2015 Digi International Inc.,
+ * All rights not expressly granted are reserved.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
+ * =======================================================================
+ */
+
+#include "mbed.h"
+#include "XBeeLib.h"
+#if defined(ENABLE_LOGGING)
+#include "DigiLoggerMbedSerial.h"
+using namespace DigiLog;
+#endif
+
+// TODO Replace with the MSB of the remote module's 64-bit address (SH parameter)
+#define REMOTE_NODE_ADDR64_MSB  ((uint32_t)0x0013A200)
+// TODO Replace with the LSB of the remote module's 64-bit address (SL parameter)
+#define REMOTE_NODE_ADDR64_LSB  ((uint32_t)0x40D2B03E)
+// TODO Replace with the remote module's 16-bit address (MY parameter)
+#define REMOTE_NODE_ADDR16      ((uint16_t)0x1111)
+
+using namespace XBeeLib;
+
+Serial *log_serial;
+
+static void send_broadcast_data(XBee802& xbee)
+{
+    const char data[] = "send_broadcast_data";
+    const uint16_t data_len = strlen(data);
+
+    const TxStatus txStatus = xbee.send_data_broadcast((const uint8_t *)data, data_len);
+
+    if (txStatus == TxStatusSuccess)
+        log_serial->printf("send_broadcast_data OK\r\n");
+    else
+        log_serial->printf("send_broadcast_data failed with %d\r\n", (int) txStatus);
+}
+
+static void send_data_to_remote_node(XBee802& xbee, const RemoteXBee802& RemoteDevice)
+{
+    const char data[] = "send_data_to_remote_node";
+    const uint16_t data_len = strlen(data);
+
+    const TxStatus txStatus = xbee.send_data(RemoteDevice, (const uint8_t *)data, data_len);
+
+    if (txStatus == TxStatusSuccess)
+        log_serial->printf("send_data_to_remote_node OK\r\n");
+    else
+        log_serial->printf("send_data_to_remote_node failed with %d\r\n", (int) txStatus);
+}
+
+int main()
+{
+    log_serial = new Serial(DEBUG_TX, DEBUG_RX);
+    log_serial->baud(9600);
+    log_serial->printf("Sample application to demo how to send unicast and broadcast data with the XBee802\r\n\r\n");
+    log_serial->printf(XB_LIB_BANNER);
+
+#if defined(ENABLE_LOGGING)
+    new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
+#endif
+
+    XBee802 xbee = XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
+
+    RadioStatus radioStatus = xbee.init();
+    MBED_ASSERT(radioStatus == Success);
+
+    const RemoteXBee802 remoteDevice64b = RemoteXBee802(Addr64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB));
+    const RemoteXBee802 remoteDevice16b = RemoteXBee802(REMOTE_NODE_ADDR16);
+
+    send_broadcast_data(xbee);
+    send_data_to_remote_node(xbee, remoteDevice64b);
+    send_data_to_remote_node(xbee, remoteDevice16b);
+
+    delete(log_serial);
+}