ZigBee Power Management using Cyclic Sleep example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * Copyright (c) 2015 Digi International Inc.,
00003  * All rights not expressly granted are reserved.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
00007  * You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
00010  * =======================================================================
00011  */
00012 
00013 #include "mbed.h"
00014 #include "XBeeLib.h"
00015 #if defined(ENABLE_LOGGING)
00016 #include "DigiLoggerMbedSerial.h"
00017 using namespace DigiLog;
00018 #endif
00019 
00020 using namespace XBeeLib;
00021 
00022 /* Configure Sleep Options */
00023 #define SLEEP_OPTIONS 0      /* Short Sleep */
00024 
00025 /* Configure Sleep Period in mS */
00026 #define SLEEP_PERIOD_MS (5000 / 10)   /* 5000 mS */
00027 
00028 /* Configure Time before sleep */
00029 #define TIME_BEFORE_SLEEP_MS 500    /* 500 mS */
00030 
00031 /* Configure Number of sleep periods */
00032 #define SLEEP_PERIOD_NUMBER 65535    /* High value so radio doesn't awake unnecessarily */
00033 
00034 
00035 Serial *log_serial;
00036 
00037 /** Callback function, invoked at packet reception */
00038 static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len)
00039 {
00040     const uint64_t remote_addr64 = remote.get_addr64();
00041 
00042     log_serial->printf("\r\nGot a %s RX packet [%08x:%08x|%04x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST",
00043                                       UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16(), len);
00044 
00045     log_serial->printf("%.*s\r\n\r\n", len, data);
00046 }
00047 
00048 int main()
00049 {
00050     log_serial = new Serial(DEBUG_TX, DEBUG_RX);
00051     log_serial->baud(9600);
00052     log_serial->printf("Sample application to demo cyclic sleep power management with the XBeeZB\r\n\r\n");
00053     log_serial->printf(XB_LIB_BANNER);
00054 
00055 #if defined(ENABLE_LOGGING)
00056     new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
00057 #endif
00058 
00059     XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
00060 
00061     /* Register callbacks */
00062     xbee.register_receive_cb(&receive_cb);
00063 
00064     RadioStatus const radioStatus = xbee.init();
00065     MBED_ASSERT(radioStatus == Success);
00066 
00067     /* Configure sleep parameters */
00068     AtCmdFrame::AtCmdResp cmdresp;
00069 
00070     cmdresp = xbee.set_param("SO", (uint32_t)SLEEP_OPTIONS);
00071     if (cmdresp != AtCmdFrame::AtCmdRespOk) {
00072         log_serial->printf("SO Failed!!\r\n");
00073     }
00074 
00075     cmdresp = xbee.set_param("SP", SLEEP_PERIOD_MS);
00076     if (cmdresp != AtCmdFrame::AtCmdRespOk) {
00077         log_serial->printf("SP Failed!!\r\n");
00078     }
00079 
00080     cmdresp = xbee.set_param("ST", TIME_BEFORE_SLEEP_MS);
00081     if (cmdresp != AtCmdFrame::AtCmdRespOk) {
00082         log_serial->printf("ST Failed!!\r\n");
00083     }
00084 
00085     cmdresp = xbee.set_param("SN", SLEEP_PERIOD_NUMBER);
00086     if (cmdresp != AtCmdFrame::AtCmdRespOk) {
00087         log_serial->printf("SN Failed!!\r\n");
00088     }
00089 
00090     /* Configure Sleep mode */
00091     cmdresp = xbee.set_param("SM", 4);  /* Cyclic Sleep */
00092     if (cmdresp != AtCmdFrame::AtCmdRespOk) {
00093         log_serial->printf("SM Failed!!\r\n");
00094     }
00095 
00096     /* Wait until the device has joined the network */
00097     log_serial->printf("Waiting for device to join the network: ");
00098     while (!xbee.is_joined()) {
00099         wait_ms(1000);
00100         log_serial->printf(".");
00101     }
00102     log_serial->printf("OK\r\n");
00103 
00104     /* Start processing frames */
00105     while (true) {
00106         xbee.process_rx_frames();
00107         wait_ms(100);
00108         log_serial->printf(".");
00109     }
00110 
00111     delete(log_serial);
00112 }