Digi International Inc. / Mbed 2 deprecated XBeeDM_node_discovery

Dependencies:   XBeeLib mbed

Fork of XBeeZB_node_discovery by Digi International Inc.

Files at this revision

API Documentation at this revision

Comitter:
spastor
Date:
Mon Jun 01 19:01:41 2015 +0200
Parent:
6:51c5ad956ea9
Child:
8:96103dbc8063
Commit message:
Automatic upload

Changed in this revision

XBeeLib.lib Show annotated file Show diff for this revision Revisions of this file
config.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/XBeeLib.lib	Mon May 18 13:19:14 2015 +0200
+++ b/XBeeLib.lib	Mon Jun 01 19:01:41 2015 +0200
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/Digi-International-Inc/code/XBeeLib/#8662ebe83570
+http://developer.mbed.org/teams/Digi-International-Inc/code/XBeeLib/#629712865107
--- a/config.h	Mon May 18 13:19:14 2015 +0200
+++ b/config.h	Mon Jun 01 19:01:41 2015 +0200
@@ -18,7 +18,6 @@
 #define ENABLE_ASSERTIONS
 #define FRAME_BUFFER_SIZE           4
 #define MAX_FRAME_PAYLOAD_LEN       128
-#define ENABLE_PM_SUPPORT
 
 #define SYNC_OPS_TIMEOUT_MS         2000
 
--- a/main.cpp	Mon May 18 13:19:14 2015 +0200
+++ b/main.cpp	Mon Jun 01 19:01:41 2015 +0200
@@ -17,27 +17,32 @@
 using namespace DigiLog;
 #endif
 
-#define REMOTE_NODE_ID      "MyNodeID" /* TODO: Write a node's NI here*/
+#error "Replace next define with the remote module's Node Identifier (NI parameter)"
+#define REMOTE_NODE_ID              "MyNodeID"
+
+#define MAX_NODES                   5
+#define NODE_DISCOVERY_TIMEOUT_MS   5000
 
 using namespace XBeeLib;
 
 Serial *log_serial;
+XBeeZB * xbee = NULL;
 
-XBeeZB * xbee = NULL;
+RemoteXBeeZB remote_nodes_in_network[MAX_NODES];
+unsigned int remote_nodes_count = 0;
 
 void discovery_function(const RemoteXBeeZB& remote, char const * const node_id)
 {
     MBED_ASSERT(xbee != NULL);
-
     const uint64_t remote_addr64 = remote.get_addr64();
 
-    log_serial->printf("Found device '%s' [%08x:%08x][%04X] sending a message to it\r\n", node_id, UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16());
+    log_serial->printf("Found device '%s' [%08x:%08x][%04X]\r\n", node_id, UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16());
 
-    const char message[] = "Hello neighbor!";
-    log_serial->printf("Sending data to remote device\r\n");
-    const TxStatus txStatus = xbee->send_data(remote, (const uint8_t *)message, sizeof message - 1);
-    if (txStatus != TxStatusSuccess) {
-        log_serial->printf("Found an error while sending data %d\r\n", txStatus);
+    if (remote_nodes_count < MAX_NODES) {
+        remote_nodes_in_network[remote_nodes_count] = remote;
+        remote_nodes_count++;
+    } else {
+        log_serial->printf("Found more nodes than maximum configured for example (%d)\r\n", MAX_NODES);
     }
 }
 
@@ -45,7 +50,7 @@
 {
     log_serial = new Serial(DEBUG_TX, DEBUG_RX);
     log_serial->baud(9600);
-    log_serial->printf("Sample application to demo how to discover nodes in the ZigBee network and send a message to them\r\n\r\n");
+    log_serial->printf("Sample application to demo how to discover other XBee 802.15.4 modules in the network and send a message to them\r\n\r\n");
     log_serial->printf(XB_LIB_BANNER);
 
 #if defined(ENABLE_LOGGING)
@@ -53,11 +58,7 @@
 #endif
 
     xbee = new XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
-
-    /* Register callbacks */
-    xbee->register_node_discovery_cb(&discovery_function);
-
-    RadioStatus const radioStatus = xbee->init();
+    RadioStatus radioStatus = xbee->init();
     MBED_ASSERT(radioStatus == Success);
 
     /* Wait until the device has joined the network */
@@ -68,7 +69,11 @@
     }
     log_serial->printf("OK\r\n");
 
-    log_serial->printf("Starting Node Discovery\r\n");
+    log_serial->printf("Configuring Node Discovery timeout to %d ms\r\n", NODE_DISCOVERY_TIMEOUT_MS);
+    radioStatus = xbee->config_node_discovery(NODE_DISCOVERY_TIMEOUT_MS);
+    if (radioStatus != Success) {
+        log_serial->printf("Error on config_node_discovery()\r\n");
+    }
 
     log_serial->printf("Trying to discover '%s' by its NI\r\n", REMOTE_NODE_ID);
     RemoteXBeeZB remote_node = xbee->get_remote_node_by_id(REMOTE_NODE_ID);
@@ -87,13 +92,36 @@
         log_serial->printf("Couldn't find '%s' node\r\n", REMOTE_NODE_ID);
     }
 
-    log_serial->printf("Starting Node Discovery\r\n");
+    /* Register callbacks */
+    xbee->register_node_discovery_cb(&discovery_function);
+
+    log_serial->printf("\r\nStarting Node Discovery\r\n\r\n");
     xbee->start_node_discovery();
 
-    while (true) {
+    do {
         xbee->process_rx_frames();
         wait_ms(10);
+    } while(xbee->is_node_discovery_in_progress());
+    log_serial->printf("\r\nNode Discovery Finished\r\n\r\n");
+
+    for (unsigned int i = 0; i < remote_nodes_count; i++) {
+
+        RemoteXBeeZB remote = remote_nodes_in_network[i];
+        const uint64_t remote_addr64 = remote.get_addr64();
+
+        const char message[] = "Hello neighbor!";
+        log_serial->printf("Sending data to remote device [%08x:%08x][%04X]\r\n", UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16());
+        const TxStatus txStatus = xbee->send_data(remote, (const uint8_t *)message, sizeof message - 1);
+        if (txStatus != TxStatusSuccess) {
+            log_serial->printf("Found an error while sending data %d\r\n", txStatus);
+        }
     }
 
+    log_serial->printf("Example finished\r\n");
+
     delete(log_serial);
+    delete(xbee);
+    while (true) {
+        wait_ms(10000);
+    }
 }