802.15.4 Power Management using Pin Sleep example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Description

This example characterizes a device that after taking samples from some sensors and sending the information collected through the radio, does nothing for a long period of time that could range from several minutes to several hours.
In that long period of inactivity it's not expected to have communication with the coordinator or other remote devices. The device will not be able to receive packets and should save as much power as possible, therefore the radio is set into low power.

The example does following cycle endlessly:

  • Some sensor is read. For demonstration, a counter is incremented.
  • For demonstration, a message containing the data collected during sampling stage is sent to a remote device.
  • After job has been done the radio will go to sleep:
  • First radio is requested to go to sleep. When radio finally sleeps, then the application waits for the time configured in the SLEEP_SECONDS define (40 seconds).
  • This time can be increased as desired.
  • After that time, the application will awake the radio through the On/Sleep pin and another cycle will start by taking a sample.

Setup

Application

Define RADIO_SLEEP_REQ and RADIO_ON_SLEEP in config.h file according to the mbed micro-controller GPIOs that will be used to control the XBee module power.

Hardware

It's necessary to wire following connections from the mbed micro-controller to the XBee radio according to the configuration done in the application:

  • From the mbed micro-controller RADIO_SLEEP_REQ to the XBee module SLEEP_RQ pin (pin 9 on THT modules, pin 10 on SMT modules) will allow the mbed micro-controller to request the radio to sleep or awake.
  • From the mbed micro-controller RADIO_ON_SLEEP to XBee module ON/SLEEP# pin (pin 13 on THT modules, pin 26 on SMT modules) will allow the mbed micro-controller to know if the radio is awake or slept.

Demo run

While the demo is running, you will see the frames sent by the XBee module through the serial console terminal.

Verify that the remote device is receiving the frames by accessing the "Console" tab of the XCTU.
If the frames are successfully sent, they will be displayed there, every 40 seconds.

     Sensor sample: 0, next sample in 40 seconds
     Sensor sample: 1, next sample in 40 seconds
Committer:
hbujanda
Date:
Mon May 11 18:04:04 2015 +0200
Revision:
0:daed954d256a
Automatic upload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hbujanda 0:daed954d256a 1 /**
hbujanda 0:daed954d256a 2 * Copyright (c) 2015 Digi International Inc.,
hbujanda 0:daed954d256a 3 * All rights not expressly granted are reserved.
hbujanda 0:daed954d256a 4 *
hbujanda 0:daed954d256a 5 * This Source Code Form is subject to the terms of the Mozilla Public
hbujanda 0:daed954d256a 6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
hbujanda 0:daed954d256a 7 * You can obtain one at http://mozilla.org/MPL/2.0/.
hbujanda 0:daed954d256a 8 *
hbujanda 0:daed954d256a 9 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
hbujanda 0:daed954d256a 10 * =======================================================================
hbujanda 0:daed954d256a 11 */
hbujanda 0:daed954d256a 12
hbujanda 0:daed954d256a 13 #include "mbed.h"
hbujanda 0:daed954d256a 14 #include "XBeeLib.h"
hbujanda 0:daed954d256a 15 #if defined(ENABLE_LOGGING)
hbujanda 0:daed954d256a 16 #include "DigiLoggerMbedSerial.h"
hbujanda 0:daed954d256a 17 using namespace DigiLog;
hbujanda 0:daed954d256a 18 #endif
hbujanda 0:daed954d256a 19
hbujanda 0:daed954d256a 20 using namespace XBeeLib;
hbujanda 0:daed954d256a 21
hbujanda 0:daed954d256a 22 //TODO: Configure pin according to your hardware
hbujanda 0:daed954d256a 23 #define SLEEP_REQ_PIN P0_4
hbujanda 0:daed954d256a 24
hbujanda 0:daed954d256a 25 //TODO: Configure pin according to your hardware
hbujanda 0:daed954d256a 26 #define ON_SLEEP_PIN P0_5
hbujanda 0:daed954d256a 27
hbujanda 0:daed954d256a 28 // TODO Replace with the MSB of the remote module's 64-bit address (SH parameter)
hbujanda 0:daed954d256a 29 #define REMOTE_NODE_ADDR64_MSB ((uint32_t)0x0013A200)
hbujanda 0:daed954d256a 30 // TODO Replace with the LSB of the remote module's 64-bit address (SL parameter)
hbujanda 0:daed954d256a 31 #define REMOTE_NODE_ADDR64_LSB ((uint32_t)0x40D2B03E)
hbujanda 0:daed954d256a 32
hbujanda 0:daed954d256a 33 #define REMOTE_NODE_ADDR64 UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB)
hbujanda 0:daed954d256a 34
hbujanda 0:daed954d256a 35 #define SLEEP_SECONDS (40) /* 40 seconds, for demo */
hbujanda 0:daed954d256a 36 //#define SLEEP_SECONDS (86400) /* 24 hours */
hbujanda 0:daed954d256a 37
hbujanda 0:daed954d256a 38 Serial *log_serial;
hbujanda 0:daed954d256a 39
hbujanda 0:daed954d256a 40 DigitalOut* _sleep_req = NULL;
hbujanda 0:daed954d256a 41 DigitalIn* _on_sleep = NULL;
hbujanda 0:daed954d256a 42
hbujanda 0:daed954d256a 43 void radio_sleep()
hbujanda 0:daed954d256a 44 {
hbujanda 0:daed954d256a 45 _sleep_req->write(1);
hbujanda 0:daed954d256a 46 }
hbujanda 0:daed954d256a 47
hbujanda 0:daed954d256a 48 void radio_on()
hbujanda 0:daed954d256a 49 {
hbujanda 0:daed954d256a 50 _sleep_req->write(0);
hbujanda 0:daed954d256a 51 }
hbujanda 0:daed954d256a 52
hbujanda 0:daed954d256a 53 bool is_radio_sleeping()
hbujanda 0:daed954d256a 54 {
hbujanda 0:daed954d256a 55 return _on_sleep->read() == 0;
hbujanda 0:daed954d256a 56 }
hbujanda 0:daed954d256a 57
hbujanda 0:daed954d256a 58 static void send_sample(XBee802& xbee, const RemoteXBee802& RemoteDevice)
hbujanda 0:daed954d256a 59 {
hbujanda 0:daed954d256a 60 char data[60];
hbujanda 0:daed954d256a 61 static uint16_t sample = 0;
hbujanda 0:daed954d256a 62 const uint16_t data_len = sprintf(data, "\r\nSensor sample: %u, next sample in %lu seconds\r\n",
hbujanda 0:daed954d256a 63 sample++, (uint32_t)SLEEP_SECONDS);
hbujanda 0:daed954d256a 64
hbujanda 0:daed954d256a 65 const TxStatus txStatus = xbee.send_data(RemoteDevice, (const uint8_t *)data, data_len);
hbujanda 0:daed954d256a 66 if (txStatus == TxStatusSuccess)
hbujanda 0:daed954d256a 67 log_serial->printf("send_data OK\r\n");
hbujanda 0:daed954d256a 68 else
hbujanda 0:daed954d256a 69 log_serial->printf("send_data failed with %d\r\n", (int) txStatus);
hbujanda 0:daed954d256a 70 }
hbujanda 0:daed954d256a 71
hbujanda 0:daed954d256a 72 int main()
hbujanda 0:daed954d256a 73 {
hbujanda 0:daed954d256a 74 AtCmdFrame::AtCmdResp cmdresp;
hbujanda 0:daed954d256a 75
hbujanda 0:daed954d256a 76 /* Configure cpu pins to control the radio power management pins:
hbujanda 0:daed954d256a 77 * - _sleep_req: This pin connected to the radio SLEEP_RQ pin (pin 9) will allow the cpu to request the radio
hbujanda 0:daed954d256a 78 * to sleep or awake.
hbujanda 0:daed954d256a 79 * - _on_sleep: This pin connected to radio ON/SLEEP# pin (pin 13) will allow the cpu to know if the radio
hbujanda 0:daed954d256a 80 * is awaked or sleept.
hbujanda 0:daed954d256a 81 */
hbujanda 0:daed954d256a 82 _sleep_req = new DigitalOut(SLEEP_REQ_PIN);
hbujanda 0:daed954d256a 83 _on_sleep = new DigitalIn(ON_SLEEP_PIN);
hbujanda 0:daed954d256a 84
hbujanda 0:daed954d256a 85 /* Set the radio on so we can send initialization messages to the radio */
hbujanda 0:daed954d256a 86 radio_on();
hbujanda 0:daed954d256a 87
hbujanda 0:daed954d256a 88 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
hbujanda 0:daed954d256a 89 log_serial->baud(9600);
hbujanda 0:daed954d256a 90 log_serial->printf("Sample application to demo how to send unicast and broadcast data with the XBee802\r\n\r\n");
hbujanda 0:daed954d256a 91 log_serial->printf(XB_LIB_BANNER);
hbujanda 0:daed954d256a 92
hbujanda 0:daed954d256a 93 #if defined(ENABLE_LOGGING)
hbujanda 0:daed954d256a 94 new DigiLoggerMbedSerial(log_serial, LogLevelDebug);
hbujanda 0:daed954d256a 95 #endif
hbujanda 0:daed954d256a 96
hbujanda 0:daed954d256a 97 XBee802 xbee = XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
hbujanda 0:daed954d256a 98
hbujanda 0:daed954d256a 99 RadioStatus radioStatus = xbee.init();
hbujanda 0:daed954d256a 100 MBED_ASSERT(radioStatus == Success);
hbujanda 0:daed954d256a 101
hbujanda 0:daed954d256a 102 /* Configure Sleep mode */
hbujanda 0:daed954d256a 103 cmdresp = xbee.set_param("SM", 1); /* Pin Hibernate */
hbujanda 0:daed954d256a 104 if (cmdresp != AtCmdFrame::AtCmdRespOk) {
hbujanda 0:daed954d256a 105 log_serial->printf("SM Failed!!\r\n");
hbujanda 0:daed954d256a 106 }
hbujanda 0:daed954d256a 107
hbujanda 0:daed954d256a 108 const RemoteXBee802 remoteDevice = RemoteXBee802(REMOTE_NODE_ADDR64);
hbujanda 0:daed954d256a 109
hbujanda 0:daed954d256a 110 /* Start sending samples */
hbujanda 0:daed954d256a 111 while (1) {
hbujanda 0:daed954d256a 112 /* Awake the radio */
hbujanda 0:daed954d256a 113 radio_on();
hbujanda 0:daed954d256a 114
hbujanda 0:daed954d256a 115 /* Wait until radio awakes. Typically 14 mS */
hbujanda 0:daed954d256a 116 while(is_radio_sleeping());
hbujanda 0:daed954d256a 117
hbujanda 0:daed954d256a 118 send_sample(xbee, remoteDevice);
hbujanda 0:daed954d256a 119
hbujanda 0:daed954d256a 120 /* Sleep the radio again */
hbujanda 0:daed954d256a 121 radio_sleep();
hbujanda 0:daed954d256a 122
hbujanda 0:daed954d256a 123 wait(SLEEP_SECONDS);
hbujanda 0:daed954d256a 124 }
hbujanda 0:daed954d256a 125
hbujanda 0:daed954d256a 126 delete(log_serial);
hbujanda 0:daed954d256a 127 }