Clock() function?

I’m new to squirrel and needed to time an event. (How long my garage door has been open)

I saw the clock() function which is documented as:

clock();
returns a float representing the number of seconds elapsed since the start of the process

I took this as meaning since the imp rebooted, I don’t need an absolute time, so I thought this would work fine.

I’m not getting back what I expect. I can restart my imp, a few seconds later call clock() and I get negative numbers from it, so just wondering if anyone else has used it or knows more details about it.

P.S. I don’t need to use it anymore, I found that my good ole unix time “time” was added to the date() function.

Thanks!

http://devwiki.electricimp.com/doku.php?id=electricimpapi:hardware:millis also explains the other timers available on the imp.

Peter

Perfect. Thanks!

@dreslism If you write up any interesting code, please share. I’ve been thinking about adding a timer to my security system/garage door code to send alerts if doors/windows are open to long, or after certain hours.

@jwehr sure.

Not doing anything really exciting, I send my self a notifcation using prowl api in agent, and prowl app on my iphone everytime the door opens or closes.

My kids(college age and beyond) are famous for opening the garage door after I’ve shut it for the night, so I was going to send myself a prowl notification if the door was open after 10:00 PM.

Then I thought, heck why notify me, just close it, then tell me it did it.

Then I realized I can’t just close it after 10:00 PM, as if someone came home after 10, it would close the door as soon as it opened, so I needed make sure the door was open for at least X duration after 10PM, then close it.

So that is why I’m timing the door open time.

Anyhow when my door input toggles, I

agent.send(“buttonOpen”, temp); or agent.send(“buttonClose”, temp);

I also have a temp sensor hooked up, so I can see the temp of the garage anytime the door opens or closes, or I query the states.

On the agent side, I store otime = time(); when the door opens, and I then calculate duration when the door closes and send the Open time in the door closed notification.

It’s cool to see “garage opened at 2:30PM temp=71F”, 5 to 10 minutes later I then get “garage closed, Open Time was 8m”.

That’s my son coming home and letting the dogs out after school.

Also I was doing this through a HTTP POST, via a webpage, but did not like it exposed. I saw your app, and it works perfect. I also integrated the agent handling json input from the little devil app, so I now have 2 different ways to query my garage door or close it.

Let me know if you want to see anything.

Did you use IoT Buddy or Pitchfork? Pitchfork has a control set up just for garage door control, with http response.

I might set some code to check the door at a certain time of night, just to make sure it was closed. I should hook up a speaker and play an alert… Door closing in 5, 4, 3, 2, 1 or something.

@jwehr, I used IoT Buddy, I did not know that pitchfork existed. I have my own notification system, so I don’t really need the http response.

The speaker thing is a great idea!

Just released Pitchfork last week. It sends JSON, allows you to send and API key, and has four project panels.

@dreslism are you using agent storage? As you time variables and timers will get cleared on agent re-start which happens from time to time. I do a lot of timer based stuff both in imp and agent.

@controlCloud not using agent storage at this moment.

You’re saying the agent can restart without the imp restart?

That’s not something I expected or considered.

Yep, Agents still in Beta and get restarted from time to time. Though not happering that offten now. In future release you will get A shutdown notification http://devwiki.electricimp.com/doku.php?id=electricimpapi:server:onshutdown&s[]=shutdown

@dreslism yes, agents can restart; they generally won’t restart when an imp does (imps can go online and offline - one of the uses of an agent is to be the consistent always running representation of an imp).

Right now, agents tend to get restarted when the agent environment is upgraded or has bugs fixed, and the frequency of this will reduce over time as we move out of beta. However, for various reasons (load balancing, server failovers, etc), agent code needs to be able to deal with being restarted, usually with notice as controlCloud notes.