IMP Network Time Accuracy

Is there any good place to look for consolidated information on functions that return a time, similar to the ‘Sleeping and Waking’ wiki page ? It would be great to see at a glace what the functions are, what they’re referenced to (hardware clock as well as 0 reference), what their accuracy is, etc.

Not really, no; the devwiki has our functions (hardware.millis() and micros(), both of which are boot time referenced), but the clock() and time() calls are squirrel standards. Clock() is zero referenced and ticks at 100Hz, time() is unix time and ticks at 1Hz.

is there any reason you want to ask the imp server for time?

Project link that uses the Electric IMP:

My imp can keep time to within 1ms / hour drift. Using only your server to get within 10ms to begin with. Want to see the demo?

@sbright33: bring it on!

`
Agent:
local s,i;
for(i=0;i<20;i++) {
s=time();
while(s==time());
device.send(“dn”,(s+1));
}
while(1){
s+=100;
while(time()<s);
device.send(“dn”,s);
}

Device:
//imp.configure(“time”,[],[]);
local t,mt,dtm,dtm2; //dtm is difference between time/getms() and millis() kinda

function getms() {
local s,i,s1,s2;
i=(hardware.millis()%1000)-t;
if(i<0) i+=1000;
s1=s=time();
imp.sleep(0.1); //blocking
s2=time();
if((s1==s2)&&(i>920)) s–;
if((s2 >s1)&&(i<100)) s=s2;
//if((s2>s1)&&(i>900)) s=s1;
//return(s); //1365…
s=s%1000000; //not 1e6 only 2 weeks before wraps
return(mt=(s*1000+i+1000)); //2000?
}
local ms1,ms2,ms3,ms=0,n=0;

function fdn(value){
local x1,x2,x3;
dtm2=(value%1000000)*1000-hardware.millis()+50; //alternative excellent!
server.log(dtm2);
n++;
if(n==1) ms1=getms()%1000;
if(n==2) ms2=getms()%1000;
if(n==3) { ms3=getms()%1000;
x1=math.abs(ms1-ms);
x2=math.abs(ms2-ms1);
x3=math.abs(ms3-ms2);
if((x1<50)&&(x2<50)&&(x3<50)){
if(ms1<ms)ms=ms1;
if(ms2<ms)ms=ms2;
if(ms3<ms)ms=ms3;
if((mt/1000)%10 !=(value%10)) server.log(“Bad”); //time() drifts about 4ms/min
dtm= -hardware.millis()+getms()+50-ms; //order matters
ms2=ms; //2 return values
server.log(format(“dtm=%d”,dtm%1000));
if(math.abs(dtm-dtm2)>10) server.log(“dtm2 error”) else {
agent.on(“dn”,fdn2); //server.log(ms);
server.log(“RTC synced to server”);
}
}
ms=ms3;
n=0;
}}
//server.log((getms()+50-ms2)%10000);
//if((mt/1000)%10 !=(value%10)) server.log(“Bad”); //time() drifts about 4ms/min also in fdn()
//dtm does not change AT ALL when rebooting using Run button, ms2 does randomly because time() does
//dtm resets when power on
//dtm resets when deep sleep wake?
//use dtm and millis() to record real time
//drifts about 14ms/hour experimentally
//do not use time() and getms(), drifts 8s in an hour
//Agent does not reboot when power on, must reboot with Run button
//can fix with onconnect()

function fdn2(value){
local x;
x=hardware.millis();
x-=x/240000; //15ms 257k=14ms/hr
server.log(dtm+x);
//x=(dtm+hardware.millis())/100; //+650,000,000 changes every 2 weeks in tenths sec server.log(x);
//server.log((dtm+x)%100);
//server.log(time()); //1365e6…
}
function initt(){
local s;
imp.enableblinkup(true); //millis() off by 6% without
s=time();
while(s==time());
t=hardware.millis()%1000;
agent.on(“dn”,fdn);
}
function inittest(){ //this shows time() drifts about 4ms/min
local s,tt;
s=time();
while(s==time());
tt=hardware.millis()%1000;
server.log(tt-t);
imp.wakeup(30,inittest); //60 crashes
}
initt();
//inittest();
`

240000 is specific to my hardware. Yours may be different.
I have assumed it takes 100ms round trip from the east coast.

Any chance we might see NTP like time in the Device at some point?

Yep, there’s a chance of that. One issue is the RTC in the imp can’t be set with sub-second accuracy though, so it’s a bit of a pain. I believe it can be reset to a second boundary though.