Imp goes from Online -> Offline, and then back online (even when logging every second)

Here’s something weird - my Imp keeps going from Online -> Offline and back online every 30 seconds or so. I’ve read posts in the past, where the solution was to log a message every ~5minutes to prevent the NAT from tearing down the connection - but that doesn’t seem to be the issue I’m facing.

I’ve tried server.log() every second and still have this issue. Look at the log, and the code here:

what happens if you try…

function testWake() { Server.log("test logging"); imp.wakeup(1, testWake); } testWake();

I don’t use imp.sleep() myself, so can’t compare directly.

Your log seems to show the issue every ~33 secs. Does look strange…

As @opb says, try using imp.wakeup instead of imp.sleep:

imp.sleep() blocks without processing messages from the server

Works like a charm! I assumed the Sleep command would work the same way it does in the Arduino, but I was wrong.

Is there a way to make it wakeup and continue where it left off, without having to wrap it in a function block?

No, because the imp is event driven; you need to “fall off” functions in order to let the network stack do some work.

Often this means you’d use a state machine to step through tasks that need to be separated by delays.

OPB’s and Phil’s solution is the idiomatic way of doing it, and the easiest to understand. But for purely academic interest: Squirrel can in fact “continue where it left off” if you use a generator. Generators are poorly-documented even by Squirrel standards, but here is some code that does the same thing as your original Squirrel:

`gen <- function() {
i <- 0;

while(1) {
    server.log("i is now "+i);
    yield imp.wakeup(1,function() { resume gen; });


resume gen;`


Thanks @Hugo, @Peter. I was reading the documentation on the wakeup() and it states that the resolution only goes up to centiseconds (0.01s), whereas I need it to be 0.0002seconds (20 usec).

Any suggestions on how I can get finer resolution using this, or any other method?

Oh, and thanks a TON for your help so far!

If you need a long sleep, seconds say, then use imp.wakeup(). If you need a short sleep, then use imp.sleep(). Using imp.sleep() only confuses the server if you stay asleep for tens of seconds in total, and, hopefully, if you’re sleeping for tens of seconds then that sleep needn’t be microsecond-accurate.


Though do bear in mind that even executing a line of Squirrel takes a noticeable time at the microsecond level. Really tight timing can’t be achieved by bit-banging directly from Squirrel; you might need to use a SPI port instead, or the sampler or fixed-frequency DAC, to have the timing taken care of in hardware.