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