802.15.4 network configuration example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Committer:
hbujanda
Date:
Fri Jul 29 12:12:17 2016 +0200
Revision:
3:3fecffaf52c8
Parent:
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 2:d22849a604f3 24 #define NEW_CHANNEL 0x1A
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 2:d22849a604f3 33 log_serial->printf("Data received\r\n");
spastor 0:8013d167495f 34 }
spastor 0:8013d167495f 35
spastor 0:8013d167495f 36 int main()
spastor 0:8013d167495f 37 {
spastor 0:8013d167495f 38 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
spastor 0:8013d167495f 39 log_serial->baud(9600);
spastor 0:8013d167495f 40 log_serial->printf("Sample application to demo how to configure a XBee802 module through AT wrappers\r\n\r\n");
spastor 0:8013d167495f 41 log_serial->printf(XB_LIB_BANNER);
spastor 0:8013d167495f 42
spastor 0:8013d167495f 43 #if defined(ENABLE_LOGGING)
spastor 0:8013d167495f 44 new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
spastor 0:8013d167495f 45 #endif
spastor 0:8013d167495f 46
spastor 0:8013d167495f 47 XBee802 xbee = XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
spastor 0:8013d167495f 48
spastor 0:8013d167495f 49 xbee.register_receive_cb(&receive_cb);
spastor 0:8013d167495f 50 RadioStatus radioStatus = xbee.init();
spastor 0:8013d167495f 51 MBED_ASSERT(radioStatus == Success);
spastor 0:8013d167495f 52
spastor 0:8013d167495f 53 uint16_t current_network_addr;
spastor 0:8013d167495f 54 radioStatus = xbee.get_network_address(&current_network_addr);
spastor 0:8013d167495f 55 if (radioStatus != Success) {
spastor 0:8013d167495f 56 log_serial->printf("Error reading the Network Address\r\n");
spastor 0:8013d167495f 57 current_network_addr = 0;
spastor 0:8013d167495f 58 }
spastor 0:8013d167495f 59 log_serial->printf("Current Network Address is '%04X', setting it to '%04X'\r\n", current_network_addr, NEW_NETWORK_ADDR);
spastor 0:8013d167495f 60 radioStatus = xbee.set_network_address(NEW_NETWORK_ADDR);
spastor 0:8013d167495f 61 if (radioStatus != Success) {
spastor 0:8013d167495f 62 log_serial->printf("Error when setting Network Address\r\n");
spastor 0:8013d167495f 63 }
spastor 0:8013d167495f 64
spastor 0:8013d167495f 65 char current_node_identifier[21];
spastor 0:8013d167495f 66 radioStatus = xbee.get_node_identifier(current_node_identifier);
spastor 0:8013d167495f 67 if (radioStatus != Success) {
spastor 0:8013d167495f 68 log_serial->printf("Error reading the node identifier");
spastor 0:8013d167495f 69 current_node_identifier[0] = '\0'; /* Set to empty string. */
spastor 0:8013d167495f 70 }
spastor 0:8013d167495f 71 log_serial->printf("Current Node Identifier '%s', setting it to '%s'\r\n", current_node_identifier, NEW_NODE_ID);
spastor 0:8013d167495f 72 radioStatus = xbee.set_node_identifier(NEW_NODE_ID);
spastor 0:8013d167495f 73 if (radioStatus != Success) {
spastor 0:8013d167495f 74 log_serial->printf("Error setting the node identifier\r\n");
spastor 0:8013d167495f 75 }
spastor 0:8013d167495f 76
spastor 0:8013d167495f 77 uint8_t current_power_level;
spastor 0:8013d167495f 78 radioStatus = xbee.get_power_level(&current_power_level);
spastor 0:8013d167495f 79 if (radioStatus != Success) {
spastor 0:8013d167495f 80 log_serial->printf("Error reading the Power Level\r\n");
spastor 0:8013d167495f 81 current_power_level = 0;
spastor 0:8013d167495f 82 }
spastor 0:8013d167495f 83 log_serial->printf("Current Power level is '%d', setting it to '%d'\r\n", current_power_level, NEW_POWER_LEVEL);
spastor 0:8013d167495f 84 radioStatus = xbee.set_power_level(NEW_POWER_LEVEL);
spastor 0:8013d167495f 85 if (radioStatus != Success) {
spastor 0:8013d167495f 86 log_serial->printf("Error when setting Power Level\r\n");
spastor 0:8013d167495f 87 }
spastor 0:8013d167495f 88
spastor 0:8013d167495f 89 uint8_t current_channel;
spastor 0:8013d167495f 90 radioStatus = xbee.get_channel(&current_channel);
spastor 0:8013d167495f 91 if (radioStatus != Success) {
spastor 0:8013d167495f 92 log_serial->printf("Error reading the Channel\r\n");
spastor 0:8013d167495f 93 current_channel = 0;
spastor 0:8013d167495f 94 }
spastor 0:8013d167495f 95 log_serial->printf("Current channel is '%02X', setting it to '%02X'\r\n", current_channel, NEW_CHANNEL);
spastor 0:8013d167495f 96 radioStatus = xbee.set_channel(NEW_CHANNEL);
spastor 0:8013d167495f 97 if (radioStatus != Success) {
spastor 0:8013d167495f 98 log_serial->printf("Error when setting channel\r\n");
spastor 0:8013d167495f 99 }
spastor 0:8013d167495f 100
spastor 0:8013d167495f 101 wait_ms(100); /* Chaning the channel or PAN ID leaves the radio irresponsive for a little while */
spastor 0:8013d167495f 102
spastor 0:8013d167495f 103 uint16_t current_panid;
spastor 0:8013d167495f 104 radioStatus = xbee.get_panid(&current_panid);
spastor 0:8013d167495f 105 if (radioStatus != Success) {
spastor 0:8013d167495f 106 log_serial->printf("Error reading the PAN ID\r\n");
spastor 0:8013d167495f 107 current_panid = 0;
spastor 0:8013d167495f 108 }
spastor 0:8013d167495f 109 log_serial->printf("Current PAN ID is '%04X', setting it to '%04X'\r\n", current_panid, NEW_PANID);
spastor 0:8013d167495f 110 radioStatus = xbee.set_panid(NEW_PANID);
spastor 0:8013d167495f 111 if (radioStatus != Success) {
spastor 0:8013d167495f 112 log_serial->printf("Error when setting PAN ID\r\n");
spastor 0:8013d167495f 113 }
spastor 0:8013d167495f 114
spastor 0:8013d167495f 115 uint8_t encryption_key[] = {0x12, 0x34, 0x56, 0x78};
spastor 0:8013d167495f 116 log_serial->printf("Configuring Encryption Key\r\n");
spastor 0:8013d167495f 117 radioStatus = xbee.set_network_encryption_key(encryption_key, sizeof encryption_key);
spastor 0:8013d167495f 118 if (radioStatus != Success) {
spastor 0:8013d167495f 119 log_serial->printf("Error when setting encryption key\r\n");
spastor 0:8013d167495f 120 }
spastor 0:8013d167495f 121
spastor 0:8013d167495f 122 log_serial->printf("Enabling Encryption\r\n");
spastor 0:8013d167495f 123 radioStatus = xbee.enable_network_encryption(true);
spastor 0:8013d167495f 124 if (radioStatus != Success) {
spastor 0:8013d167495f 125 log_serial->printf("Error when enabling encryption\r\n");
spastor 0:8013d167495f 126 }
spastor 0:8013d167495f 127
spastor 0:8013d167495f 128 //xbee.write_config(); /* Uncomment this line to save changes into Flash. */
spastor 0:8013d167495f 129
spastor 0:8013d167495f 130 for(;;) {
spastor 0:8013d167495f 131 xbee.process_rx_frames();
spastor 0:8013d167495f 132 wait_ms(1000);
spastor 0:8013d167495f 133 }
spastor 0:8013d167495f 134 delete(log_serial);
spastor 0:8013d167495f 135 }