DigiMesh Send Data example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Fork of XBeeZB_Send_Data by Digi International Inc.

Description

This example shows how to send data to a remote XBeeDM 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

Application

Broadcast messages doesn'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. The application will first send 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.

Committer:
hbujanda
Date:
Wed Apr 29 17:59:24 2015 +0200
Revision:
0:6cb09a6542ff
Child:
2:e17ec1d60e80
Automatic upload

Who changed what in which revision?

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