802.15.4 network configuration example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Committer:
hbujanda
Date:
Thu Mar 31 11:42:40 2016 +0200
Revision:
1:8d969493fc6a
Parent:
0:8013d167495f
Child:
2:d22849a604f3
Automatic upload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
spastor 0:8013d167495f 1 /**
spastor 0:8013d167495f 2 * Copyright (c) 2015 Digi International Inc.,
spastor 0:8013d167495f 3 * All rights not expressly granted are reserved.
spastor 0:8013d167495f 4 *
spastor 0:8013d167495f 5 * This Source Code Form is subject to the terms of the Mozilla Public
spastor 0:8013d167495f 6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
spastor 0:8013d167495f 7 * You can obtain one at http://mozilla.org/MPL/2.0/.
spastor 0:8013d167495f 8 *
spastor 0:8013d167495f 9 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
spastor 0:8013d167495f 10 * =======================================================================
spastor 0:8013d167495f 11 */
spastor 0:8013d167495f 12
spastor 0:8013d167495f 13 #include "mbed.h"
spastor 0:8013d167495f 14 #include "XBeeLib.h"
spastor 0:8013d167495f 15 #if defined(ENABLE_LOGGING)
spastor 0:8013d167495f 16 #include "DigiLoggerMbedSerial.h"
spastor 0:8013d167495f 17 using namespace DigiLog;
spastor 0:8013d167495f 18 #endif
spastor 0:8013d167495f 19
spastor 0:8013d167495f 20 using namespace XBeeLib;
spastor 0:8013d167495f 21
spastor 0:8013d167495f 22 #define NEW_NODE_ID "mbed XBee"
spastor 0:8013d167495f 23 #define NEW_PANID 0xD161
hbujanda 1:8d969493fc6a 24 #define NEW_CHANNEL 0x17
spastor 0:8013d167495f 25 #define NEW_NETWORK_ADDR 0x2015
spastor 0:8013d167495f 26 #define NEW_POWER_LEVEL 2
spastor 0:8013d167495f 27
spastor 0:8013d167495f 28 Serial *log_serial;
spastor 0:8013d167495f 29
spastor 0:8013d167495f 30 /** Callback function, invoked at packet reception */
spastor 0:8013d167495f 31 static void receive_cb(const RemoteXBee802& remote, bool broadcast, const uint8_t *const data, uint16_t len)
spastor 0:8013d167495f 32 {
hbujanda 1:8d969493fc6a 33 if (remote.is_valid_addr16b()) {
hbujanda 1:8d969493fc6a 34 log_serial->printf("\r\nGot a %s 16-bit RX packet [%04x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST", remote.get_addr16(), len);
hbujanda 1:8d969493fc6a 35 } else {
hbujanda 1:8d969493fc6a 36 log_serial->printf("\r\nGot a %s 64-bit RX packet [%08x:%08x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST", remote.get_addr64(), len);
hbujanda 1:8d969493fc6a 37 }
hbujanda 1:8d969493fc6a 38
hbujanda 1:8d969493fc6a 39 for (int i = 0; i < len; i++)
hbujanda 1:8d969493fc6a 40 log_serial->printf("%02x ", data[i]);
hbujanda 1:8d969493fc6a 41
hbujanda 1:8d969493fc6a 42 log_serial->printf("\r\n");
spastor 0:8013d167495f 43 }
spastor 0:8013d167495f 44
spastor 0:8013d167495f 45 int main()
spastor 0:8013d167495f 46 {
spastor 0:8013d167495f 47 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
spastor 0:8013d167495f 48 log_serial->baud(9600);
spastor 0:8013d167495f 49 log_serial->printf("Sample application to demo how to configure a XBee802 module through AT wrappers\r\n\r\n");
spastor 0:8013d167495f 50 log_serial->printf(XB_LIB_BANNER);
spastor 0:8013d167495f 51
spastor 0:8013d167495f 52 #if defined(ENABLE_LOGGING)
spastor 0:8013d167495f 53 new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
spastor 0:8013d167495f 54 #endif
spastor 0:8013d167495f 55
spastor 0:8013d167495f 56 XBee802 xbee = XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
spastor 0:8013d167495f 57
spastor 0:8013d167495f 58 xbee.register_receive_cb(&receive_cb);
spastor 0:8013d167495f 59 RadioStatus radioStatus = xbee.init();
spastor 0:8013d167495f 60 MBED_ASSERT(radioStatus == Success);
spastor 0:8013d167495f 61
spastor 0:8013d167495f 62 uint16_t current_network_addr;
spastor 0:8013d167495f 63 radioStatus = xbee.get_network_address(&current_network_addr);
spastor 0:8013d167495f 64 if (radioStatus != Success) {
spastor 0:8013d167495f 65 log_serial->printf("Error reading the Network Address\r\n");
spastor 0:8013d167495f 66 current_network_addr = 0;
spastor 0:8013d167495f 67 }
spastor 0:8013d167495f 68 log_serial->printf("Current Network Address is '%04X', setting it to '%04X'\r\n", current_network_addr, NEW_NETWORK_ADDR);
spastor 0:8013d167495f 69 radioStatus = xbee.set_network_address(NEW_NETWORK_ADDR);
spastor 0:8013d167495f 70 if (radioStatus != Success) {
spastor 0:8013d167495f 71 log_serial->printf("Error when setting Network Address\r\n");
spastor 0:8013d167495f 72 }
spastor 0:8013d167495f 73
spastor 0:8013d167495f 74 char current_node_identifier[21];
spastor 0:8013d167495f 75 radioStatus = xbee.get_node_identifier(current_node_identifier);
spastor 0:8013d167495f 76 if (radioStatus != Success) {
spastor 0:8013d167495f 77 log_serial->printf("Error reading the node identifier");
spastor 0:8013d167495f 78 current_node_identifier[0] = '\0'; /* Set to empty string. */
spastor 0:8013d167495f 79 }
spastor 0:8013d167495f 80 log_serial->printf("Current Node Identifier '%s', setting it to '%s'\r\n", current_node_identifier, NEW_NODE_ID);
spastor 0:8013d167495f 81 radioStatus = xbee.set_node_identifier(NEW_NODE_ID);
spastor 0:8013d167495f 82 if (radioStatus != Success) {
spastor 0:8013d167495f 83 log_serial->printf("Error setting the node identifier\r\n");
spastor 0:8013d167495f 84 }
spastor 0:8013d167495f 85
spastor 0:8013d167495f 86 uint8_t current_power_level;
spastor 0:8013d167495f 87 radioStatus = xbee.get_power_level(&current_power_level);
spastor 0:8013d167495f 88 if (radioStatus != Success) {
spastor 0:8013d167495f 89 log_serial->printf("Error reading the Power Level\r\n");
spastor 0:8013d167495f 90 current_power_level = 0;
spastor 0:8013d167495f 91 }
spastor 0:8013d167495f 92 log_serial->printf("Current Power level is '%d', setting it to '%d'\r\n", current_power_level, NEW_POWER_LEVEL);
spastor 0:8013d167495f 93 radioStatus = xbee.set_power_level(NEW_POWER_LEVEL);
spastor 0:8013d167495f 94 if (radioStatus != Success) {
spastor 0:8013d167495f 95 log_serial->printf("Error when setting Power Level\r\n");
spastor 0:8013d167495f 96 }
spastor 0:8013d167495f 97
spastor 0:8013d167495f 98 uint8_t current_channel;
spastor 0:8013d167495f 99 radioStatus = xbee.get_channel(&current_channel);
spastor 0:8013d167495f 100 if (radioStatus != Success) {
spastor 0:8013d167495f 101 log_serial->printf("Error reading the Channel\r\n");
spastor 0:8013d167495f 102 current_channel = 0;
spastor 0:8013d167495f 103 }
spastor 0:8013d167495f 104 log_serial->printf("Current channel is '%02X', setting it to '%02X'\r\n", current_channel, NEW_CHANNEL);
spastor 0:8013d167495f 105 radioStatus = xbee.set_channel(NEW_CHANNEL);
spastor 0:8013d167495f 106 if (radioStatus != Success) {
spastor 0:8013d167495f 107 log_serial->printf("Error when setting channel\r\n");
spastor 0:8013d167495f 108 }
spastor 0:8013d167495f 109
spastor 0:8013d167495f 110 wait_ms(100); /* Chaning the channel or PAN ID leaves the radio irresponsive for a little while */
spastor 0:8013d167495f 111
spastor 0:8013d167495f 112 uint16_t current_panid;
spastor 0:8013d167495f 113 radioStatus = xbee.get_panid(&current_panid);
spastor 0:8013d167495f 114 if (radioStatus != Success) {
spastor 0:8013d167495f 115 log_serial->printf("Error reading the PAN ID\r\n");
spastor 0:8013d167495f 116 current_panid = 0;
spastor 0:8013d167495f 117 }
spastor 0:8013d167495f 118 log_serial->printf("Current PAN ID is '%04X', setting it to '%04X'\r\n", current_panid, NEW_PANID);
spastor 0:8013d167495f 119 radioStatus = xbee.set_panid(NEW_PANID);
spastor 0:8013d167495f 120 if (radioStatus != Success) {
spastor 0:8013d167495f 121 log_serial->printf("Error when setting PAN ID\r\n");
spastor 0:8013d167495f 122 }
spastor 0:8013d167495f 123
spastor 0:8013d167495f 124 uint8_t encryption_key[] = {0x12, 0x34, 0x56, 0x78};
spastor 0:8013d167495f 125 log_serial->printf("Configuring Encryption Key\r\n");
spastor 0:8013d167495f 126 radioStatus = xbee.set_network_encryption_key(encryption_key, sizeof encryption_key);
spastor 0:8013d167495f 127 if (radioStatus != Success) {
spastor 0:8013d167495f 128 log_serial->printf("Error when setting encryption key\r\n");
spastor 0:8013d167495f 129 }
spastor 0:8013d167495f 130
spastor 0:8013d167495f 131 log_serial->printf("Enabling Encryption\r\n");
spastor 0:8013d167495f 132 radioStatus = xbee.enable_network_encryption(true);
spastor 0:8013d167495f 133 if (radioStatus != Success) {
spastor 0:8013d167495f 134 log_serial->printf("Error when enabling encryption\r\n");
spastor 0:8013d167495f 135 }
spastor 0:8013d167495f 136
spastor 0:8013d167495f 137 //xbee.write_config(); /* Uncomment this line to save changes into Flash. */
spastor 0:8013d167495f 138
spastor 0:8013d167495f 139 for(;;) {
spastor 0:8013d167495f 140 xbee.process_rx_frames();
spastor 0:8013d167495f 141 wait_ms(1000);
spastor 0:8013d167495f 142 }
spastor 0:8013d167495f 143 delete(log_serial);
spastor 0:8013d167495f 144 }