Simple Pause or Wait statement in Agent?

Two questions:

  • I have an agent that calls a PHP script. I want to have a 30 sec wait or delay after the call. How do I do it?

  • The command seems so common thought I would find it ‘somewhere’, but couldn’t. Where should I look for obvious RTFM questions on Agent?

Thanks!

Use Imp.wakeup(). It works in both the agent and the device, and allows you to schedule an function to be called at the interval that you set in the future, like so…
imp.wakeup(30, waitForMyScript);

Thanks.

I’m actually using a switch closure to call a PHP listed in swEvent with:
hardware.pin7.configure(DIGITAL_IN_PULLUP, swEvent);

So I really don’t want to schedule the event for every 30 seconds; juston switch closure. I am looking for a statement that I could put in the swEvent function to make the Imp “no nothing” for 30 seconds. In effect, I am wanting a 30 second “de-bounce” circuit on the switch I am monitoring implemented in software.

You can use imp.sleep().

imp.wakeup does not schedule events to happen every 30 seconds. It only schedules one event that will occur 30 seconds later. It would be appropriate for this case. If you are running on batteries and are concerned with battery life then perhaps you want imp.sleep() as jwehr has suggested. This is something I have never tried.

If you want recurring events then you must (re)-call your function inside the wake-up function.

I really like the wakeup function but I think the name is a little bit misleading. I think of it as an event scheduler and it is one of the bread and butter commands to use imho.

I have it working, but honestly I don’t fully understand why (This, BTW is not unusual :-O)

I ‘bare bones’ the code to help me debug things. As shown below, I was
imp.sleeping in the code in the “if block”.

Moving the imp.sleep outside the “if block” has things working. I am still unclear as to why the Imp would always enter the “if block” twice; never more and never less. I understand that a button switch changes value when pressed and when released, but I falsely assumed the “if state==1” would account for that.

Changing the imp.sleep time inside the “if block” would not have an effect. I even tried manually setting “state = 0;” at the end of the “if block”.

Result was always two executions of the if block. Thanks all!

`// function swEvent handles looking for action on the door switch
function swEvent() {
imp.sleep(5.0); // switch settle time of x.x seconds
// for some reason having settle time only in the ‘if’ block below resulted in two executions

    if (state == 1) {   // 
        server.log("I'm in the state==1 block);  
        imp.sleep(10.0);  // switch settle time of x.x seconds 
    }

} // End function swEvent`

Thanks for the advise. I think misdiagnosed and have some issue other than bounce (?).

Using
hardware.pin7.configure(DIGITAL_IN_PULLUP, swEvent);
The swEvent seems to execute the code in my
if (switchstate == 1) {
block twice; which is why I was blaming switch bounce.

However, the “if (switchstate == 1) {” always executes exactly twice regardless of the imp.sleep() time. Some server.log debug statements tell me imp.sleep() is delaying things, but regardless of the sleep time the result is always the same. Two passes separated by the imp.sleep() time. I have even tried setting
switchstate = 0;
right after entering the if condition code block. It still executes twice.

I’ll keep at it. Any comments are, of course, welcome.

a pin event will occur on the rising and falling edge (on a change). Even a perfect button press with no bounce will create two occurrences of the pin change event.

Hugo showed one way to handle this and bounce in this thread

PIN CHANGED CALLBACK

Really, and this is what other debounce code does, you want to trigger on the change, but then ignore subsequent changes within a window (bounces), vs just putting in sleeps.

The thing here is that subsequent changes are likely to still queue events to be run by your event handler. Sleeping just stops the imp from doing anything (including processing network traffic).

ie, something like this:

lastevent <- 0; function swEvent() { local state = hardware.pinX.read(); if (state == 1 && (hardware.millis()-lastevent) > 1000) { lastevent = hardware.millis(); server.log("got 0->1 transition, ignoring subsequent transitions for 1000ms"); } }

Hugo wrote>>>>
The thing here is that subsequent changes are likely to still queue events to be run by your event handler.
<<<<

Thanks. I saw this by following the link suggested above to put me on the right. Making more and more sense. B-)