802.15.4 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 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 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

Network

You need to set up a coordinator in the network so it buffers the packets send to our local XBee device while it's sleeping.
A module configured as coordinator requires:

  • 'Coordinator Enable (CE)' parameter set to 1.
  • 'Coordinator Association (A2)' parameter set to 0x04 so the coordinator allows end device association.
  • 'Cyclic Sleep Period (SP)' parameter set as the local XBee device.
    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
  • 'Time Before Sleep (ST)' parameter set as the local XBee device; 500 ms = 0x1F4.

Demo run

While it is running, send a packet to the device:

  • Go to the "Console" tab of the XCTU connected to the coordinator.
  • Press the "Add API frame to the list" button.
  • Press "Create using API Frame generator" button.
  • Select "802.15.4" protocol and "0x00 - Tx (Transmit) Request 64-bit address" Frame Type.
  • Paste the Local XBee device 64-bit address in the "64-bit dest. address" text box. (You can copy it from the application banner).
  • In the "RF data" text box, select the ASCII box and type "Hello XBee!".
  • Press OK, Add Frame.
  • 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:
Mon May 11 18:03:56 2015 +0200
Revision:
0:8b05ec51d9b9
Automatic upload

Who changed what in which revision?

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