DigiMesh Power Management using Asynchronous Cyclic Sleep example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Fork of XBeeZB_power_mngmnt_cyclic_sleep by Digi International Inc.

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.

Note that in DigiMesh modules, the polling mechanism required in this example is restricted to devices one hop away (Point to Multipoint delivery); it will not work fine across a DigiMesh network.

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.
  • '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.
  • 'Transmit Options (TO)' parameter set to 0x40 so delivery method is Point to Multipoint.

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 "DigiMesh" protocol and "0x10 Transmit Request" 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 "Options" text box, type 0x40 to select delivery method as Point To Multipoint (you can skip this if you configured atTO accordingly)
  • 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:37 2015 +0200
Revision:
0:4fadd116f813
Automatic upload

Who changed what in which revision?

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