Digi International Inc. / Mbed 2 deprecated XBee802_power_mngmnt_pin_sleep

Dependencies:   XBeeLib mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * Copyright (c) 2015 Digi International Inc.,
00003  * All rights not expressly granted are reserved.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
00007  * You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
00010  * =======================================================================
00011  */
00012 
00013 #include "mbed.h"
00014 #include "XBeeLib.h"
00015 #if defined(ENABLE_LOGGING)
00016 #include "DigiLoggerMbedSerial.h"
00017 using namespace DigiLog;
00018 #endif
00019 
00020 using namespace XBeeLib;
00021 
00022 #define SLEEP_SECONDS           (40)    /* 40 seconds, for demo */
00023 
00024 #if !defined(RADIO_SLEEP_REQ)
00025     #error "Please define RADIO_SLEEP_REQ pin in config.h"
00026 #endif
00027 #if !defined(RADIO_ON_SLEEP)
00028     #error "Please define RADIO_ON_SLEEP pin in config.h"
00029 #endif
00030 
00031 Serial *log_serial;
00032 
00033 DigitalOut* sleep_req = NULL;
00034 DigitalIn* on_sleep = NULL;
00035 
00036 bool is_radio_sleeping()
00037 {
00038     assert(on_sleep != NULL);
00039 
00040     return on_sleep->read() == 0;
00041 }
00042 
00043 void sleep_radio()
00044 {
00045     assert(sleep_req != NULL);
00046 
00047     sleep_req->write(1);
00048 }
00049 
00050 void awake_radio()
00051 {
00052     assert(sleep_req != NULL);
00053 
00054     sleep_req->write(0);
00055 
00056     /* Wait until radio awakes. Typically 14 mS */
00057     while(is_radio_sleeping());
00058 }
00059 
00060 static void send_sample(XBee802& xbee)
00061 {
00062     char data[60];
00063     static uint16_t sample = 0;
00064     const uint16_t data_len = sprintf(data, "\r\nSensor sample: %u, next sample in %lu seconds\r\n",
00065                                  sample++, (uint32_t)SLEEP_SECONDS);
00066 
00067     const TxStatus txStatus = xbee.send_data_broadcast((const uint8_t *)data, data_len);
00068     if (txStatus == TxStatusSuccess)
00069         log_serial->printf("send_data OK\r\n");
00070     else
00071         log_serial->printf("send_data failed with %d\r\n", (int) txStatus);
00072 }
00073 
00074 int main()
00075 {
00076     AtCmdFrame::AtCmdResp cmdresp;
00077 
00078     /* Configure cpu pins to control the radio power management pins:
00079      *     - RADIO_SLEEP_REQ: This pin connected to the radio SLEEP_RQ pin (pin 9 on THT modules, pin 10 on SMT modules) will
00080      *                   allow the cpu to request the radio to sleep or awake.
00081      *     - RADIO_ON_SLEEP: This pin connected to radio ON/SLEEP# pin (pin 13 on THT modules, pin 26 on SMT modules) will
00082      *                  allow the cpu to know if the radio is awaked or sleept.
00083      */
00084     sleep_req = new DigitalOut(RADIO_SLEEP_REQ);
00085     on_sleep = new DigitalIn(RADIO_ON_SLEEP);
00086 
00087     log_serial = new Serial(DEBUG_TX, DEBUG_RX);
00088     log_serial->baud(9600);
00089     log_serial->printf("Sample application to demo pin sleep power management with the XBee802\r\n\r\n");
00090     log_serial->printf(XB_LIB_BANNER);
00091 
00092 #if defined(ENABLE_LOGGING)
00093     new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
00094 #endif
00095 
00096     XBee802 xbee = XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
00097 
00098     RadioStatus radioStatus = xbee.init();
00099     MBED_ASSERT(radioStatus == Success);
00100 
00101     /* Configure Sleep mode */
00102     cmdresp = xbee.set_param("SM", 1); /* Pin Hibernate */
00103     if (cmdresp != AtCmdFrame::AtCmdRespOk) {
00104         log_serial->printf("SM Failed!!\r\n");
00105     }
00106 
00107     /* Start sending samples */
00108     while (1) {
00109         /* Awake the radio */
00110         awake_radio();
00111 
00112         send_sample(xbee);
00113 
00114         /* Sleep the radio again */
00115         sleep_radio();
00116 
00117         wait(SLEEP_SECONDS);
00118     }
00119 
00120     delete(log_serial);
00121 }