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:
Thu Mar 31 11:42:20 2016 +0200
Revision:
5:a75c2c21666f
Parent:
4:8b3ec5997504
Child:
6:70eef80784f8
Automatic upload

Who changed what in which revision?

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