LowPowerLab Forum

Software support => Pi Gateway => Topic started by: mario.almeida on February 07, 2017, 02:54:00 PM

Title: Timer example
Post by: mario.almeida on February 07, 2017, 02:54:00 PM
Hi All,

Anyone here has an example on how to create a timer?

If SwitchMote is turn on, a timer should be created to turn off the SwitchMote after 1 hour and if SwitchMote is turn off before 1 hour the timer should be canceled and also it should show me the remaining time to turn off the SwitchMote (in secs)
Title: Re: Timer example
Post by: ChemE on February 07, 2017, 04:12:42 PM
If you don't care about high precision, this would work:


#include "LowPower.h"

uint16_t sleep_count=0;
do LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
while (++sleep_count < 450);  // Sleep for 1 hour
Title: Re: Timer example
Post by: Felix on February 08, 2017, 01:37:48 PM
ChemE's code will sleep the MCU/SwitchMote which you may not really want if button presses are important (that could be achieved by using pin change interrupts but I assume you still want to use the default sketch that doesn't sleep anything, with minimal changes).

So you can simply count time without dealing with timers. Save a millis() reading to a uint32_t timerstart variable when you begin the cycle, then every loop check if the current millis() - timerstart > 3600000, which is 1 hour, then turn on/off and reset the variable to latest millis() etc. This was done in several of the RFM69 lib examples.
Title: Re: Timer example
Post by: mario.almeida on February 09, 2017, 03:16:37 PM
Hi Felix,

By doing this way means, I'll have to do it on all the SwitchMote in Arduino sketch and each time I change the hour(ON time) I'll have re-upload the sketch. I want something to do on the PiGateway as an event. So dynamically I want to change the time as per the input value from the UI.
Title: Re: Timer example
Post by: mario.almeida on February 10, 2017, 05:37:05 AM
Hi Felix,

I want to create something like this.

Quote
exports.events = {
  switchMote0OFF_30MIN : {
    label:'SwitchMote : Off after 30 min',
    icon:'clock',
    descr:'Automatically turn off SwitchMote0 after being ON for 30 min',
        nextSchedule:function(node) {
          return 30000; // 30sec for testing
        },
        scheduledExecute:function(node) {
          db.findOne({ _id : node._id }, function (err, nodeRightNow) {
            if (nodeRightNow) {
              console.log('***> SM0 is '+nodeRightNow.metrics['B0'].value);
              if (nodeRightNow.metrics['B0'] && nodeRightNow.metrics['B0'].value == 'ON') {
                sendMessageToNode({nodeId:nodeRightNow._id, action:'BTN0:0'});
              };
            };
          });
        }
}
}

Above schedule runs every 30sec , how to I make the schedule to run only if SM0 is ON?
Title: Re: Timer example
Post by: mario.almeida on February 10, 2017, 08:01:34 AM
I manage to get it working this way.

Quote
exports.events = {
  switchMote0OFF_30MIN : {
    label:'SwitchMote : Off after 30 min',
    icon:'clock',
    descr:'Automatically turn off SwitchMote0 after being ON for 30 min',
    serverExecute:function(node) {
      if (node.metrics['B0'] && node.metrics['B0'].value == 'ON' && (Date.now() - new Date(node.metrics['B0'].updated).getTime() < 2000)) {
        setTimeout(function() { sendMessageToNode({nodeId:node._id, action:'BTN0:0'}); }, 3000);
      };
    }
}
}

Now I have 3 question.

1) Input field to enter the timeout value
2) How do I display the remaining time to turn OFF
3) Cancel the timeout if SM0 is turned OFF
Title: Re: Timer example
Post by: mario.almeida on February 10, 2017, 08:16:23 AM
Ok, manage to get the timeout to cancel. Now I need to see how to get question 1 and 2


Quote
var SM0_timeout_call;
exports.SMB0OnDate = null;
exports.events = {
  switchMote0OFF_30MIN : {
    label:'SwitchMote : Off after 30 min',
    icon:'clock',
    descr:'Automatically turn off SwitchMote0 after being ON for 30 min',
    serverExecute:function(node) {
      if (node.metrics['B0'] && node.metrics['B0'].value == 'ON' && (Date.now() - new Date(node.metrics['B0'].updated).getTime() < 2000)) {
        SM0_timeout_call = setTimeout(function() { sendMessageToNode({nodeId:node._id, action:'BTN0:0'}); }, 10000);
      };

      if (node.metrics['B0'] && node.metrics['B0'].value == 'OFF') {
        clearTimeout(SM0_timeout_call);
      };
    }
}
}