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:
Thu Jul 28 10:36:50 2016 +0000
Revision:
7:0b34ce30516a
Parent:
4:378daca584ef
Child:
8:f7fdee8c39a4
Initial commit

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 7:0b34ce30516a 34 #define REMOTE_NODE_ADDR64_MSB ((uint32_t)0x0013A200)
hbujanda 7:0b34ce30516a 35
hbujanda 7:0b34ce30516a 36 //#error "Replace next define with the LSB of the remote module's 64-bit address (SL parameter)"
hbujanda 7:0b34ce30516a 37 //#define REMOTE_NODE_ADDR64_LSB ((uint32_t)0x01234567)
hbujanda 7:0b34ce30516a 38 #define REMOTE_NODE_ADDR64_LSB ((uint32_t)0x40F86A88)
hbujanda 7:0b34ce30516a 39
hbujanda 7:0b34ce30516a 40 #define REMOTE_NODE_ADDR64 UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB)
hbujanda 2:cd92b9bcb7f1 41
hbujanda 2:cd92b9bcb7f1 42 Serial *log_serial;
hbujanda 2:cd92b9bcb7f1 43
hbujanda 2:cd92b9bcb7f1 44 /** Callback function, invoked at packet reception */
hbujanda 7:0b34ce30516a 45 static void receive_cb(const RemoteXBeeDM& remote, bool broadcast, const uint8_t *const data, uint16_t len)
hbujanda 2:cd92b9bcb7f1 46 {
hbujanda 2:cd92b9bcb7f1 47 const uint64_t remote_addr64 = remote.get_addr64();
hbujanda 2:cd92b9bcb7f1 48
spastor 4:378daca584ef 49 log_serial->printf("\r\nGot a %s RX packet [%08x:%08x|%04x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST",
hbujanda 2:cd92b9bcb7f1 50 UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16(), len);
hbujanda 2:cd92b9bcb7f1 51
hbujanda 2:cd92b9bcb7f1 52 log_serial->printf("%.*s\r\n\r\n", len, data);
hbujanda 2:cd92b9bcb7f1 53 }
hbujanda 2:cd92b9bcb7f1 54
spastor 4:378daca584ef 55 int main()
hbujanda 2:cd92b9bcb7f1 56 {
hbujanda 2:cd92b9bcb7f1 57 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
hbujanda 2:cd92b9bcb7f1 58 log_serial->baud(9600);
hbujanda 7:0b34ce30516a 59 log_serial->printf("Sample application to demo cyclic sleep power management with the XBeeDM\r\n\r\n");
hbujanda 2:cd92b9bcb7f1 60 log_serial->printf(XB_LIB_BANNER);
hbujanda 2:cd92b9bcb7f1 61
hbujanda 2:cd92b9bcb7f1 62 #if defined(ENABLE_LOGGING)
hbujanda 2:cd92b9bcb7f1 63 new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
hbujanda 2:cd92b9bcb7f1 64 #endif
hbujanda 2:cd92b9bcb7f1 65
hbujanda 7:0b34ce30516a 66 XBeeDM xbee = XBeeDM(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
hbujanda 7:0b34ce30516a 67
hbujanda 7:0b34ce30516a 68 const RemoteXBeeDM remoteDevice = RemoteXBeeDM(REMOTE_NODE_ADDR64);
hbujanda 2:cd92b9bcb7f1 69
hbujanda 2:cd92b9bcb7f1 70 /* Register callbacks */
hbujanda 2:cd92b9bcb7f1 71 xbee.register_receive_cb(&receive_cb);
hbujanda 2:cd92b9bcb7f1 72
hbujanda 7:0b34ce30516a 73 RadioStatus radioStatus = xbee.init();
hbujanda 2:cd92b9bcb7f1 74 MBED_ASSERT(radioStatus == Success);
hbujanda 2:cd92b9bcb7f1 75
hbujanda 2:cd92b9bcb7f1 76 /* Configure sleep parameters */
hbujanda 2:cd92b9bcb7f1 77 AtCmdFrame::AtCmdResp cmdresp;
hbujanda 2:cd92b9bcb7f1 78
hbujanda 2:cd92b9bcb7f1 79 cmdresp = xbee.set_param("SO", (uint32_t)SLEEP_OPTIONS);
hbujanda 2:cd92b9bcb7f1 80 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
hbujanda 7:0b34ce30516a 81 log_serial->printf("Setting SO Failed!!\r\n");
hbujanda 7:0b34ce30516a 82 goto end;
hbujanda 2:cd92b9bcb7f1 83 }
hbujanda 2:cd92b9bcb7f1 84
hbujanda 2:cd92b9bcb7f1 85 cmdresp = xbee.set_param("SP", SLEEP_PERIOD_MS);
hbujanda 2:cd92b9bcb7f1 86 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
hbujanda 7:0b34ce30516a 87 log_serial->printf("Setting SP Failed!!\r\n");
hbujanda 7:0b34ce30516a 88 goto end;
hbujanda 2:cd92b9bcb7f1 89 }
hbujanda 2:cd92b9bcb7f1 90
hbujanda 2:cd92b9bcb7f1 91 cmdresp = xbee.set_param("ST", TIME_BEFORE_SLEEP_MS);
hbujanda 2:cd92b9bcb7f1 92 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
hbujanda 7:0b34ce30516a 93 log_serial->printf("Setting ST Failed!!\r\n");
hbujanda 7:0b34ce30516a 94 goto end;
hbujanda 2:cd92b9bcb7f1 95 }
hbujanda 2:cd92b9bcb7f1 96
hbujanda 2:cd92b9bcb7f1 97 cmdresp = xbee.set_param("SN", SLEEP_PERIOD_NUMBER);
hbujanda 2:cd92b9bcb7f1 98 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
hbujanda 7:0b34ce30516a 99 log_serial->printf("Setting SN Failed!!\r\n");
hbujanda 7:0b34ce30516a 100 goto end;
hbujanda 7:0b34ce30516a 101 }
hbujanda 7:0b34ce30516a 102
hbujanda 7:0b34ce30516a 103 cmdresp = xbee.set_param("CE", 4); /* Indirect Message Poller */
hbujanda 7:0b34ce30516a 104 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
hbujanda 7:0b34ce30516a 105 log_serial->printf("Setting CE Failed!!\r\n");
hbujanda 7:0b34ce30516a 106 log_serial->printf("This example only works if both devices are S2C modules where asynchronous cyclic sleep is supported!!\r\n");
hbujanda 7:0b34ce30516a 107 goto end;
hbujanda 7:0b34ce30516a 108 }
hbujanda 7:0b34ce30516a 109
hbujanda 7:0b34ce30516a 110 /* Configure DH/DL with the address of the remoter device that acts
hbujanda 7:0b34ce30516a 111 as indirect message coordinator. Poll messages will be sent to this
hbujanda 7:0b34ce30516a 112 address each time the device awakes to check for pending messages */
hbujanda 7:0b34ce30516a 113 radioStatus = xbee.config_poll_destination(remoteDevice);
hbujanda 7:0b34ce30516a 114 if (radioStatus != Success) {
hbujanda 7:0b34ce30516a 115 log_serial->printf("config_poll_destination Failed!!\r\n");
hbujanda 7:0b34ce30516a 116 goto end;
hbujanda 2:cd92b9bcb7f1 117 }
hbujanda 2:cd92b9bcb7f1 118
hbujanda 2:cd92b9bcb7f1 119 /* Configure Sleep mode */
hbujanda 2:cd92b9bcb7f1 120 cmdresp = xbee.set_param("SM", 4); /* Cyclic Sleep */
hbujanda 2:cd92b9bcb7f1 121 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
hbujanda 7:0b34ce30516a 122 log_serial->printf("Setting SM Failed!!\r\n");
hbujanda 7:0b34ce30516a 123 goto end;
hbujanda 2:cd92b9bcb7f1 124 }
hbujanda 2:cd92b9bcb7f1 125
hbujanda 2:cd92b9bcb7f1 126 /* Start processing frames */
hbujanda 2:cd92b9bcb7f1 127 while (true) {
hbujanda 2:cd92b9bcb7f1 128 xbee.process_rx_frames();
hbujanda 2:cd92b9bcb7f1 129 wait_ms(100);
hbujanda 2:cd92b9bcb7f1 130 log_serial->printf(".");
hbujanda 2:cd92b9bcb7f1 131 }
hbujanda 2:cd92b9bcb7f1 132
hbujanda 7:0b34ce30516a 133 end:
hbujanda 2:cd92b9bcb7f1 134 delete(log_serial);
hbujanda 2:cd92b9bcb7f1 135 }