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

Dependencies:   XBeeLib mbed

Fork of XBeeZB_power_mngmnt_pin_sleep by Digi International Inc.

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:03:48 2015 +0200
Revision:
0:5e030e8ab809
Automatic upload

Who changed what in which revision?

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