DigiMesh network configuration example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Fork of XBeeZB_module_config by Digi International Inc.

main.cpp

Committer:
hbujanda
Date:
2016-07-29
Revision:
4:55bbe30579da
Parent:
3:8655a1c17787

File content as of revision 4:55bbe30579da:

/**
 * Copyright (c) 2015 Digi International Inc.,
 * All rights not expressly granted are reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
 * =======================================================================
 */

#include "mbed.h"
#include "XBeeLib.h"
#if defined(ENABLE_LOGGING)
#include "DigiLoggerMbedSerial.h"
using namespace DigiLog;
#endif

using namespace XBeeLib;

#define NEW_NODE_ID         "mbed XBee"
#define NEW_NETWORK_ID      0xD161
#define NEW_CHANNEL         0x10
#define NEW_POWER_LEVEL     2

Serial *log_serial;

/** Callback function, invoked at packet reception */
static void receive_cb(const RemoteXBeeDM& remote, bool broadcast, const uint8_t *const data, uint16_t len)
{
    log_serial->printf("Data received\r\n");
}

int main()
{
    log_serial = new Serial(DEBUG_TX, DEBUG_RX);
    log_serial->baud(9600);
    log_serial->printf("Sample application to demo how to configure a XBeeDM module through AT wrappers\r\n\r\n");
    log_serial->printf(XB_LIB_BANNER);

#if defined(ENABLE_LOGGING)
    new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
#endif

    XBeeDM xbee = XBeeDM(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);

    xbee.register_receive_cb(&receive_cb);
    RadioStatus radioStatus = xbee.init();
    MBED_ASSERT(radioStatus == Success);

    uint8_t current_channel;
    radioStatus = xbee.get_channel(&current_channel);
    if (radioStatus != Success) {
        log_serial->printf("Error reading the Channel\r\n");
        current_channel = 0;
    }
    log_serial->printf("Current channel is '%02X', setting it to '%02X'\r\n", current_channel, NEW_CHANNEL);
    radioStatus = xbee.set_channel(NEW_CHANNEL);
    if (radioStatus != Success) {
        log_serial->printf("Error when setting channel\r\n");
    }

    uint16_t network_id;
    radioStatus = xbee.get_network_id(&network_id);
    if (radioStatus != Success) {
        log_serial->printf("Error reading the Network ID\r\n");
        network_id = 0;
    }
    log_serial->printf("Current Network ID is '%04X', setting it to '%04X'\r\n", network_id, NEW_NETWORK_ID);
    radioStatus = xbee.set_network_id(NEW_NETWORK_ID);
    if (radioStatus != Success) {
        log_serial->printf("Error when setting Network ID\r\n");
    }

    uint8_t encryption_key[] = {0x12, 0x34, 0x56, 0x78};
    log_serial->printf("Configuring Encryption Key\r\n");
    radioStatus = xbee.set_network_encryption_key(encryption_key, sizeof encryption_key);
    if (radioStatus != Success) {
        log_serial->printf("Error when setting encryption key\r\n");
    }

    log_serial->printf("Enabling Encryption\r\n");
    radioStatus = xbee.enable_network_encryption(true);
    if (radioStatus != Success) {
        log_serial->printf("Error when enabling encryption\r\n");
    }

    char current_node_identifier[21];
    radioStatus = xbee.get_node_identifier(current_node_identifier);
    if (radioStatus != Success) {
        log_serial->printf("Error reading the node identifier");
        current_node_identifier[0] = '\0'; /* Set to empty string. */
    }
    log_serial->printf("Current Node Identifier '%s', setting it to '%s'\r\n", current_node_identifier, NEW_NODE_ID);
    radioStatus = xbee.set_node_identifier(NEW_NODE_ID);
    if (radioStatus != Success) {
        log_serial->printf("Error setting the node identifier\r\n");
    }

    uint8_t current_power_level;
    radioStatus = xbee.get_power_level(&current_power_level);
    if (radioStatus != Success) {
        log_serial->printf("Error reading the Power Level\r\n");
        current_power_level = 0;
    }
    log_serial->printf("Current Power level is '%d', setting it to '%d'\r\n", current_power_level, NEW_POWER_LEVEL);
    radioStatus = xbee.set_power_level(NEW_POWER_LEVEL);
    if (radioStatus != Success) {
        log_serial->printf("Error when setting Power Level\r\n");
    }


    //xbee.write_config(); /* Uncomment this line to save changes into Flash. */

    for(;;) {
        xbee.process_rx_frames();
        wait_ms(1000);
    }
    delete(log_serial);
}