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.

Committer:
hbujanda
Date:
Wed Apr 29 18:11:56 2015 +0200
Revision:
0:52b14877e9eb
Child:
2:5e41a845f211
Automatic upload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hbujanda 0:52b14877e9eb 1 /**
hbujanda 0:52b14877e9eb 2 * Copyright (c) 2015 Digi International Inc.,
hbujanda 0:52b14877e9eb 3 * All rights not expressly granted are reserved.
hbujanda 0:52b14877e9eb 4 *
hbujanda 0:52b14877e9eb 5 * This Source Code Form is subject to the terms of the Mozilla Public
hbujanda 0:52b14877e9eb 6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
hbujanda 0:52b14877e9eb 7 * You can obtain one at http://mozilla.org/MPL/2.0/.
hbujanda 0:52b14877e9eb 8 *
hbujanda 0:52b14877e9eb 9 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
hbujanda 0:52b14877e9eb 10 * =======================================================================
hbujanda 0:52b14877e9eb 11 */
hbujanda 0:52b14877e9eb 12
hbujanda 0:52b14877e9eb 13 #include "mbed.h"
hbujanda 0:52b14877e9eb 14 #include "XBeeLib.h"
hbujanda 0:52b14877e9eb 15 #if defined(ENABLE_LOGGING)
hbujanda 0:52b14877e9eb 16 #include "DigiLoggerMbedSerial.h"
hbujanda 0:52b14877e9eb 17 using namespace DigiLog;
hbujanda 0:52b14877e9eb 18 #endif
hbujanda 0:52b14877e9eb 19
hbujanda 0:52b14877e9eb 20 using namespace XBeeLib;
hbujanda 0:52b14877e9eb 21
hbujanda 0:52b14877e9eb 22 Serial *log_serial;
hbujanda 0:52b14877e9eb 23
hbujanda 0:52b14877e9eb 24 static RemoteXBeeZB send_to = RemoteXBeeZB();
hbujanda 0:52b14877e9eb 25
hbujanda 0:52b14877e9eb 26 void discovery_function(const RemoteXBeeZB& remote, char const * const node_id)
hbujanda 0:52b14877e9eb 27 {
hbujanda 0:52b14877e9eb 28 Addr64 addr64;
hbujanda 0:52b14877e9eb 29 uint16_t addr16;
hbujanda 0:52b14877e9eb 30 remote.get_addr(&addr64);
hbujanda 0:52b14877e9eb 31 remote.get_addr(&addr16);
hbujanda 0:52b14877e9eb 32
hbujanda 0:52b14877e9eb 33 log_serial->printf("Found device '%s' [%08X:%08X][%04X]\r\n", node_id, addr64.get_high32(), addr64.get_low32(), addr16);
hbujanda 0:52b14877e9eb 34 if (send_to.is_valid()) {
hbujanda 0:52b14877e9eb 35 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);
hbujanda 0:52b14877e9eb 36 } else {
hbujanda 0:52b14877e9eb 37 send_to = remote;
hbujanda 0:52b14877e9eb 38 }
hbujanda 0:52b14877e9eb 39 }
hbujanda 0:52b14877e9eb 40
hbujanda 0:52b14877e9eb 41 int main()
hbujanda 0:52b14877e9eb 42 {
hbujanda 0:52b14877e9eb 43 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
hbujanda 0:52b14877e9eb 44 log_serial->baud(9600);
hbujanda 0:52b14877e9eb 45 log_serial->printf("Sample application to demo how to discover nodes in the ZigBee network and send a message to them\r\n\r\n");
hbujanda 0:52b14877e9eb 46 log_serial->printf(XB_LIB_BANNER);
hbujanda 0:52b14877e9eb 47
hbujanda 0:52b14877e9eb 48 #if defined(ENABLE_LOGGING)
hbujanda 0:52b14877e9eb 49 new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
hbujanda 0:52b14877e9eb 50 #endif
hbujanda 0:52b14877e9eb 51
hbujanda 0:52b14877e9eb 52 XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
hbujanda 0:52b14877e9eb 53
hbujanda 0:52b14877e9eb 54 /* Register callbacks */
hbujanda 0:52b14877e9eb 55 xbee.register_node_discovery_cb(&discovery_function);
hbujanda 0:52b14877e9eb 56
hbujanda 0:52b14877e9eb 57 RadioStatus const radioStatus = xbee.init();
hbujanda 0:52b14877e9eb 58 MBED_ASSERT(radioStatus == Success);
hbujanda 0:52b14877e9eb 59
hbujanda 0:52b14877e9eb 60 /* Wait until the device has joined the network */
hbujanda 0:52b14877e9eb 61 log_serial->printf("Waiting for device to join the network: ");
hbujanda 0:52b14877e9eb 62 while (!xbee.is_joined()) {
hbujanda 0:52b14877e9eb 63 wait_ms(1000);
hbujanda 0:52b14877e9eb 64 log_serial->printf(".");
hbujanda 0:52b14877e9eb 65 }
hbujanda 0:52b14877e9eb 66 log_serial->printf("OK\r\n");
hbujanda 0:52b14877e9eb 67
hbujanda 0:52b14877e9eb 68 log_serial->printf("Starting Node Discovery\r\n");
hbujanda 0:52b14877e9eb 69 xbee.start_node_discovery();
hbujanda 0:52b14877e9eb 70
hbujanda 0:52b14877e9eb 71 while (true) {
hbujanda 0:52b14877e9eb 72 xbee.process_rx_frames();
hbujanda 0:52b14877e9eb 73 if (send_to.is_valid()) {
hbujanda 0:52b14877e9eb 74 const char message[] = "Hello neighbor!";
hbujanda 0:52b14877e9eb 75 log_serial->printf("Sending data to remote device\r\n");
hbujanda 0:52b14877e9eb 76 const TxStatus txStatus = xbee.send_data(send_to, (const uint8_t *)message, sizeof message - 1);
hbujanda 0:52b14877e9eb 77 if (txStatus != TxStatusSuccess) {
hbujanda 0:52b14877e9eb 78 log_serial->printf("Found an error while sending data %d\r\n", txStatus);
hbujanda 0:52b14877e9eb 79 }
hbujanda 0:52b14877e9eb 80 send_to = RemoteXBeeZB(); /* Set it to an uninitialized object again */
hbujanda 0:52b14877e9eb 81 }
hbujanda 0:52b14877e9eb 82 wait_ms(10);
hbujanda 0:52b14877e9eb 83 }
hbujanda 0:52b14877e9eb 84
hbujanda 0:52b14877e9eb 85 delete(log_serial);
hbujanda 0:52b14877e9eb 86 }