ZigBee network configuration example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 using namespace XBeeLib;
00021 
00022 #define NEW_NODE_ID         "mbed XBee"
00023 #define NEW_PANID           0xD161
00024 #define NEW_CHANNEL_MASK    0x3FFF
00025 #define NEW_POWER_LEVEL     2
00026 
00027 Serial *log_serial;
00028 
00029 /** Callback function, invoked at packet reception */
00030 static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len)
00031 {
00032     log_serial->printf("Data received\r\n");
00033 }
00034 
00035 int main()
00036 {
00037     log_serial = new Serial(DEBUG_TX, DEBUG_RX);
00038     log_serial->baud(9600);
00039     log_serial->printf("Sample application to demo how to configure a XBeeZB module through AT wrappers\r\n\r\n");
00040     log_serial->printf(XB_LIB_BANNER);
00041 
00042 #if defined(ENABLE_LOGGING)
00043     new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
00044 #endif
00045 
00046     XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
00047 
00048     xbee.register_receive_cb(&receive_cb);
00049     RadioStatus radioStatus = xbee.init();
00050     MBED_ASSERT(radioStatus == Success);
00051 
00052     log_serial->printf("Always check for coordinator before connecting\r\n");
00053     xbee.check_for_coordinator_at_start(true);
00054     if (radioStatus != Success) {
00055         log_serial->printf("Failed to set check_for_coordinator_at_start()\r\n");
00056     }
00057 
00058     uint8_t encryption_key[] = {0x12, 0x34, 0x56, 0x78};
00059     log_serial->printf("Configuring Encryption Key\r\n");
00060     radioStatus = xbee.set_network_encryption_key(encryption_key, sizeof encryption_key);
00061     if (radioStatus != Success) {
00062         log_serial->printf("Error when setting encryption key\r\n");
00063     }
00064 
00065     log_serial->printf("Enabling Encryption\r\n");
00066     radioStatus = xbee.enable_network_encryption(true);
00067     if (radioStatus != Success) {
00068         log_serial->printf("Error when enabling encryption\r\n");
00069     }
00070 
00071     uint64_t current_panid;
00072     radioStatus = xbee.get_operating_panid(&current_panid);
00073     if (radioStatus != Success) {
00074         log_serial->printf("Error reading the PAN ID\r\n");
00075         current_panid = 0;
00076     }
00077     log_serial->printf("Current PAN ID is '%X%X', setting it to '%04X%04X'\r\n", UINT64_HI32(current_panid), UINT64_LO32(current_panid), UINT64_HI32(NEW_PANID), UINT64_LO32(NEW_PANID));
00078     radioStatus = xbee.set_panid(NEW_PANID);
00079     if (radioStatus != Success) {
00080         log_serial->printf("Error when setting PAN ID\r\n");
00081     }
00082 
00083     char current_node_identifier[21];
00084     radioStatus = xbee.get_node_identifier(current_node_identifier);
00085     if (radioStatus != Success) {
00086         log_serial->printf("Error reading the node identifier");
00087         current_node_identifier[0] = '\0'; /* Set to empty string. */
00088     }
00089     log_serial->printf("Current Node Identifier '%s', setting it to '%s'\r\n", current_node_identifier, NEW_NODE_ID);
00090     radioStatus = xbee.set_node_identifier(NEW_NODE_ID);
00091     if (radioStatus != Success) {
00092         log_serial->printf("Error setting the node identifier\r\n");
00093     }
00094 
00095     uint8_t current_power_level;
00096     radioStatus = xbee.get_power_level(&current_power_level);
00097     if (radioStatus != Success) {
00098         log_serial->printf("Error reading the Power Level\r\n");
00099         current_power_level = 0;
00100     }
00101     log_serial->printf("Current Power level is '%d', setting it to '%d'\r\n", current_power_level, NEW_POWER_LEVEL);
00102     radioStatus = xbee.set_power_level(NEW_POWER_LEVEL);
00103     if (radioStatus != Success) {
00104         log_serial->printf("Error when setting Power Level\r\n");
00105     }
00106 
00107     uint16_t current_channel_mask;
00108     radioStatus = xbee.get_channel_mask(&current_channel_mask);
00109     if (radioStatus != Success) {
00110         log_serial->printf("Error reading the Channel Mask\r\n");
00111         current_channel_mask = 0;
00112     }
00113     log_serial->printf("Current channel mask is '%04X', setting it to '%04X'\r\n", current_channel_mask, NEW_CHANNEL_MASK);
00114     radioStatus = xbee.set_channel_mask(NEW_CHANNEL_MASK);
00115     if (radioStatus != Success) {
00116         log_serial->printf("Error when setting channel\r\n");
00117     }
00118 
00119     do {
00120         wait_ms(100);
00121     } while (!xbee.is_joined());
00122 
00123     uint16_t network_addr;
00124     radioStatus = xbee.get_network_address(&network_addr);
00125     if (radioStatus != Success) {
00126         log_serial->printf("Error when reading network address\r\n");
00127         network_addr = 0xFFFF;
00128     }
00129     log_serial->printf("Network address is '%04x'\r\n", network_addr);
00130 
00131 
00132     //xbee.write_config(); /* Uncomment this line to save changes into Flash. */
00133 
00134     for(;;) {
00135         xbee.process_rx_frames();
00136         wait_ms(1000);
00137     }
00138     delete(log_serial);
00139 }