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.

Committer:
hbujanda
Date:
Thu May 14 16:22:54 2015 +0200
Revision:
5:f5ecaeb53632
Child:
7:702ecf64ffcd
Automatic upload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hbujanda 5:f5ecaeb53632 1 /**
hbujanda 5:f5ecaeb53632 2 * Copyright (c) 2015 Digi International Inc.,
hbujanda 5:f5ecaeb53632 3 * All rights not expressly granted are reserved.
hbujanda 5:f5ecaeb53632 4 *
hbujanda 5:f5ecaeb53632 5 * This Source Code Form is subject to the terms of the Mozilla Public
hbujanda 5:f5ecaeb53632 6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
hbujanda 5:f5ecaeb53632 7 * You can obtain one at http://mozilla.org/MPL/2.0/.
hbujanda 5:f5ecaeb53632 8 *
hbujanda 5:f5ecaeb53632 9 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
hbujanda 5:f5ecaeb53632 10 * =======================================================================
hbujanda 5:f5ecaeb53632 11 */
hbujanda 5:f5ecaeb53632 12
hbujanda 5:f5ecaeb53632 13 #include "mbed.h"
hbujanda 5:f5ecaeb53632 14 #include "XBeeLib.h"
hbujanda 5:f5ecaeb53632 15 #if defined(ENABLE_LOGGING)
hbujanda 5:f5ecaeb53632 16 #include "DigiLoggerMbedSerial.h"
hbujanda 5:f5ecaeb53632 17 using namespace DigiLog;
hbujanda 5:f5ecaeb53632 18 #endif
hbujanda 5:f5ecaeb53632 19
hbujanda 5:f5ecaeb53632 20 #define REMOTE_NODE_ID "MyNodeID" /* TODO: Write a node's NI here*/
hbujanda 5:f5ecaeb53632 21
hbujanda 5:f5ecaeb53632 22 using namespace XBeeLib;
hbujanda 5:f5ecaeb53632 23
hbujanda 5:f5ecaeb53632 24 Serial *log_serial;
hbujanda 5:f5ecaeb53632 25
hbujanda 5:f5ecaeb53632 26 XBee802 * xbee = NULL;
hbujanda 5:f5ecaeb53632 27
hbujanda 5:f5ecaeb53632 28 void discovery_function(const RemoteXBee802& remote, char const * const node_id)
hbujanda 5:f5ecaeb53632 29 {
hbujanda 5:f5ecaeb53632 30 MBED_ASSERT(xbee != NULL);
hbujanda 5:f5ecaeb53632 31 const uint64_t remote_addr64 = remote.get_addr64();
hbujanda 5:f5ecaeb53632 32
hbujanda 5:f5ecaeb53632 33 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());
hbujanda 5:f5ecaeb53632 34
hbujanda 5:f5ecaeb53632 35 const char message[] = "Hello neighbor!";
hbujanda 5:f5ecaeb53632 36 log_serial->printf("Sending data to remote device\r\n");
hbujanda 5:f5ecaeb53632 37 const TxStatus txStatus = xbee->send_data(remote, (const uint8_t *)message, sizeof message - 1);
hbujanda 5:f5ecaeb53632 38 if (txStatus != TxStatusSuccess) {
hbujanda 5:f5ecaeb53632 39 log_serial->printf("Found an error while sending data %d\r\n", txStatus);
hbujanda 5:f5ecaeb53632 40 }
hbujanda 5:f5ecaeb53632 41 }
hbujanda 5:f5ecaeb53632 42
hbujanda 5:f5ecaeb53632 43 int main()
hbujanda 5:f5ecaeb53632 44 {
hbujanda 5:f5ecaeb53632 45 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
hbujanda 5:f5ecaeb53632 46 log_serial->baud(9600);
hbujanda 5:f5ecaeb53632 47 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");
hbujanda 5:f5ecaeb53632 48 log_serial->printf(XB_LIB_BANNER);
hbujanda 5:f5ecaeb53632 49
hbujanda 5:f5ecaeb53632 50 #if defined(ENABLE_LOGGING)
hbujanda 5:f5ecaeb53632 51 new DigiLoggerMbedSerial(log_serial, LogLevelDebug);
hbujanda 5:f5ecaeb53632 52 #endif
hbujanda 5:f5ecaeb53632 53
hbujanda 5:f5ecaeb53632 54 xbee = new XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
hbujanda 5:f5ecaeb53632 55 /* Register callbacks */
hbujanda 5:f5ecaeb53632 56 xbee->register_node_discovery_cb(&discovery_function);
hbujanda 5:f5ecaeb53632 57
hbujanda 5:f5ecaeb53632 58 RadioStatus const radioStatus = xbee->init();
hbujanda 5:f5ecaeb53632 59 MBED_ASSERT(radioStatus == Success);
hbujanda 5:f5ecaeb53632 60
hbujanda 5:f5ecaeb53632 61 log_serial->printf("Trying to discover '%s' by its NI\r\n", REMOTE_NODE_ID);
hbujanda 5:f5ecaeb53632 62 RemoteXBee802 remote_node = xbee->get_remote_node_by_id(REMOTE_NODE_ID);
hbujanda 5:f5ecaeb53632 63
hbujanda 5:f5ecaeb53632 64 if (remote_node.is_valid()) {
hbujanda 5:f5ecaeb53632 65 const uint64_t addr64 = remote_node.get_addr64();
hbujanda 5:f5ecaeb53632 66 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:f5ecaeb53632 67
hbujanda 5:f5ecaeb53632 68 const char message[] = "Hello " REMOTE_NODE_ID "!";
hbujanda 5:f5ecaeb53632 69 log_serial->printf("Sending data to remote device\r\n");
hbujanda 5:f5ecaeb53632 70 const TxStatus txStatus = xbee->send_data(remote_node, (const uint8_t *)message, sizeof message - 1);
hbujanda 5:f5ecaeb53632 71 if (txStatus != TxStatusSuccess) {
hbujanda 5:f5ecaeb53632 72 log_serial->printf("Found an error while sending data %d\r\n", txStatus);
hbujanda 5:f5ecaeb53632 73 }
hbujanda 5:f5ecaeb53632 74 } else {
hbujanda 5:f5ecaeb53632 75 log_serial->printf("Couldn't find '%s' node\r\n", REMOTE_NODE_ID);
hbujanda 5:f5ecaeb53632 76 }
hbujanda 5:f5ecaeb53632 77
hbujanda 5:f5ecaeb53632 78 log_serial->printf("Starting Node Discovery\r\n");
hbujanda 5:f5ecaeb53632 79 xbee->start_node_discovery();
hbujanda 5:f5ecaeb53632 80
hbujanda 5:f5ecaeb53632 81 while (true) {
hbujanda 5:f5ecaeb53632 82 xbee->process_rx_frames();
hbujanda 5:f5ecaeb53632 83 wait_ms(10);
hbujanda 5:f5ecaeb53632 84 }
hbujanda 5:f5ecaeb53632 85
hbujanda 5:f5ecaeb53632 86 delete(log_serial);
hbujanda 5:f5ecaeb53632 87 }