DigiMesh network configuration example for mbed XBeeLib By Digi
Fork of XBeeZB_module_config by
main.cpp@3:8655a1c17787, 2016-07-28 (annotated)
- Committer:
- hbujanda
- Date:
- Thu Jul 28 10:24:32 2016 +0000
- Revision:
- 3:8655a1c17787
- Parent:
- 0:03d43843c066
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
spastor | 0:03d43843c066 | 1 | /** |
spastor | 0:03d43843c066 | 2 | * Copyright (c) 2015 Digi International Inc., |
spastor | 0:03d43843c066 | 3 | * All rights not expressly granted are reserved. |
spastor | 0:03d43843c066 | 4 | * |
spastor | 0:03d43843c066 | 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
spastor | 0:03d43843c066 | 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
spastor | 0:03d43843c066 | 7 | * You can obtain one at http://mozilla.org/MPL/2.0/. |
spastor | 0:03d43843c066 | 8 | * |
spastor | 0:03d43843c066 | 9 | * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343 |
spastor | 0:03d43843c066 | 10 | * ======================================================================= |
spastor | 0:03d43843c066 | 11 | */ |
spastor | 0:03d43843c066 | 12 | |
spastor | 0:03d43843c066 | 13 | #include "mbed.h" |
spastor | 0:03d43843c066 | 14 | #include "XBeeLib.h" |
spastor | 0:03d43843c066 | 15 | #if defined(ENABLE_LOGGING) |
spastor | 0:03d43843c066 | 16 | #include "DigiLoggerMbedSerial.h" |
spastor | 0:03d43843c066 | 17 | using namespace DigiLog; |
spastor | 0:03d43843c066 | 18 | #endif |
spastor | 0:03d43843c066 | 19 | |
spastor | 0:03d43843c066 | 20 | using namespace XBeeLib; |
spastor | 0:03d43843c066 | 21 | |
spastor | 0:03d43843c066 | 22 | #define NEW_NODE_ID "mbed XBee" |
hbujanda | 3:8655a1c17787 | 23 | #define NEW_NETWORK_ID 0xD161 |
hbujanda | 3:8655a1c17787 | 24 | #define NEW_CHANNEL 0x10 |
spastor | 0:03d43843c066 | 25 | #define NEW_POWER_LEVEL 2 |
spastor | 0:03d43843c066 | 26 | |
spastor | 0:03d43843c066 | 27 | Serial *log_serial; |
spastor | 0:03d43843c066 | 28 | |
spastor | 0:03d43843c066 | 29 | /** Callback function, invoked at packet reception */ |
hbujanda | 3:8655a1c17787 | 30 | static void receive_cb(const RemoteXBeeDM& remote, bool broadcast, const uint8_t *const data, uint16_t len) |
spastor | 0:03d43843c066 | 31 | { |
spastor | 0:03d43843c066 | 32 | log_serial->printf("Data received\r\n"); |
spastor | 0:03d43843c066 | 33 | } |
spastor | 0:03d43843c066 | 34 | |
spastor | 0:03d43843c066 | 35 | int main() |
spastor | 0:03d43843c066 | 36 | { |
spastor | 0:03d43843c066 | 37 | log_serial = new Serial(DEBUG_TX, DEBUG_RX); |
spastor | 0:03d43843c066 | 38 | log_serial->baud(9600); |
hbujanda | 3:8655a1c17787 | 39 | log_serial->printf("Sample application to demo how to configure a XBeeDM module through AT wrappers\r\n\r\n"); |
spastor | 0:03d43843c066 | 40 | log_serial->printf(XB_LIB_BANNER); |
spastor | 0:03d43843c066 | 41 | |
spastor | 0:03d43843c066 | 42 | #if defined(ENABLE_LOGGING) |
spastor | 0:03d43843c066 | 43 | new DigiLoggerMbedSerial(log_serial, LogLevelInfo); |
spastor | 0:03d43843c066 | 44 | #endif |
spastor | 0:03d43843c066 | 45 | |
hbujanda | 3:8655a1c17787 | 46 | XBeeDM xbee = XBeeDM(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600); |
spastor | 0:03d43843c066 | 47 | |
spastor | 0:03d43843c066 | 48 | xbee.register_receive_cb(&receive_cb); |
spastor | 0:03d43843c066 | 49 | RadioStatus radioStatus = xbee.init(); |
spastor | 0:03d43843c066 | 50 | MBED_ASSERT(radioStatus == Success); |
spastor | 0:03d43843c066 | 51 | |
hbujanda | 3:8655a1c17787 | 52 | uint8_t current_channel; |
hbujanda | 3:8655a1c17787 | 53 | radioStatus = xbee.get_channel(¤t_channel); |
hbujanda | 3:8655a1c17787 | 54 | if (radioStatus != Success) { |
hbujanda | 3:8655a1c17787 | 55 | log_serial->printf("Error reading the Channel\r\n"); |
hbujanda | 3:8655a1c17787 | 56 | current_channel = 0; |
hbujanda | 3:8655a1c17787 | 57 | } |
hbujanda | 3:8655a1c17787 | 58 | log_serial->printf("Current channel is '%02X', setting it to '%02X'\r\n", current_channel, NEW_CHANNEL); |
hbujanda | 3:8655a1c17787 | 59 | radioStatus = xbee.set_channel(NEW_CHANNEL); |
spastor | 0:03d43843c066 | 60 | if (radioStatus != Success) { |
hbujanda | 3:8655a1c17787 | 61 | log_serial->printf("Error when setting channel\r\n"); |
hbujanda | 3:8655a1c17787 | 62 | } |
hbujanda | 3:8655a1c17787 | 63 | |
hbujanda | 3:8655a1c17787 | 64 | uint16_t network_id; |
hbujanda | 3:8655a1c17787 | 65 | radioStatus = xbee.get_network_id(&network_id); |
hbujanda | 3:8655a1c17787 | 66 | if (radioStatus != Success) { |
hbujanda | 3:8655a1c17787 | 67 | log_serial->printf("Error reading the Network ID\r\n"); |
hbujanda | 3:8655a1c17787 | 68 | network_id = 0; |
hbujanda | 3:8655a1c17787 | 69 | } |
hbujanda | 3:8655a1c17787 | 70 | log_serial->printf("Current Network ID is '%04X', setting it to '%04X'\r\n", network_id, NEW_NETWORK_ID); |
hbujanda | 3:8655a1c17787 | 71 | radioStatus = xbee.set_network_id(NEW_NETWORK_ID); |
hbujanda | 3:8655a1c17787 | 72 | if (radioStatus != Success) { |
hbujanda | 3:8655a1c17787 | 73 | log_serial->printf("Error when setting Network ID\r\n"); |
spastor | 0:03d43843c066 | 74 | } |
spastor | 0:03d43843c066 | 75 | |
spastor | 0:03d43843c066 | 76 | uint8_t encryption_key[] = {0x12, 0x34, 0x56, 0x78}; |
spastor | 0:03d43843c066 | 77 | log_serial->printf("Configuring Encryption Key\r\n"); |
spastor | 0:03d43843c066 | 78 | radioStatus = xbee.set_network_encryption_key(encryption_key, sizeof encryption_key); |
spastor | 0:03d43843c066 | 79 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 80 | log_serial->printf("Error when setting encryption key\r\n"); |
spastor | 0:03d43843c066 | 81 | } |
spastor | 0:03d43843c066 | 82 | |
spastor | 0:03d43843c066 | 83 | log_serial->printf("Enabling Encryption\r\n"); |
spastor | 0:03d43843c066 | 84 | radioStatus = xbee.enable_network_encryption(true); |
spastor | 0:03d43843c066 | 85 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 86 | log_serial->printf("Error when enabling encryption\r\n"); |
spastor | 0:03d43843c066 | 87 | } |
spastor | 0:03d43843c066 | 88 | |
spastor | 0:03d43843c066 | 89 | char current_node_identifier[21]; |
spastor | 0:03d43843c066 | 90 | radioStatus = xbee.get_node_identifier(current_node_identifier); |
spastor | 0:03d43843c066 | 91 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 92 | log_serial->printf("Error reading the node identifier"); |
spastor | 0:03d43843c066 | 93 | current_node_identifier[0] = '\0'; /* Set to empty string. */ |
spastor | 0:03d43843c066 | 94 | } |
spastor | 0:03d43843c066 | 95 | log_serial->printf("Current Node Identifier '%s', setting it to '%s'\r\n", current_node_identifier, NEW_NODE_ID); |
spastor | 0:03d43843c066 | 96 | radioStatus = xbee.set_node_identifier(NEW_NODE_ID); |
spastor | 0:03d43843c066 | 97 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 98 | log_serial->printf("Error setting the node identifier\r\n"); |
spastor | 0:03d43843c066 | 99 | } |
spastor | 0:03d43843c066 | 100 | |
spastor | 0:03d43843c066 | 101 | uint8_t current_power_level; |
spastor | 0:03d43843c066 | 102 | radioStatus = xbee.get_power_level(¤t_power_level); |
spastor | 0:03d43843c066 | 103 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 104 | log_serial->printf("Error reading the Power Level\r\n"); |
spastor | 0:03d43843c066 | 105 | current_power_level = 0; |
spastor | 0:03d43843c066 | 106 | } |
spastor | 0:03d43843c066 | 107 | log_serial->printf("Current Power level is '%d', setting it to '%d'\r\n", current_power_level, NEW_POWER_LEVEL); |
spastor | 0:03d43843c066 | 108 | radioStatus = xbee.set_power_level(NEW_POWER_LEVEL); |
spastor | 0:03d43843c066 | 109 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 110 | log_serial->printf("Error when setting Power Level\r\n"); |
spastor | 0:03d43843c066 | 111 | } |
spastor | 0:03d43843c066 | 112 | |
spastor | 0:03d43843c066 | 113 | |
spastor | 0:03d43843c066 | 114 | //xbee.write_config(); /* Uncomment this line to save changes into Flash. */ |
spastor | 0:03d43843c066 | 115 | |
spastor | 0:03d43843c066 | 116 | for(;;) { |
spastor | 0:03d43843c066 | 117 | xbee.process_rx_frames(); |
spastor | 0:03d43843c066 | 118 | wait_ms(1000); |
spastor | 0:03d43843c066 | 119 | } |
spastor | 0:03d43843c066 | 120 | delete(log_serial); |
spastor | 0:03d43843c066 | 121 | } |