ZigBee Node Discovery 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 #error "Replace next define with the remote module's Node Identifier (NI parameter)"
00021 #define REMOTE_NODE_ID              "MyNodeID"
00022 
00023 #define MAX_NODES                   5
00024 #define NODE_DISCOVERY_TIMEOUT_MS   5000
00025 
00026 using namespace XBeeLib;
00027 
00028 Serial *log_serial;
00029 XBeeZB * xbee = NULL;
00030 
00031 RemoteXBeeZB remote_nodes_in_network[MAX_NODES];
00032 unsigned int remote_nodes_count = 0;
00033 
00034 void discovery_function(const RemoteXBeeZB& remote, char const * const node_id)
00035 {
00036     MBED_ASSERT(xbee != NULL);
00037     const uint64_t remote_addr64 = remote.get_addr64();
00038 
00039     log_serial->printf("Found device '%s' [%08x:%08x][%04X]\r\n", node_id, UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16());
00040 
00041     if (remote_nodes_count < MAX_NODES) {
00042         remote_nodes_in_network[remote_nodes_count] = remote;
00043         remote_nodes_count++;
00044     } else {
00045         log_serial->printf("Found more nodes than maximum configured for example (%d)\r\n", MAX_NODES);
00046     }
00047 }
00048 
00049 int main()
00050 {
00051     log_serial = new Serial(DEBUG_TX, DEBUG_RX);
00052     log_serial->baud(9600);
00053     log_serial->printf("Sample application to demo how to discover other XBeeZB modules in the network and send a message to them\r\n\r\n");
00054     log_serial->printf(XB_LIB_BANNER);
00055 
00056 #if defined(ENABLE_LOGGING)
00057     new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
00058 #endif
00059 
00060     xbee = new XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
00061     RadioStatus radioStatus = xbee->init();
00062     MBED_ASSERT(radioStatus == Success);
00063 
00064     /* Wait until the device has joined the network */
00065     log_serial->printf("Waiting for device to join the network: ");
00066     while (!xbee->is_joined()) {
00067         wait_ms(1000);
00068         log_serial->printf(".");
00069     }
00070     log_serial->printf("OK\r\n");
00071 
00072     log_serial->printf("Configuring Node Discovery timeout to %d ms\r\n", NODE_DISCOVERY_TIMEOUT_MS);
00073     radioStatus = xbee->config_node_discovery(NODE_DISCOVERY_TIMEOUT_MS);
00074     if (radioStatus != Success) {
00075         log_serial->printf("Error on config_node_discovery()\r\n");
00076     }
00077 
00078     log_serial->printf("Trying to discover '%s' by its NI\r\n", REMOTE_NODE_ID);
00079     RemoteXBeeZB remote_node = xbee->get_remote_node_by_id(REMOTE_NODE_ID);
00080 
00081     if (remote_node.is_valid()) {
00082         const uint64_t addr64 = remote_node.get_addr64();
00083         log_serial->printf("Found '%s'! [%08x:%08x|%04x]\r\n", REMOTE_NODE_ID, UINT64_HI32(addr64), UINT64_LO32(addr64), remote_node.get_addr16());
00084 
00085         const char message[] = "Hello " REMOTE_NODE_ID "!";
00086         log_serial->printf("Sending data to remote device\r\n");
00087         const TxStatus txStatus = xbee->send_data(remote_node, (const uint8_t *)message, sizeof message - 1);
00088         if (txStatus != TxStatusSuccess) {
00089             log_serial->printf("Found an error while sending data %d\r\n", txStatus);
00090         }
00091     } else {
00092         log_serial->printf("Couldn't find '%s' node\r\n", REMOTE_NODE_ID);
00093     }
00094 
00095     /* Register callbacks */
00096     xbee->register_node_discovery_cb(&discovery_function);
00097 
00098     log_serial->printf("\r\nStarting Node Discovery\r\n\r\n");
00099     xbee->start_node_discovery();
00100 
00101     do {
00102         xbee->process_rx_frames();
00103         wait_ms(10);
00104     } while(xbee->is_node_discovery_in_progress());
00105     log_serial->printf("\r\nNode Discovery Finished\r\n\r\n");
00106 
00107     for (unsigned int i = 0; i < remote_nodes_count; i++) {
00108 
00109         RemoteXBeeZB remote = remote_nodes_in_network[i];
00110         const uint64_t remote_addr64 = remote.get_addr64();
00111 
00112         const char message[] = "Hello neighbor!";
00113         log_serial->printf("Sending data to remote device [%08x:%08x][%04X]\r\n", UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16());
00114         const TxStatus txStatus = xbee->send_data(remote, (const uint8_t *)message, sizeof message - 1);
00115         if (txStatus != TxStatusSuccess) {
00116             log_serial->printf("Found an error while sending data %d\r\n", txStatus);
00117         }
00118     }
00119 
00120     log_serial->printf("Example finished\r\n");
00121 
00122     delete(log_serial);
00123     delete(xbee);
00124     while (true) {
00125         wait_ms(10000);
00126     }
00127 }