ZigBee 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 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.

Committer:
hbujanda
Date:
Fri Jul 29 12:13:24 2016 +0200
Revision:
10:5d61da9a78ec
Parent:
9:e2a54d5c81a7
Automatic upload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hbujanda 5:6916f9c7bdb0 1 /**
hbujanda 5:6916f9c7bdb0 2 * Copyright (c) 2015 Digi International Inc.,
hbujanda 5:6916f9c7bdb0 3 * All rights not expressly granted are reserved.
hbujanda 5:6916f9c7bdb0 4 *
hbujanda 5:6916f9c7bdb0 5 * This Source Code Form is subject to the terms of the Mozilla Public
hbujanda 5:6916f9c7bdb0 6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
hbujanda 5:6916f9c7bdb0 7 * You can obtain one at http://mozilla.org/MPL/2.0/.
hbujanda 5:6916f9c7bdb0 8 *
hbujanda 5:6916f9c7bdb0 9 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
hbujanda 5:6916f9c7bdb0 10 * =======================================================================
hbujanda 5:6916f9c7bdb0 11 */
hbujanda 5:6916f9c7bdb0 12
hbujanda 5:6916f9c7bdb0 13 #include "mbed.h"
hbujanda 5:6916f9c7bdb0 14 #include "XBeeLib.h"
hbujanda 5:6916f9c7bdb0 15 #if defined(ENABLE_LOGGING)
hbujanda 5:6916f9c7bdb0 16 #include "DigiLoggerMbedSerial.h"
hbujanda 5:6916f9c7bdb0 17 using namespace DigiLog;
hbujanda 5:6916f9c7bdb0 18 #endif
hbujanda 5:6916f9c7bdb0 19
hbujanda 9:e2a54d5c81a7 20 #error "Replace next define with the remote module's Node Identifier (NI parameter)"
hbujanda 9:e2a54d5c81a7 21 #define REMOTE_NODE_ID "MyNodeID"
spastor 7:694d412d8a05 22
spastor 7:694d412d8a05 23 #define MAX_NODES 5
spastor 7:694d412d8a05 24 #define NODE_DISCOVERY_TIMEOUT_MS 5000
hbujanda 5:6916f9c7bdb0 25
hbujanda 5:6916f9c7bdb0 26 using namespace XBeeLib;
hbujanda 5:6916f9c7bdb0 27
hbujanda 5:6916f9c7bdb0 28 Serial *log_serial;
spastor 7:694d412d8a05 29 XBeeZB * xbee = NULL;
hbujanda 5:6916f9c7bdb0 30
spastor 7:694d412d8a05 31 RemoteXBeeZB remote_nodes_in_network[MAX_NODES];
spastor 7:694d412d8a05 32 unsigned int remote_nodes_count = 0;
hbujanda 5:6916f9c7bdb0 33
hbujanda 5:6916f9c7bdb0 34 void discovery_function(const RemoteXBeeZB& remote, char const * const node_id)
hbujanda 5:6916f9c7bdb0 35 {
hbujanda 5:6916f9c7bdb0 36 MBED_ASSERT(xbee != NULL);
hbujanda 5:6916f9c7bdb0 37 const uint64_t remote_addr64 = remote.get_addr64();
hbujanda 5:6916f9c7bdb0 38
spastor 7:694d412d8a05 39 log_serial->printf("Found device '%s' [%08x:%08x][%04X]\r\n", node_id, UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16());
hbujanda 5:6916f9c7bdb0 40
spastor 7:694d412d8a05 41 if (remote_nodes_count < MAX_NODES) {
spastor 7:694d412d8a05 42 remote_nodes_in_network[remote_nodes_count] = remote;
spastor 7:694d412d8a05 43 remote_nodes_count++;
spastor 7:694d412d8a05 44 } else {
spastor 7:694d412d8a05 45 log_serial->printf("Found more nodes than maximum configured for example (%d)\r\n", MAX_NODES);
hbujanda 5:6916f9c7bdb0 46 }
hbujanda 5:6916f9c7bdb0 47 }
hbujanda 5:6916f9c7bdb0 48
hbujanda 5:6916f9c7bdb0 49 int main()
hbujanda 5:6916f9c7bdb0 50 {
hbujanda 5:6916f9c7bdb0 51 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
hbujanda 5:6916f9c7bdb0 52 log_serial->baud(9600);
hbujanda 9:e2a54d5c81a7 53 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");
hbujanda 5:6916f9c7bdb0 54 log_serial->printf(XB_LIB_BANNER);
hbujanda 5:6916f9c7bdb0 55
hbujanda 5:6916f9c7bdb0 56 #if defined(ENABLE_LOGGING)
hbujanda 5:6916f9c7bdb0 57 new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
hbujanda 5:6916f9c7bdb0 58 #endif
hbujanda 5:6916f9c7bdb0 59
hbujanda 5:6916f9c7bdb0 60 xbee = new XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
spastor 7:694d412d8a05 61 RadioStatus radioStatus = xbee->init();
hbujanda 5:6916f9c7bdb0 62 MBED_ASSERT(radioStatus == Success);
hbujanda 5:6916f9c7bdb0 63
hbujanda 5:6916f9c7bdb0 64 /* Wait until the device has joined the network */
hbujanda 5:6916f9c7bdb0 65 log_serial->printf("Waiting for device to join the network: ");
hbujanda 5:6916f9c7bdb0 66 while (!xbee->is_joined()) {
hbujanda 5:6916f9c7bdb0 67 wait_ms(1000);
hbujanda 5:6916f9c7bdb0 68 log_serial->printf(".");
hbujanda 5:6916f9c7bdb0 69 }
hbujanda 5:6916f9c7bdb0 70 log_serial->printf("OK\r\n");
hbujanda 5:6916f9c7bdb0 71
spastor 7:694d412d8a05 72 log_serial->printf("Configuring Node Discovery timeout to %d ms\r\n", NODE_DISCOVERY_TIMEOUT_MS);
spastor 7:694d412d8a05 73 radioStatus = xbee->config_node_discovery(NODE_DISCOVERY_TIMEOUT_MS);
spastor 7:694d412d8a05 74 if (radioStatus != Success) {
spastor 7:694d412d8a05 75 log_serial->printf("Error on config_node_discovery()\r\n");
spastor 7:694d412d8a05 76 }
hbujanda 5:6916f9c7bdb0 77
hbujanda 5:6916f9c7bdb0 78 log_serial->printf("Trying to discover '%s' by its NI\r\n", REMOTE_NODE_ID);
hbujanda 5:6916f9c7bdb0 79 RemoteXBeeZB remote_node = xbee->get_remote_node_by_id(REMOTE_NODE_ID);
hbujanda 5:6916f9c7bdb0 80
hbujanda 5:6916f9c7bdb0 81 if (remote_node.is_valid()) {
hbujanda 5:6916f9c7bdb0 82 const uint64_t addr64 = remote_node.get_addr64();
hbujanda 5:6916f9c7bdb0 83 log_serial->printf("Found '%s'! [%08x:%08x|%04x]\r\n", REMOTE_NODE_ID, UINT64_HI32(addr64), UINT64_LO32(addr64), remote_node.get_addr16());
hbujanda 5:6916f9c7bdb0 84
hbujanda 5:6916f9c7bdb0 85 const char message[] = "Hello " REMOTE_NODE_ID "!";
hbujanda 5:6916f9c7bdb0 86 log_serial->printf("Sending data to remote device\r\n");
hbujanda 5:6916f9c7bdb0 87 const TxStatus txStatus = xbee->send_data(remote_node, (const uint8_t *)message, sizeof message - 1);
hbujanda 5:6916f9c7bdb0 88 if (txStatus != TxStatusSuccess) {
hbujanda 5:6916f9c7bdb0 89 log_serial->printf("Found an error while sending data %d\r\n", txStatus);
hbujanda 5:6916f9c7bdb0 90 }
hbujanda 5:6916f9c7bdb0 91 } else {
hbujanda 5:6916f9c7bdb0 92 log_serial->printf("Couldn't find '%s' node\r\n", REMOTE_NODE_ID);
hbujanda 5:6916f9c7bdb0 93 }
hbujanda 5:6916f9c7bdb0 94
spastor 7:694d412d8a05 95 /* Register callbacks */
spastor 7:694d412d8a05 96 xbee->register_node_discovery_cb(&discovery_function);
spastor 7:694d412d8a05 97
spastor 7:694d412d8a05 98 log_serial->printf("\r\nStarting Node Discovery\r\n\r\n");
hbujanda 5:6916f9c7bdb0 99 xbee->start_node_discovery();
hbujanda 5:6916f9c7bdb0 100
spastor 7:694d412d8a05 101 do {
hbujanda 5:6916f9c7bdb0 102 xbee->process_rx_frames();
hbujanda 5:6916f9c7bdb0 103 wait_ms(10);
spastor 7:694d412d8a05 104 } while(xbee->is_node_discovery_in_progress());
spastor 7:694d412d8a05 105 log_serial->printf("\r\nNode Discovery Finished\r\n\r\n");
spastor 7:694d412d8a05 106
spastor 7:694d412d8a05 107 for (unsigned int i = 0; i < remote_nodes_count; i++) {
spastor 7:694d412d8a05 108
spastor 7:694d412d8a05 109 RemoteXBeeZB remote = remote_nodes_in_network[i];
spastor 7:694d412d8a05 110 const uint64_t remote_addr64 = remote.get_addr64();
spastor 7:694d412d8a05 111
spastor 7:694d412d8a05 112 const char message[] = "Hello neighbor!";
spastor 7:694d412d8a05 113 log_serial->printf("Sending data to remote device [%08x:%08x][%04X]\r\n", UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16());
spastor 7:694d412d8a05 114 const TxStatus txStatus = xbee->send_data(remote, (const uint8_t *)message, sizeof message - 1);
spastor 7:694d412d8a05 115 if (txStatus != TxStatusSuccess) {
spastor 7:694d412d8a05 116 log_serial->printf("Found an error while sending data %d\r\n", txStatus);
spastor 7:694d412d8a05 117 }
hbujanda 5:6916f9c7bdb0 118 }
hbujanda 5:6916f9c7bdb0 119
spastor 7:694d412d8a05 120 log_serial->printf("Example finished\r\n");
spastor 7:694d412d8a05 121
hbujanda 5:6916f9c7bdb0 122 delete(log_serial);
spastor 7:694d412d8a05 123 delete(xbee);
spastor 7:694d412d8a05 124 while (true) {
spastor 7:694d412d8a05 125 wait_ms(10000);
spastor 7:694d412d8a05 126 }
hbujanda 5:6916f9c7bdb0 127 }