Device code running even if disconnected

Is this device code correct ?

It works (dunno exactly how) but I have lot of doubts about timeout values and asynchronous processes…

server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30); function doit() { server.log("AmHere"); agent.send("AmHere", 1); if (!server.isconnected()) { server.connect(function(result) { server.log("Device reconnected"); }, 30); } imp.wakeup(10.0, doit); } doit();

Thank You !

This code isn’t ever actually sleeping. imp.wakeup() keeps the imp running.

If you replaced the imp.wakeup() line with:

imp.onidle(function() { server.sleepfor(10); });
…then it’d boot, log “amhere”, deep sleep for 10 seconds, then log “device reconnected”, and repeat from the deep sleep for 10 seconds (IF the connect worked - you’re not checking for success in the server.connect callback).

The AmHere log/agent.send will only be seen on a cold boot, because when it wakes after a sleep with wifi off, the server.log() and agent.send() will just return an error because there’s no connection at that point.

Hi Hugo,
what about this piece of device code ?

`
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30);
imp.onidle(function() { server.sleepfor(300); });

x <- server.load();
if (!(“a” in x)) { x[“a”] <- 0; }

agent.on(“STORE”, function(y) { x[“a”] <- y; server.save(x); });

function count() {
server.log("a = " + x[“a”]);
x[“a”] += 1;
}
count();
`

In this case the IMP logs a = 0 then goes to sleep for 5’
When it wakes after sleep the server.load will be executed again ?
What happens if the wifi is OFF ?
During the sleep the agent can send the STORE message ?
Is there a message buffer when IMP sleeps or when wifi is off ?

I need to know the differences with imp.wakeup(300, count); for agent messages.

Thank you

Consult https://electricimp.com/docs/resources/wifistatediagram/

Given the above device code, when the imp wakes up after its five minutes, it goes into the ACTIVE-OFFLINE state. The server.load() returns null, because the server hasn’t been contacted yet. Then count() is called; the server.log does nothing (doesn’t start a connection) because we’re in RETURN_ON_ERROR. So the code never reconnects to the server, which is probably not what you want.

In RETURN_ON_ERROR mode, an imp that wakes from deep sleep must call server.connect() in order to reconnect to the server by entering (consult the diagram) the ACTIVE-CONNECTING and then ACTIVE states.

Peter

Yeah Peter you right ! I correct the code but I still need help about some questions:

  1. When it wakes after sleep (in this new code) the server.load(); works ?
  2. During the sleep the agent can send the STORE message ?
  3. Is there a message buffer when IMP sleeps or when wifi is off ?

`
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30);
imp.onidle(function() { server.sleepfor(300); });

if (!server.isconnected()) { server.connect(function(result) { server.log(“Device reconnected”); }, 30); }

x <- server.load();
if (!(“a” in x)) { x[“a”] <- 0; }

agent.on(“STORE”, function(y) { x[“a”] <- y; server.save(x); });

function count() {
server.log("a = " + x[“a”]);
x[“a”] += 1;
}
count();
`

Thanks !

  1. The server.load() won’t work until and unless the connection succeeds – if that logic is essential, it should move into the server.connect completion handler. Do note that the connection handler can be called with “result” indicating a failure, not just a success.

2 and 3. There’s no built-in buffer – messages sent while the imp is offline are discarded – but you can build one yourself using the agent’s device.onconnected handler, as mentioned in this current thread: http://forums.electricimp.com/discussion/2613

Peter