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

Dependencies:   XBeeLib mbed

Description

This example characterizes a device that needs to be continuously joined to the coordinator so it doesn't lose any incoming packets. A fast response to packets is not required so the radio can sleep for some time to save power while the coordinator buffers its packets.

The example configures the radio to poll its coordinator every 5 seconds to check if there is any packet for it. If there is one or more, it awakes and send the packets to the mbed micro-controller.

Setup

Firmware

In S2B modules, router firmware will not work for this example, because routers don't support sleep modes.
Make sure the XBee module has end-device firmware.
To flash new firmware into an XBee module, you need to use the XCTU software.

Network

Make sure the coordinator has at Cyclic Sleep Period (SP) option a value greater than XBee module.
In the example it is set to 5000 which represent 5000 mS or 5 seconds.
In the coordinator, when using X-CTU for configuration, the SP parameter has to be entered as the desired sleep time divided by 10 and in hexadecimal so to cope with the example it should be 5000 / 10 = 500 = 0x1F4 -> 1F4
On a router or coordinator, SP determines the transmission timeout when sending to a sleeping end device. SP also determines how long the parent will buffer a message for a sleeping child.

Demo run

While it is running, go to the "Console" tab of the X-CTU connected to the coordinator. Press the "Add API frame to the list" and paste following bytes that create a new broadcast transmit request packet:

7E 00 19 10 01 00 00 00 00 00 00 FF FF FF FE 00 00 48 65 6C 6C 6F 20 58 42 65 65 21 5A

Select the frame and press the "Send selected frame" button to send the frame ("Hello XBee!") to the XBee network.

Once the XBee device receives the frame, the radio will wake up to send it to mbed micro-controller, which will process and print the received data.

Verify that the XBee module has received the frame by accessing the serial console terminal. If it was successful, the "Hello XBee!" message will be displayed there.

After that, the radio will go to sleep again until new data is received.

Committer:
hbujanda
Date:
Fri Jul 29 12:13:47 2016 +0200
Revision:
7:fe3aee4d263a
Parent:
4:378daca584ef
Automatic upload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hbujanda 2:cd92b9bcb7f1 1 /**
hbujanda 2:cd92b9bcb7f1 2 * Copyright (c) 2015 Digi International Inc.,
hbujanda 2:cd92b9bcb7f1 3 * All rights not expressly granted are reserved.
hbujanda 2:cd92b9bcb7f1 4 *
hbujanda 2:cd92b9bcb7f1 5 * This Source Code Form is subject to the terms of the Mozilla Public
hbujanda 2:cd92b9bcb7f1 6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
hbujanda 2:cd92b9bcb7f1 7 * You can obtain one at http://mozilla.org/MPL/2.0/.
hbujanda 2:cd92b9bcb7f1 8 *
hbujanda 2:cd92b9bcb7f1 9 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
hbujanda 2:cd92b9bcb7f1 10 * =======================================================================
hbujanda 2:cd92b9bcb7f1 11 */
hbujanda 2:cd92b9bcb7f1 12
hbujanda 2:cd92b9bcb7f1 13 #include "mbed.h"
hbujanda 2:cd92b9bcb7f1 14 #include "XBeeLib.h"
hbujanda 2:cd92b9bcb7f1 15 #if defined(ENABLE_LOGGING)
hbujanda 2:cd92b9bcb7f1 16 #include "DigiLoggerMbedSerial.h"
hbujanda 2:cd92b9bcb7f1 17 using namespace DigiLog;
hbujanda 2:cd92b9bcb7f1 18 #endif
hbujanda 2:cd92b9bcb7f1 19
hbujanda 2:cd92b9bcb7f1 20 using namespace XBeeLib;
hbujanda 2:cd92b9bcb7f1 21
hbujanda 2:cd92b9bcb7f1 22 /* Configure Sleep Options */
hbujanda 2:cd92b9bcb7f1 23 #define SLEEP_OPTIONS 0 /* Short Sleep */
hbujanda 2:cd92b9bcb7f1 24
hbujanda 2:cd92b9bcb7f1 25 /* Configure Sleep Period in mS */
hbujanda 2:cd92b9bcb7f1 26 #define SLEEP_PERIOD_MS (5000 / 10) /* 5000 mS */
hbujanda 2:cd92b9bcb7f1 27
hbujanda 2:cd92b9bcb7f1 28 /* Configure Time before sleep */
hbujanda 2:cd92b9bcb7f1 29 #define TIME_BEFORE_SLEEP_MS 500 /* 500 mS */
hbujanda 2:cd92b9bcb7f1 30
hbujanda 2:cd92b9bcb7f1 31 /* Configure Number of sleep periods */
hbujanda 2:cd92b9bcb7f1 32 #define SLEEP_PERIOD_NUMBER 65535 /* High value so radio doesn't awake unnecessarily */
hbujanda 2:cd92b9bcb7f1 33
hbujanda 2:cd92b9bcb7f1 34
hbujanda 2:cd92b9bcb7f1 35 Serial *log_serial;
hbujanda 2:cd92b9bcb7f1 36
hbujanda 2:cd92b9bcb7f1 37 /** Callback function, invoked at packet reception */
hbujanda 2:cd92b9bcb7f1 38 static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len)
hbujanda 2:cd92b9bcb7f1 39 {
hbujanda 2:cd92b9bcb7f1 40 const uint64_t remote_addr64 = remote.get_addr64();
hbujanda 2:cd92b9bcb7f1 41
spastor 4:378daca584ef 42 log_serial->printf("\r\nGot a %s RX packet [%08x:%08x|%04x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST",
hbujanda 2:cd92b9bcb7f1 43 UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16(), len);
hbujanda 2:cd92b9bcb7f1 44
hbujanda 2:cd92b9bcb7f1 45 log_serial->printf("%.*s\r\n\r\n", len, data);
hbujanda 2:cd92b9bcb7f1 46 }
hbujanda 2:cd92b9bcb7f1 47
spastor 4:378daca584ef 48 int main()
hbujanda 2:cd92b9bcb7f1 49 {
hbujanda 2:cd92b9bcb7f1 50 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
hbujanda 2:cd92b9bcb7f1 51 log_serial->baud(9600);
hbujanda 2:cd92b9bcb7f1 52 log_serial->printf("Sample application to demo cyclic sleep power management with the XBeeZB\r\n\r\n");
hbujanda 2:cd92b9bcb7f1 53 log_serial->printf(XB_LIB_BANNER);
hbujanda 2:cd92b9bcb7f1 54
hbujanda 2:cd92b9bcb7f1 55 #if defined(ENABLE_LOGGING)
hbujanda 2:cd92b9bcb7f1 56 new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
hbujanda 2:cd92b9bcb7f1 57 #endif
hbujanda 2:cd92b9bcb7f1 58
hbujanda 2:cd92b9bcb7f1 59 XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
hbujanda 2:cd92b9bcb7f1 60
hbujanda 2:cd92b9bcb7f1 61 /* Register callbacks */
hbujanda 2:cd92b9bcb7f1 62 xbee.register_receive_cb(&receive_cb);
hbujanda 2:cd92b9bcb7f1 63
hbujanda 2:cd92b9bcb7f1 64 RadioStatus const radioStatus = xbee.init();
hbujanda 2:cd92b9bcb7f1 65 MBED_ASSERT(radioStatus == Success);
hbujanda 2:cd92b9bcb7f1 66
hbujanda 2:cd92b9bcb7f1 67 /* Configure sleep parameters */
hbujanda 2:cd92b9bcb7f1 68 AtCmdFrame::AtCmdResp cmdresp;
hbujanda 2:cd92b9bcb7f1 69
hbujanda 2:cd92b9bcb7f1 70 cmdresp = xbee.set_param("SO", (uint32_t)SLEEP_OPTIONS);
hbujanda 2:cd92b9bcb7f1 71 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
hbujanda 2:cd92b9bcb7f1 72 log_serial->printf("SO Failed!!\r\n");
hbujanda 2:cd92b9bcb7f1 73 }
hbujanda 2:cd92b9bcb7f1 74
hbujanda 2:cd92b9bcb7f1 75 cmdresp = xbee.set_param("SP", SLEEP_PERIOD_MS);
hbujanda 2:cd92b9bcb7f1 76 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
hbujanda 2:cd92b9bcb7f1 77 log_serial->printf("SP Failed!!\r\n");
hbujanda 2:cd92b9bcb7f1 78 }
hbujanda 2:cd92b9bcb7f1 79
hbujanda 2:cd92b9bcb7f1 80 cmdresp = xbee.set_param("ST", TIME_BEFORE_SLEEP_MS);
hbujanda 2:cd92b9bcb7f1 81 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
hbujanda 2:cd92b9bcb7f1 82 log_serial->printf("ST Failed!!\r\n");
hbujanda 2:cd92b9bcb7f1 83 }
hbujanda 2:cd92b9bcb7f1 84
hbujanda 2:cd92b9bcb7f1 85 cmdresp = xbee.set_param("SN", SLEEP_PERIOD_NUMBER);
hbujanda 2:cd92b9bcb7f1 86 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
hbujanda 2:cd92b9bcb7f1 87 log_serial->printf("SN Failed!!\r\n");
hbujanda 2:cd92b9bcb7f1 88 }
hbujanda 2:cd92b9bcb7f1 89
hbujanda 2:cd92b9bcb7f1 90 /* Configure Sleep mode */
hbujanda 2:cd92b9bcb7f1 91 cmdresp = xbee.set_param("SM", 4); /* Cyclic Sleep */
hbujanda 2:cd92b9bcb7f1 92 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
hbujanda 2:cd92b9bcb7f1 93 log_serial->printf("SM Failed!!\r\n");
hbujanda 2:cd92b9bcb7f1 94 }
hbujanda 2:cd92b9bcb7f1 95
hbujanda 2:cd92b9bcb7f1 96 /* Wait until the device has joined the network */
hbujanda 2:cd92b9bcb7f1 97 log_serial->printf("Waiting for device to join the network: ");
hbujanda 2:cd92b9bcb7f1 98 while (!xbee.is_joined()) {
hbujanda 2:cd92b9bcb7f1 99 wait_ms(1000);
hbujanda 2:cd92b9bcb7f1 100 log_serial->printf(".");
hbujanda 2:cd92b9bcb7f1 101 }
hbujanda 2:cd92b9bcb7f1 102 log_serial->printf("OK\r\n");
hbujanda 2:cd92b9bcb7f1 103
hbujanda 2:cd92b9bcb7f1 104 /* Start processing frames */
hbujanda 2:cd92b9bcb7f1 105 while (true) {
hbujanda 2:cd92b9bcb7f1 106 xbee.process_rx_frames();
hbujanda 2:cd92b9bcb7f1 107 wait_ms(100);
hbujanda 2:cd92b9bcb7f1 108 log_serial->printf(".");
hbujanda 2:cd92b9bcb7f1 109 }
hbujanda 2:cd92b9bcb7f1 110
hbujanda 2:cd92b9bcb7f1 111 delete(log_serial);
hbujanda 2:cd92b9bcb7f1 112 }