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.

main.cpp

Committer:
hbujanda
Date:
2016-07-29
Revision:
11:4d916ff3fa2c
Parent:
10:4b97421d51af

File content as of revision 11:4d916ff3fa2c:

/**
 * Copyright (c) 2015 Digi International Inc.,
 * All rights not expressly granted are reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
 * =======================================================================
 */

#include "mbed.h"
#include "XBeeLib.h"
#if defined(ENABLE_LOGGING)
#include "DigiLoggerMbedSerial.h"
using namespace DigiLog;
#endif

#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;
XBeeDM * xbee = NULL;

RemoteXBeeDM remote_nodes_in_network[MAX_NODES];
unsigned int remote_nodes_count = 0;

void discovery_function(const RemoteXBeeDM& 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]\r\n", node_id, UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64));

    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);
    }
}

int main()
{
    log_serial = new Serial(DEBUG_TX, DEBUG_RX);
    log_serial->baud(9600);
    log_serial->printf("Sample application to demo how to discover other XBeeDM modules in the network and send a message to them\r\n\r\n");
    log_serial->printf(XB_LIB_BANNER);

#if defined(ENABLE_LOGGING)
    new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
#endif

    xbee = new XBeeDM(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
    RadioStatus 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);
    RemoteXBeeDM 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]\r\n", REMOTE_NODE_ID, UINT64_HI32(addr64), UINT64_LO32(addr64));

        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);
    }

    /* Register callbacks */
    xbee->register_node_discovery_cb(&discovery_function);

    log_serial->printf("\r\nStarting Node Discovery\r\n\r\n");
    xbee->start_node_discovery();

    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++) {

        RemoteXBeeDM 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]\r\n", UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64));
        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);
    }
}