ZigBee Send Data example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * Copyright (c) 2015 Digi International Inc.,
00003  * All rights not expressly granted are reserved.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
00007  * You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
00010  * =======================================================================
00011  */
00012 
00013 #include "mbed.h"
00014 #include "XBeeLib.h"
00015 #if defined(ENABLE_LOGGING)
00016 #include "DigiLoggerMbedSerial.h"
00017 using namespace DigiLog;
00018 #endif
00019 
00020 #define REMOTE_NODE_ADDR64_MSB  ((uint32_t)0x0013A200)
00021 
00022 #error "Replace next define with the LSB of the remote module's 64-bit address (SL parameter)"
00023 #define REMOTE_NODE_ADDR64_LSB  ((uint32_t)0x01234567)
00024 
00025 #define REMOTE_NODE_ADDR64      UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB)
00026 
00027 using namespace XBeeLib;
00028 
00029 Serial *log_serial;
00030 
00031 static void send_data_to_coordinator(XBeeZB& xbee)
00032 {
00033     const char data[] = "send_data_to_coordinator";
00034     const uint16_t data_len = strlen(data);
00035 
00036     const TxStatus txStatus = xbee.send_data_to_coordinator((const uint8_t *)data, data_len);
00037     if (txStatus == TxStatusSuccess)
00038         log_serial->printf("send_data_to_coordinator OK\r\n");
00039     else
00040         log_serial->printf("send_data_to_coordinator failed with %d\r\n", (int) txStatus);
00041 }
00042 
00043 static void send_broadcast_data(XBeeZB& xbee)
00044 {
00045     const char data[] = "send_broadcast_data";
00046     const uint16_t data_len = strlen(data);
00047 
00048     const TxStatus txStatus = xbee.send_data_broadcast((const uint8_t *)data, data_len);
00049     if (txStatus == TxStatusSuccess)
00050         log_serial->printf("send_broadcast_data OK\r\n");
00051     else
00052         log_serial->printf("send_broadcast_data failed with %d\r\n", (int) txStatus);
00053 }
00054 
00055 static void send_data_to_remote_node(XBeeZB& xbee, const RemoteXBeeZB& RemoteDevice)
00056 {
00057     const char data[] = "send_data_to_remote_node";
00058     const uint16_t data_len = strlen(data);
00059 
00060     const TxStatus txStatus = xbee.send_data(RemoteDevice, (const uint8_t *)data, data_len);
00061     if (txStatus == TxStatusSuccess)
00062         log_serial->printf("send_data_to_remote_node OK\r\n");
00063     else
00064         log_serial->printf("send_data_to_remote_node failed with %d\r\n", (int) txStatus);
00065 }
00066 
00067 static void send_explicit_data_to_remote_node(XBeeZB& xbee, const RemoteXBeeZB& RemoteDevice)
00068 {
00069     char data[] = "send_explicit_data_to_remote_node";
00070     const uint16_t data_len = strlen(data);
00071     const uint8_t dstEP = 0xE8;
00072     const uint8_t srcEP = 0xE8;
00073     const uint16_t clusterID = 0x0011;
00074     const uint16_t profileID = 0xC105;
00075 
00076     const TxStatus txStatus = xbee.send_data(RemoteDevice, dstEP, srcEP, clusterID, profileID, (const uint8_t *)data, data_len);
00077     if (txStatus == TxStatusSuccess)
00078         log_serial->printf("send_explicit_data_to_remote_node OK\r\n");
00079     else
00080         log_serial->printf("send_explicit_data_to_remote_node failed with %d\r\n", (int) txStatus);
00081 }
00082 
00083 int main()
00084 {
00085     log_serial = new Serial(DEBUG_TX, DEBUG_RX);
00086     log_serial->baud(9600);
00087     log_serial->printf("Sample application to demo how to send unicast and broadcast data with the XBeeZB\r\n\r\n");
00088     log_serial->printf(XB_LIB_BANNER);
00089 
00090 #if defined(ENABLE_LOGGING)
00091     new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
00092 #endif
00093 
00094     XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
00095 
00096     RadioStatus radioStatus = xbee.init();
00097     MBED_ASSERT(radioStatus == Success);
00098 
00099     /* Wait until the device has joined the network */
00100     log_serial->printf("Waiting for device to join the network: ");
00101     while (!xbee.is_joined()) {
00102         wait_ms(1000);
00103         log_serial->printf(".");
00104     }
00105     log_serial->printf("OK\r\n");
00106 
00107     const RemoteXBeeZB remoteDevice = RemoteXBeeZB(REMOTE_NODE_ADDR64);
00108 
00109     send_data_to_coordinator(xbee);
00110     send_broadcast_data(xbee);
00111     send_data_to_remote_node(xbee, remoteDevice);
00112     send_explicit_data_to_remote_node(xbee, remoteDevice);
00113 
00114     delete(log_serial);
00115 }