Main Menu

Timer example

Started by mario.almeida, February 07, 2017, 02:54:00 PM

mario.almeida

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)

ChemE

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

Felix

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.

mario.almeida

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.

mario.almeida

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?

mario.almeida

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

mario.almeida

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);
      };
    }
}
}