802.15.4 Node Discovery example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Description

This example shows how to discover remote XBee modules based on their NI (Node Identifier) instead of using its 64-bit or 16-bit network address

The example demonstrates the two ways of discovering nodes:

  • Search for all nodes to identify
  • Search for only one node

See Discovering nodes in the network chapter for more information.

Common Setup

Make sure you have a valid Example Common Setup

Example Setup

You need to have your remote XBee module with a valid Node Identifier "NI" (Remember that by default its value is a space). Then customize the REMOTE_NODE_ID define in the application with that value.

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 try to discover the remote XBee module with NI equal to the defined REMOTE_NODE_ID. If found, it will send it a 'hello' message. Check in the "Console" tab of the X-CTU connected to the remote XBee module that the message arrived.
Then a node discovery function callback will be registered and a node discovery performed. All remote XBee modules in the network should answer. This library will process each answer and call the registered callback. The callback sends a 'Hello neighbor!' message to each discovered remote XBee module. Check in the "Console" tab of the X-CTU connected to the remote XBee module that the message arrived.

Revision:
7:702ecf64ffcd
Parent:
5:f5ecaeb53632
Child:
8:dbff8c266dc5
--- a/main.cpp	Mon May 18 13:18:05 2015 +0200
+++ b/main.cpp	Mon Jun 01 19:00:42 2015 +0200
@@ -17,26 +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;
+XBee802 * xbee = NULL;
 
-XBee802 * xbee = NULL;
+RemoteXBee802 remote_nodes_in_network[MAX_NODES];
+unsigned int remote_nodes_count = 0;
 
 void discovery_function(const RemoteXBee802& 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);
     }
 }
 
@@ -48,15 +54,19 @@
     log_serial->printf(XB_LIB_BANNER);
 
 #if defined(ENABLE_LOGGING)
-    new DigiLoggerMbedSerial(log_serial, LogLevelDebug);
+    new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
 #endif
 
     xbee = new XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
-    /* Register callbacks */
-    xbee->register_node_discovery_cb(&discovery_function);
+
+    RadioStatus radioStatus = xbee->init();
+    MBED_ASSERT(radioStatus == Success);
 
-    RadioStatus const radioStatus = xbee->init();
-    MBED_ASSERT(radioStatus == Success);
+    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);
     RemoteXBee802 remote_node = xbee->get_remote_node_by_id(REMOTE_NODE_ID);
@@ -75,13 +85,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++) {
+
+        RemoteXBee802 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);
+    }
 }