ZigBee Send Data example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Description

This example shows how to send data to a remote XBeeZB 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. It also shows how to send messages to the network coordinator.

See Sending data to another module chapter for more information.

Common Setup

Make sure you have a valid Example Common Setup

Example Setup

Application

Broadcast messages and messages to the coordinator don't require any configuration, but for unicast messages to a specific node 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.

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. Once joined to the coordinator, the application will first send a message to the coordinator, then a broadcast message and finally a couple of unicast messages to the configured remote XBee module.

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. If the remote XBee module is also the network coordinator, you should see there also the message sent to the coordinator.

Committer:
hbujanda
Date:
Fri Jul 29 12:13:12 2016 +0200
Revision:
10:b75412a32a4c
Parent:
7:d685568a3bc8
Automatic upload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hbujanda 5:7dd815802c47 1 /**
hbujanda 5:7dd815802c47 2 * Copyright (c) 2015 Digi International Inc.,
hbujanda 5:7dd815802c47 3 * All rights not expressly granted are reserved.
hbujanda 5:7dd815802c47 4 *
hbujanda 5:7dd815802c47 5 * This Source Code Form is subject to the terms of the Mozilla Public
hbujanda 5:7dd815802c47 6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
hbujanda 5:7dd815802c47 7 * You can obtain one at http://mozilla.org/MPL/2.0/.
hbujanda 5:7dd815802c47 8 *
hbujanda 5:7dd815802c47 9 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
hbujanda 5:7dd815802c47 10 * =======================================================================
hbujanda 5:7dd815802c47 11 */
hbujanda 5:7dd815802c47 12
hbujanda 5:7dd815802c47 13 #include "mbed.h"
hbujanda 5:7dd815802c47 14 #include "XBeeLib.h"
hbujanda 5:7dd815802c47 15 #if defined(ENABLE_LOGGING)
hbujanda 5:7dd815802c47 16 #include "DigiLoggerMbedSerial.h"
hbujanda 5:7dd815802c47 17 using namespace DigiLog;
hbujanda 5:7dd815802c47 18 #endif
hbujanda 5:7dd815802c47 19
hbujanda 5:7dd815802c47 20 #define REMOTE_NODE_ADDR64_MSB ((uint32_t)0x0013A200)
spastor 7:d685568a3bc8 21
spastor 7:d685568a3bc8 22 #error "Replace next define with the LSB of the remote module's 64-bit address (SL parameter)"
spastor 7:d685568a3bc8 23 #define REMOTE_NODE_ADDR64_LSB ((uint32_t)0x01234567)
hbujanda 5:7dd815802c47 24
hbujanda 5:7dd815802c47 25 #define REMOTE_NODE_ADDR64 UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB)
hbujanda 5:7dd815802c47 26
hbujanda 5:7dd815802c47 27 using namespace XBeeLib;
hbujanda 5:7dd815802c47 28
hbujanda 5:7dd815802c47 29 Serial *log_serial;
hbujanda 5:7dd815802c47 30
hbujanda 5:7dd815802c47 31 static void send_data_to_coordinator(XBeeZB& xbee)
hbujanda 5:7dd815802c47 32 {
hbujanda 5:7dd815802c47 33 const char data[] = "send_data_to_coordinator";
hbujanda 5:7dd815802c47 34 const uint16_t data_len = strlen(data);
hbujanda 5:7dd815802c47 35
hbujanda 5:7dd815802c47 36 const TxStatus txStatus = xbee.send_data_to_coordinator((const uint8_t *)data, data_len);
hbujanda 5:7dd815802c47 37 if (txStatus == TxStatusSuccess)
hbujanda 5:7dd815802c47 38 log_serial->printf("send_data_to_coordinator OK\r\n");
hbujanda 5:7dd815802c47 39 else
hbujanda 5:7dd815802c47 40 log_serial->printf("send_data_to_coordinator failed with %d\r\n", (int) txStatus);
hbujanda 5:7dd815802c47 41 }
hbujanda 5:7dd815802c47 42
hbujanda 5:7dd815802c47 43 static void send_broadcast_data(XBeeZB& xbee)
hbujanda 5:7dd815802c47 44 {
hbujanda 5:7dd815802c47 45 const char data[] = "send_broadcast_data";
hbujanda 5:7dd815802c47 46 const uint16_t data_len = strlen(data);
hbujanda 5:7dd815802c47 47
hbujanda 5:7dd815802c47 48 const TxStatus txStatus = xbee.send_data_broadcast((const uint8_t *)data, data_len);
hbujanda 5:7dd815802c47 49 if (txStatus == TxStatusSuccess)
hbujanda 5:7dd815802c47 50 log_serial->printf("send_broadcast_data OK\r\n");
hbujanda 5:7dd815802c47 51 else
hbujanda 5:7dd815802c47 52 log_serial->printf("send_broadcast_data failed with %d\r\n", (int) txStatus);
hbujanda 5:7dd815802c47 53 }
hbujanda 5:7dd815802c47 54
hbujanda 5:7dd815802c47 55 static void send_data_to_remote_node(XBeeZB& xbee, const RemoteXBeeZB& RemoteDevice)
hbujanda 5:7dd815802c47 56 {
hbujanda 5:7dd815802c47 57 const char data[] = "send_data_to_remote_node";
hbujanda 5:7dd815802c47 58 const uint16_t data_len = strlen(data);
hbujanda 5:7dd815802c47 59
hbujanda 5:7dd815802c47 60 const TxStatus txStatus = xbee.send_data(RemoteDevice, (const uint8_t *)data, data_len);
hbujanda 5:7dd815802c47 61 if (txStatus == TxStatusSuccess)
hbujanda 5:7dd815802c47 62 log_serial->printf("send_data_to_remote_node OK\r\n");
hbujanda 5:7dd815802c47 63 else
hbujanda 5:7dd815802c47 64 log_serial->printf("send_data_to_remote_node failed with %d\r\n", (int) txStatus);
hbujanda 5:7dd815802c47 65 }
hbujanda 5:7dd815802c47 66
hbujanda 5:7dd815802c47 67 static void send_explicit_data_to_remote_node(XBeeZB& xbee, const RemoteXBeeZB& RemoteDevice)
hbujanda 5:7dd815802c47 68 {
hbujanda 5:7dd815802c47 69 char data[] = "send_explicit_data_to_remote_node";
hbujanda 5:7dd815802c47 70 const uint16_t data_len = strlen(data);
hbujanda 5:7dd815802c47 71 const uint8_t dstEP = 0xE8;
hbujanda 5:7dd815802c47 72 const uint8_t srcEP = 0xE8;
hbujanda 5:7dd815802c47 73 const uint16_t clusterID = 0x0011;
hbujanda 5:7dd815802c47 74 const uint16_t profileID = 0xC105;
hbujanda 5:7dd815802c47 75
hbujanda 5:7dd815802c47 76 const TxStatus txStatus = xbee.send_data(RemoteDevice, dstEP, srcEP, clusterID, profileID, (const uint8_t *)data, data_len);
hbujanda 5:7dd815802c47 77 if (txStatus == TxStatusSuccess)
hbujanda 5:7dd815802c47 78 log_serial->printf("send_explicit_data_to_remote_node OK\r\n");
hbujanda 5:7dd815802c47 79 else
hbujanda 5:7dd815802c47 80 log_serial->printf("send_explicit_data_to_remote_node failed with %d\r\n", (int) txStatus);
hbujanda 5:7dd815802c47 81 }
hbujanda 5:7dd815802c47 82
hbujanda 5:7dd815802c47 83 int main()
hbujanda 5:7dd815802c47 84 {
hbujanda 5:7dd815802c47 85 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
hbujanda 5:7dd815802c47 86 log_serial->baud(9600);
hbujanda 5:7dd815802c47 87 log_serial->printf("Sample application to demo how to send unicast and broadcast data with the XBeeZB\r\n\r\n");
hbujanda 5:7dd815802c47 88 log_serial->printf(XB_LIB_BANNER);
hbujanda 5:7dd815802c47 89
hbujanda 5:7dd815802c47 90 #if defined(ENABLE_LOGGING)
hbujanda 5:7dd815802c47 91 new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
hbujanda 5:7dd815802c47 92 #endif
hbujanda 5:7dd815802c47 93
hbujanda 5:7dd815802c47 94 XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
hbujanda 5:7dd815802c47 95
hbujanda 5:7dd815802c47 96 RadioStatus radioStatus = xbee.init();
hbujanda 5:7dd815802c47 97 MBED_ASSERT(radioStatus == Success);
hbujanda 5:7dd815802c47 98
hbujanda 5:7dd815802c47 99 /* Wait until the device has joined the network */
hbujanda 5:7dd815802c47 100 log_serial->printf("Waiting for device to join the network: ");
hbujanda 5:7dd815802c47 101 while (!xbee.is_joined()) {
hbujanda 5:7dd815802c47 102 wait_ms(1000);
hbujanda 5:7dd815802c47 103 log_serial->printf(".");
hbujanda 5:7dd815802c47 104 }
hbujanda 5:7dd815802c47 105 log_serial->printf("OK\r\n");
hbujanda 5:7dd815802c47 106
hbujanda 5:7dd815802c47 107 const RemoteXBeeZB remoteDevice = RemoteXBeeZB(REMOTE_NODE_ADDR64);
hbujanda 5:7dd815802c47 108
hbujanda 5:7dd815802c47 109 send_data_to_coordinator(xbee);
hbujanda 5:7dd815802c47 110 send_broadcast_data(xbee);
hbujanda 5:7dd815802c47 111 send_data_to_remote_node(xbee, remoteDevice);
hbujanda 5:7dd815802c47 112 send_explicit_data_to_remote_node(xbee, remoteDevice);
hbujanda 5:7dd815802c47 113
hbujanda 5:7dd815802c47 114 delete(log_serial);
hbujanda 5:7dd815802c47 115 }