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:
Mon May 11 17:59:14 2015 +0200
Revision:
3:44059a052f3a
Parent:
2:8c1955f15fdd
Automatic upload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hbujanda 0:7be0794fb24f 1 /**
hbujanda 0:7be0794fb24f 2 * Copyright (c) 2015 Digi International Inc.,
hbujanda 0:7be0794fb24f 3 * All rights not expressly granted are reserved.
hbujanda 0:7be0794fb24f 4 *
hbujanda 0:7be0794fb24f 5 * This Source Code Form is subject to the terms of the Mozilla Public
hbujanda 0:7be0794fb24f 6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
hbujanda 0:7be0794fb24f 7 * You can obtain one at http://mozilla.org/MPL/2.0/.
hbujanda 0:7be0794fb24f 8 *
hbujanda 0:7be0794fb24f 9 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
hbujanda 0:7be0794fb24f 10 * =======================================================================
hbujanda 0:7be0794fb24f 11 */
hbujanda 0:7be0794fb24f 12
hbujanda 0:7be0794fb24f 13 #include "mbed.h"
hbujanda 0:7be0794fb24f 14 #include "XBeeLib.h"
hbujanda 0:7be0794fb24f 15 #if defined(ENABLE_LOGGING)
hbujanda 0:7be0794fb24f 16 #include "DigiLoggerMbedSerial.h"
hbujanda 0:7be0794fb24f 17 using namespace DigiLog;
hbujanda 0:7be0794fb24f 18 #endif
hbujanda 0:7be0794fb24f 19
hbujanda 0:7be0794fb24f 20 using namespace XBeeLib;
hbujanda 0:7be0794fb24f 21
hbujanda 0:7be0794fb24f 22 Serial *log_serial;
hbujanda 0:7be0794fb24f 23
hbujanda 3:44059a052f3a 24 static RemoteXBee802 send_to = RemoteXBee802();
hbujanda 0:7be0794fb24f 25
hbujanda 0:7be0794fb24f 26 void discovery_function(const RemoteXBee802& remote, char const * const node_id)
hbujanda 0:7be0794fb24f 27 {
spastor 2:8c1955f15fdd 28 const uint64_t remote_addr64 = remote.get_addr64();
hbujanda 3:44059a052f3a 29 const uint64_t remote_addr16 = remote.get_addr16();
hbujanda 0:7be0794fb24f 30
hbujanda 3:44059a052f3a 31 log_serial->printf("Found device '%s' [%08x:%08x][%04x]\r\n", node_id, UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote_addr16);
hbujanda 3:44059a052f3a 32 if (send_to.is_valid()) {
hbujanda 3:44059a052f3a 33 log_serial->printf( "Skipping salutation to '%s' [%08x:%08x][%04x] because previous one has not been sent\r\n", node_id, UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote_addr16);
hbujanda 3:44059a052f3a 34 } else {
hbujanda 3:44059a052f3a 35 send_to = remote;
hbujanda 0:7be0794fb24f 36 }
hbujanda 0:7be0794fb24f 37 }
hbujanda 0:7be0794fb24f 38
hbujanda 0:7be0794fb24f 39 int main()
hbujanda 0:7be0794fb24f 40 {
hbujanda 0:7be0794fb24f 41 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
hbujanda 0:7be0794fb24f 42 log_serial->baud(9600);
hbujanda 0:7be0794fb24f 43 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 0:7be0794fb24f 44 log_serial->printf(XB_LIB_BANNER);
hbujanda 0:7be0794fb24f 45
hbujanda 0:7be0794fb24f 46 #if defined(ENABLE_LOGGING)
hbujanda 3:44059a052f3a 47 new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
hbujanda 0:7be0794fb24f 48 #endif
hbujanda 0:7be0794fb24f 49
hbujanda 3:44059a052f3a 50 XBee802 xbee = XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
hbujanda 3:44059a052f3a 51
hbujanda 0:7be0794fb24f 52 /* Register callbacks */
hbujanda 3:44059a052f3a 53 xbee.register_node_discovery_cb(&discovery_function);
spastor 2:8c1955f15fdd 54
hbujanda 3:44059a052f3a 55 RadioStatus const radioStatus = xbee.init();
spastor 2:8c1955f15fdd 56 MBED_ASSERT(radioStatus == Success);
spastor 2:8c1955f15fdd 57
hbujanda 0:7be0794fb24f 58 log_serial->printf("Starting Node Discovery\r\n");
hbujanda 3:44059a052f3a 59 xbee.start_node_discovery();
hbujanda 0:7be0794fb24f 60
hbujanda 0:7be0794fb24f 61 while (true) {
hbujanda 3:44059a052f3a 62 xbee.process_rx_frames();
hbujanda 3:44059a052f3a 63 if (send_to.is_valid()) {
hbujanda 3:44059a052f3a 64 const char message[] = "Hello neighbor!";
hbujanda 3:44059a052f3a 65 log_serial->printf("Sending data to remote device\r\n");
hbujanda 3:44059a052f3a 66 const TxStatus txStatus = xbee.send_data(send_to, (const uint8_t *)message, sizeof message - 1);
hbujanda 3:44059a052f3a 67 if (txStatus != TxStatusSuccess) {
hbujanda 3:44059a052f3a 68 log_serial->printf("Found an error while sending data %d\r\n", txStatus);
hbujanda 3:44059a052f3a 69 }
hbujanda 3:44059a052f3a 70 send_to = RemoteXBee802(); /* Set it to an uninitialized object again */
hbujanda 3:44059a052f3a 71 }
hbujanda 0:7be0794fb24f 72 wait_ms(10);
hbujanda 0:7be0794fb24f 73 }
hbujanda 0:7be0794fb24f 74
hbujanda 0:7be0794fb24f 75 delete(log_serial);
hbujanda 0:7be0794fb24f 76 }