Digi International Inc.
/
XBee802_node_discovery
802.15.4 Node Discovery example for mbed XBeeLib By Digi
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /** 00002 * Copyright (c) 2015 Digi International Inc., 00003 * All rights not expressly granted are reserved. 00004 * 00005 * This Source Code Form is subject to the terms of the Mozilla Public 00006 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 00007 * You can obtain one at http://mozilla.org/MPL/2.0/. 00008 * 00009 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343 00010 * ======================================================================= 00011 */ 00012 00013 #include "mbed.h" 00014 #include "XBeeLib.h" 00015 #if defined(ENABLE_LOGGING) 00016 #include "DigiLoggerMbedSerial.h" 00017 using namespace DigiLog; 00018 #endif 00019 00020 #error "Replace next define with the remote module's Node Identifier (NI parameter)" 00021 #define REMOTE_NODE_ID "MyNodeID" 00022 00023 #define MAX_NODES 5 00024 #define NODE_DISCOVERY_TIMEOUT_MS 5000 00025 00026 using namespace XBeeLib; 00027 00028 Serial *log_serial; 00029 XBee802 * xbee = NULL; 00030 00031 RemoteXBee802 remote_nodes_in_network[MAX_NODES]; 00032 unsigned int remote_nodes_count = 0; 00033 00034 void discovery_function(const RemoteXBee802& remote, char const * const node_id) 00035 { 00036 MBED_ASSERT(xbee != NULL); 00037 const uint64_t remote_addr64 = remote.get_addr64(); 00038 00039 log_serial->printf("Found device '%s' [%08x:%08x][%04X]\r\n", node_id, UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16()); 00040 00041 if (remote_nodes_count < MAX_NODES) { 00042 remote_nodes_in_network[remote_nodes_count] = remote; 00043 remote_nodes_count++; 00044 } else { 00045 log_serial->printf("Found more nodes than maximum configured for example (%d)\r\n", MAX_NODES); 00046 } 00047 } 00048 00049 int main() 00050 { 00051 log_serial = new Serial(DEBUG_TX, DEBUG_RX); 00052 log_serial->baud(9600); 00053 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"); 00054 log_serial->printf(XB_LIB_BANNER); 00055 00056 #if defined(ENABLE_LOGGING) 00057 new DigiLoggerMbedSerial(log_serial, LogLevelInfo); 00058 #endif 00059 00060 xbee = new XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600); 00061 00062 RadioStatus radioStatus = xbee->init(); 00063 MBED_ASSERT(radioStatus == Success); 00064 00065 log_serial->printf("Configuring Node Discovery timeout to %d ms\r\n", NODE_DISCOVERY_TIMEOUT_MS); 00066 radioStatus = xbee->config_node_discovery(NODE_DISCOVERY_TIMEOUT_MS); 00067 if (radioStatus != Success) { 00068 log_serial->printf("Error on config_node_discovery()\r\n"); 00069 } 00070 00071 log_serial->printf("Trying to discover '%s' by its NI\r\n", REMOTE_NODE_ID); 00072 RemoteXBee802 remote_node = xbee->get_remote_node_by_id(REMOTE_NODE_ID); 00073 00074 if (remote_node.is_valid()) { 00075 const uint64_t addr64 = remote_node.get_addr64(); 00076 log_serial->printf("Found '%s'! [%08x:%08x|%04x]\r\n", REMOTE_NODE_ID, UINT64_HI32(addr64), UINT64_LO32(addr64), remote_node.get_addr16()); 00077 00078 const char message[] = "Hello " REMOTE_NODE_ID "!"; 00079 log_serial->printf("Sending data to remote device\r\n"); 00080 const TxStatus txStatus = xbee->send_data(remote_node, (const uint8_t *)message, sizeof message - 1); 00081 if (txStatus != TxStatusSuccess) { 00082 log_serial->printf("Found an error while sending data %d\r\n", txStatus); 00083 } 00084 } else { 00085 log_serial->printf("Couldn't find '%s' node\r\n", REMOTE_NODE_ID); 00086 } 00087 00088 /* Register callbacks */ 00089 xbee->register_node_discovery_cb(&discovery_function); 00090 00091 log_serial->printf("\r\nStarting Node Discovery\r\n\r\n"); 00092 xbee->start_node_discovery(); 00093 00094 do { 00095 xbee->process_rx_frames(); 00096 wait_ms(10); 00097 } while(xbee->is_node_discovery_in_progress()); 00098 log_serial->printf("\r\nNode Discovery Finished\r\n\r\n"); 00099 00100 for (unsigned int i = 0; i < remote_nodes_count; i++) { 00101 00102 RemoteXBee802 remote = remote_nodes_in_network[i]; 00103 const uint64_t remote_addr64 = remote.get_addr64(); 00104 00105 const char message[] = "Hello neighbor!"; 00106 log_serial->printf("Sending data to remote device [%08x:%08x][%04X]\r\n", UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16()); 00107 const TxStatus txStatus = xbee->send_data(remote, (const uint8_t *)message, sizeof message - 1); 00108 if (txStatus != TxStatusSuccess) { 00109 log_serial->printf("Found an error while sending data %d\r\n", txStatus); 00110 } 00111 } 00112 00113 log_serial->printf("Example finished\r\n"); 00114 00115 delete(log_serial); 00116 delete(xbee); 00117 while (true) { 00118 wait_ms(10000); 00119 } 00120 }
Generated on Fri Jul 22 2022 11:23:37 by 1.7.2