Hi.
I’m looking to sync my imp’s clock with the Agent’s time() function. Seems like it’s working when the device is online. However, I would like the code to keep running even offline (for example, when the WiFi is dropped for some reason), until it reconnects. I would then like it to use the hardware.millis() function.
The problem is that my device doesn’t seem to wake when offline! Is there something I’m missing? Does imp.wakeup() on the device work only when connected to server?
I’m attaching a piece of the code. I hope it helps understand my problem:
The Device Code:
`server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 5);
function fdn2(value){
dtm2 = (value%1000000)1000-hardware.millis()+50;
hhh = (value%86400)/3600;
if ((lasthh == (hhh - 1))&&((hhh == 20)||(hhh == 8))) {do_stuff();}
lasthh = hhh;
minsleft = (6060 - value%3600);
server.log(“Waking up in " + minsleft/60 + " minutes”);
imp.wakeup(minsleft,checkAgent);
}
function offline() {
local offlinevalue;
offlinevalue= dtm2 + hardware.millis();
hhh = (offlinevalue%86400)/3600;
if ((lasthh == (hhh - 1))&&((hhh == 20)||(hhh == 8))) {do_stuff();}
lasthh = hhh;
minsleft = (60*60 - offlinevalue%3600);
server.log("Waking up in " + minsleft/60 + " minutes");
imp.wakeup(minsleft,checkAgent);
}
function online() {
imp.onidle(agent.on(“dn”,fdn2));
agent.send(“trig”,0);
}
function checkAgent() {
agentup = 0;
imp.onidle(agent.on(“pong”,function(code){
agentup = 1; // Indicate Agent is up
}));
server.log(“Pinging agent…”);
agent.send(“ping”,0);
imp.wakeup(5,function(){if (!agentup) {server.log(“Imp offline”); offline()}
else {server.log(“Imp online”); online()}
})
};
checkAgent();`
The Agent code:
`function loop() {
imponline == false;
device.on(“ping”,function(code){
imponline == true;
server.log(“Ponging device…”);
device.send(“pong”,0);
});
device.on(“trig”,function(sendtime){
s = time();
while(s == time());
device.send(“dn”,(s+1));
}
}
loop();`
Thanks!