DigiMesh Node Discovery example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Fork of XBeeZB_node_discovery by Digi International Inc.

Description

This example shows how to discover remote XBee modules based on their NI (Node Identifier) instead of using its 64-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.

Once joined to the coordinator, 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:
2:5e41a845f211
Parent:
0:52b14877e9eb
Child:
3:ee7b06215329
--- a/main.cpp	Tue May 05 18:30:27 2015 +0200
+++ b/main.cpp	Fri May 08 11:53:15 2015 +0200
@@ -17,24 +17,27 @@
 using namespace DigiLog;
 #endif
 
+#define REMOTE_NODE_ID      "MyNodeID" /* TODO: Write a node's NI here*/
+
 using namespace XBeeLib;
 
 Serial *log_serial;
 
-static RemoteXBeeZB send_to = RemoteXBeeZB();
+XBeeZB * xbee = NULL;
 
 void discovery_function(const RemoteXBeeZB& remote, char const * const node_id)
 {
-    Addr64 addr64;
-    uint16_t addr16;
-    remote.get_addr(&addr64);
-    remote.get_addr(&addr16);
+    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, addr64.get_high32(), addr64.get_low32(), addr16);
-    if (send_to.is_valid()) {
-        log_serial->printf( "Skipping salutation to '%s' [%08X:%08X][%04X] because previous one has not been sent\r\n", node_id, addr64.get_high32(), addr64.get_low32(), addr16);
-    } else {
-        send_to = remote;
+    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);
     }
 }
 
@@ -49,36 +52,46 @@
     new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
 #endif
 
-    XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
+    xbee = new XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
 
     /* Register callbacks */
-    xbee.register_node_discovery_cb(&discovery_function);
+    xbee->register_node_discovery_cb(&discovery_function);
 
-    RadioStatus const radioStatus = xbee.init();
+    RadioStatus const radioStatus = xbee->init();
     MBED_ASSERT(radioStatus == Success);
 
     /* Wait until the device has joined the network */
     log_serial->printf("Waiting for device to join the network: ");
-    while (!xbee.is_joined()) {
+    while (!xbee->is_joined()) {
         wait_ms(1000);
         log_serial->printf(".");
     }
     log_serial->printf("OK\r\n");
 
     log_serial->printf("Starting Node Discovery\r\n");
-    xbee.start_node_discovery();
+
+    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);
+
+    if (remote_node.is_valid()) {
+        const uint64_t addr64 = remote_node.get_addr64();
+        log_serial->printf("Found '%s'! [%08x:%08x|%04x]\r\n", REMOTE_NODE_ID, UINT64_HI32(addr64), UINT64_LO32(addr64), remote_node.get_addr16());
+
+        const char message[] = "Hello " REMOTE_NODE_ID "!";
+        log_serial->printf("Sending data to remote device\r\n");
+        const TxStatus txStatus = xbee->send_data(remote_node, (const uint8_t *)message, sizeof message - 1);
+        if (txStatus != TxStatusSuccess) {
+            log_serial->printf("Found an error while sending data %d\r\n", txStatus);
+        }
+    } else {
+        log_serial->printf("Couldn't find '%s' node\r\n", REMOTE_NODE_ID);
+    }
+
+    log_serial->printf("Starting Node Discovery\r\n");
+    xbee->start_node_discovery();
 
     while (true) {
-        xbee.process_rx_frames();
-        if (send_to.is_valid()) {
-            const char message[] = "Hello neighbor!";
-            log_serial->printf("Sending data to remote device\r\n");
-            const TxStatus txStatus = xbee.send_data(send_to, (const uint8_t *)message, sizeof message - 1);
-            if (txStatus != TxStatusSuccess) {
-                log_serial->printf("Found an error while sending data %d\r\n", txStatus);
-            }
-            send_to = RemoteXBeeZB(); /* Set it to an uninitialized object again */
-        }
+        xbee->process_rx_frames();
         wait_ms(10);
     }