Event handling trouble

I’ve been trying to create a piece of code that allows me to switch a relay on and off. When it’s on (by sending a http request), to should blink. This part works fine, however I can’t in the name switch it off. In other words, I can’t leave the ‘timer’ function (strangely switch off is ledWrite(1) ):

`led <- hardware.pin9;
led.configure(DIGITAL_OUT);

function setLed(ledState) {
server.log("Set LED: " + ledState);
if(ledState == 0) {
timer();
server.log(“timer”);
}
else {
led.write(1);
server.log(“OFF”)
}
function timer(){
led.write(0);
imp.sleep(1.5);
led.write(1);
imp.wakeup(2,timer);
}
}
agent.on(“led”, setLed);`

Any ideas how could I leave the timer function?

Thank you Phil, unfortunately I get error on the console:

2014-10-22 16:03:06 UTC+1 [Status] Device disconnected 2014-10-22 16:03:07 UTC+1 [Status] Device Booting; 2.72% program storage used 2014-10-22 16:03:53 UTC+1 [Device] Set LED: 0 2014-10-22 16:03:53 UTC+1 [Device] ERROR: the index 'timer' does not exist 2014-10-22 16:03:53 UTC+1 [Device] ERROR: at setLed:11 2014-10-22 16:03:58 UTC+1 [Status] Device disconnected 2014-10-22 16:03:59 UTC+1 [Status] Device Booting; 2.72% program storage

Whoops accidently deleted my edit - change timer to timer0 :slight_smile:

Here it is again, with a couple of tweaks:

`led <- hardware.pin9;
led.configure(DIGITAL_OUT);

stillOn <- false;

function setLed(ledState) {
server.log("Set LED: " + ledState);
if(ledState == 0) {
stillOn = true;
timer0();
server.log(“timer”);
}
else {
stillOn = false;
led.write(1);
server.log(“OFF”)
}
}

function timer0(){
if(stillOn) {
led.write(0);
imp.wakeup(1.5,timer1);
}
}

function timer1(){
if(stillOn) {
led.write(1);
imp.wakeup(2,timer0);
}
}

agent.on(“led”, setLed);`

Alternatively you could call imp.cancelwakeup().

Perfect! Thank you very much!

Thanks to you Phil the heating in our home can finally be controlled remotely! The thermostat died years ago and we had to walk downstairs to switch the heating on and off manually.

I wonder how could I send messages to html? Is there a way to send messages to the agent and POST them?