DigiMesh network configuration example for mbed XBeeLib By Digi
Fork of XBeeZB_module_config by
main.cpp@0:03d43843c066, 2015-06-01 (annotated)
- Committer:
- spastor
- Date:
- Mon Jun 01 19:02:39 2015 +0200
- Revision:
- 0:03d43843c066
- Child:
- 3:8655a1c17787
Automatic upload
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" |
spastor | 0:03d43843c066 | 23 | #define NEW_PANID 0xD161 |
spastor | 0:03d43843c066 | 24 | #define NEW_CHANNEL_MASK 0x3FFF |
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 */ |
spastor | 0:03d43843c066 | 30 | static void receive_cb(const RemoteXBeeZB& 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); |
spastor | 0:03d43843c066 | 39 | log_serial->printf("Sample application to demo how to configure a XBeeZB 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 | |
spastor | 0:03d43843c066 | 46 | XBeeZB xbee = XBeeZB(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 | |
spastor | 0:03d43843c066 | 52 | log_serial->printf("Always check for coordinator before connecting\r\n"); |
spastor | 0:03d43843c066 | 53 | xbee.check_for_coordinator_at_start(true); |
spastor | 0:03d43843c066 | 54 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 55 | log_serial->printf("Failed to set check_for_coordinator_at_start()\r\n"); |
spastor | 0:03d43843c066 | 56 | } |
spastor | 0:03d43843c066 | 57 | |
spastor | 0:03d43843c066 | 58 | uint8_t encryption_key[] = {0x12, 0x34, 0x56, 0x78}; |
spastor | 0:03d43843c066 | 59 | log_serial->printf("Configuring Encryption Key\r\n"); |
spastor | 0:03d43843c066 | 60 | radioStatus = xbee.set_network_encryption_key(encryption_key, sizeof encryption_key); |
spastor | 0:03d43843c066 | 61 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 62 | log_serial->printf("Error when setting encryption key\r\n"); |
spastor | 0:03d43843c066 | 63 | } |
spastor | 0:03d43843c066 | 64 | |
spastor | 0:03d43843c066 | 65 | log_serial->printf("Enabling Encryption\r\n"); |
spastor | 0:03d43843c066 | 66 | radioStatus = xbee.enable_network_encryption(true); |
spastor | 0:03d43843c066 | 67 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 68 | log_serial->printf("Error when enabling encryption\r\n"); |
spastor | 0:03d43843c066 | 69 | } |
spastor | 0:03d43843c066 | 70 | |
spastor | 0:03d43843c066 | 71 | uint64_t current_panid; |
spastor | 0:03d43843c066 | 72 | radioStatus = xbee.get_operating_panid(¤t_panid); |
spastor | 0:03d43843c066 | 73 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 74 | log_serial->printf("Error reading the PAN ID\r\n"); |
spastor | 0:03d43843c066 | 75 | current_panid = 0; |
spastor | 0:03d43843c066 | 76 | } |
spastor | 0:03d43843c066 | 77 | 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)); |
spastor | 0:03d43843c066 | 78 | radioStatus = xbee.set_panid(NEW_PANID); |
spastor | 0:03d43843c066 | 79 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 80 | log_serial->printf("Error when setting PAN ID\r\n"); |
spastor | 0:03d43843c066 | 81 | } |
spastor | 0:03d43843c066 | 82 | |
spastor | 0:03d43843c066 | 83 | char current_node_identifier[21]; |
spastor | 0:03d43843c066 | 84 | radioStatus = xbee.get_node_identifier(current_node_identifier); |
spastor | 0:03d43843c066 | 85 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 86 | log_serial->printf("Error reading the node identifier"); |
spastor | 0:03d43843c066 | 87 | current_node_identifier[0] = '\0'; /* Set to empty string. */ |
spastor | 0:03d43843c066 | 88 | } |
spastor | 0:03d43843c066 | 89 | log_serial->printf("Current Node Identifier '%s', setting it to '%s'\r\n", current_node_identifier, NEW_NODE_ID); |
spastor | 0:03d43843c066 | 90 | radioStatus = xbee.set_node_identifier(NEW_NODE_ID); |
spastor | 0:03d43843c066 | 91 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 92 | log_serial->printf("Error setting the node identifier\r\n"); |
spastor | 0:03d43843c066 | 93 | } |
spastor | 0:03d43843c066 | 94 | |
spastor | 0:03d43843c066 | 95 | uint8_t current_power_level; |
spastor | 0:03d43843c066 | 96 | radioStatus = xbee.get_power_level(¤t_power_level); |
spastor | 0:03d43843c066 | 97 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 98 | log_serial->printf("Error reading the Power Level\r\n"); |
spastor | 0:03d43843c066 | 99 | current_power_level = 0; |
spastor | 0:03d43843c066 | 100 | } |
spastor | 0:03d43843c066 | 101 | log_serial->printf("Current Power level is '%d', setting it to '%d'\r\n", current_power_level, NEW_POWER_LEVEL); |
spastor | 0:03d43843c066 | 102 | radioStatus = xbee.set_power_level(NEW_POWER_LEVEL); |
spastor | 0:03d43843c066 | 103 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 104 | log_serial->printf("Error when setting Power Level\r\n"); |
spastor | 0:03d43843c066 | 105 | } |
spastor | 0:03d43843c066 | 106 | |
spastor | 0:03d43843c066 | 107 | uint16_t current_channel_mask; |
spastor | 0:03d43843c066 | 108 | radioStatus = xbee.get_channel_mask(¤t_channel_mask); |
spastor | 0:03d43843c066 | 109 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 110 | log_serial->printf("Error reading the Channel Mask\r\n"); |
spastor | 0:03d43843c066 | 111 | current_channel_mask = 0; |
spastor | 0:03d43843c066 | 112 | } |
spastor | 0:03d43843c066 | 113 | log_serial->printf("Current channel mask is '%04X', setting it to '%04X'\r\n", current_channel_mask, NEW_CHANNEL_MASK); |
spastor | 0:03d43843c066 | 114 | radioStatus = xbee.set_channel_mask(NEW_CHANNEL_MASK); |
spastor | 0:03d43843c066 | 115 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 116 | log_serial->printf("Error when setting channel\r\n"); |
spastor | 0:03d43843c066 | 117 | } |
spastor | 0:03d43843c066 | 118 | |
spastor | 0:03d43843c066 | 119 | do { |
spastor | 0:03d43843c066 | 120 | wait_ms(100); |
spastor | 0:03d43843c066 | 121 | } while (!xbee.is_joined()); |
spastor | 0:03d43843c066 | 122 | |
spastor | 0:03d43843c066 | 123 | uint16_t network_addr; |
spastor | 0:03d43843c066 | 124 | radioStatus = xbee.get_network_address(&network_addr); |
spastor | 0:03d43843c066 | 125 | if (radioStatus != Success) { |
spastor | 0:03d43843c066 | 126 | log_serial->printf("Error when reading network address\r\n"); |
spastor | 0:03d43843c066 | 127 | network_addr = 0xFFFF; |
spastor | 0:03d43843c066 | 128 | } |
spastor | 0:03d43843c066 | 129 | log_serial->printf("Network address is '%04x'\r\n", network_addr); |
spastor | 0:03d43843c066 | 130 | |
spastor | 0:03d43843c066 | 131 | |
spastor | 0:03d43843c066 | 132 | //xbee.write_config(); /* Uncomment this line to save changes into Flash. */ |
spastor | 0:03d43843c066 | 133 | |
spastor | 0:03d43843c066 | 134 | for(;;) { |
spastor | 0:03d43843c066 | 135 | xbee.process_rx_frames(); |
spastor | 0:03d43843c066 | 136 | wait_ms(1000); |
spastor | 0:03d43843c066 | 137 | } |
spastor | 0:03d43843c066 | 138 | delete(log_serial); |
spastor | 0:03d43843c066 | 139 | } |