Problem with setTimeout() and deep sleep mode

Posted on
  • Hello everyone,

    I am using the current release of Espruino together with the Espruino Board v1.4b and I experienced a weird problem with setTimeout() and deep sleep by using the following peace of code:

    function perform() {
        blink(5000, function() {
            setTimeout(function() {
                perform();
            }, 3600 * 1000);
            setDeepSleep(1);
        });
    };
    var blink = function(t, callback) {
        var on = false;
        var i = setInterval(function() { 
            on = !on;
            digitalWrite(LED2, on);
        }, 500);
        setTimeout(function() { 
            digitalWrite(LED2, false);
            clearInterval(i);
            callback();
        }, t);
    };
    perform();
    

    This code shall let the device blink for 5sec every hour and go into deep sleep mode after blinking. This works for a while and then it suddenly stops blinking without any error being raised or the light is blinking faster with very low power.

    The batteries are fully charged. This behaviour can not be reproduced when the device is connected via USB.

    Do you have an idea?

    Best,

    Tobias

  • That sounds strange - how long does it take before the problem happens? Can you reconnect via USB afterwards? And when you do that does blinking resume again?

    Maybe if you can log in when it happens, you could check getTime() and make sure that the value is rising at a rate of 1 per second.

    I've set one up here with your code, so I'll see if I can get it to happen.

  • Hi Gordon,

    it is different. I recognized it for the first time after 3 hours. When I reconnected the USB it directly continued blinking. For shorter intervals (less than one hour) it happend faster.

    I will set the interval a little shorter and try to log in when it happens again.

    Best,

    Tobias

  • I noticed - on mistakes (miscoding) - on my part, when interval/timeout things repeat more often or get faster than expected, that I had multiple 'the same' intervals/timeouts running at the same time: my code created the same interval/timeout unintentionally again time skewed. From 'severely' glancing over the code I do not think this should happen in your piece of code, but give it a check... (log the interval id's and timeout id's you get when setting them together with the variable names you stick the interval into (---> i), and also which ones you clear... this may help you detect multiples running or exclude 'my theory' from being a possible cause).

    (since you use an onboard LED, I exclude the option of an auto reset / brown out on power break down as have been noticed when LED strings - are run from the board's power or board supplying USB power (if not 8, then 9 RGB LEDs on full white will tear down a standard USB).

    @Gordon, could it be that the timeout/s is/are not 'completed' when the deep sleep set's in which the deep sleep 'command' resides? ...or some reentrant issues in timing code... I guess I goe on wild goose chase and fishing in a muddy pond (muddy for me ignorant, crystal clear for you as the creator, @Gordon!)

  • Actually setDeepSleep doesn't sleep immediately, it just sets a flag. You only actually need to call it once in the whole program, so any subsequent calls don't do anything.

    I'm not really sure what the issue could be - my only guess so far would be that it's using the external real-time clock, and that it's unstable - either stopping, or oscillating wildly causing time to run too quickly. It's just a guess though - it's very hard to know anything without a bit more info.

  • I replaced the blink function by logging the current system time to the SD-Card frequently. First I tried to use a 20sec interval, this worked fine (but I just tested a few minutes). When I tried to use it with an hourly interval I just had two entries after 1 day testing. The clock seems to work in a proper way.

    After that I did a polyfill for the Espruino related functions and tested my code in Node.js. There I don't have any timing problems, so it must have something to do with deep sleep. Today I am testing the code by using setInterval().

  • Maybe try without deep sleep? I've left mine running and it still seems to be going fine. I can't say I've noticed the blinking but when I plug in via USB everything seems to still be working correctly...

  • This will be the next thing I will try when setInterval() did not succeed.

    The strange thing is, when it stopped working, all the time when I reconnected the device to USB it continued working for some time.

  • Stupid question, but are you using an up to date firmware on the board? I know a while back there were issues where big values for setTimeout/setInterval overflowed an internal counter - I can't remember if that was on the firmware that comes flashed onto the boards when you get them.

    I can actually log into the board I have here and type dump(). I can see the setTimeout that it shows has a value that gradually decreases from 1 hour each time I run it, so the state doesn't seem to have got corrupted...

  • I am currently using firmware version 1v73. Did you try to power the board just by a battery and disconnect the USB cable as well? It just happens in this particular setup.

  • Yes, it's sitting on my desk like that right now - it's been running for the last 2 days without a reset.

    It's not that there's a bad connection between the board and the battery is it? If the board becomes disconnected for a small amount of time it can 'brown out' and end up in bad states.

  • I used the offical JST connector and soldered it to a 9V clip. I am using that clip with a battery holder for 8 * 1,2 V accumulators. I'll try to use other batteries.

    As well I will do the tests with another Espruino Board again.

  • Thanks! Sorry I can't be more help... If I could reproduce it here I could do something about it.

    I'm using an old 3.7v LiPo phone battery here.

  • No problem. Thank you for your support so far. If I find out what's happening I'll inform you again.

  • Actually setDeepSleep doesn't sleep immediately, it just sets a flag. You only actually need to call it once in the whole program, so any subsequent calls don't do anything.

    In other words, it is enabling / disabling deep sleep - and deep sleep is entered when enabled and 'the work/event queue' became empty. Very interesting.

    #sleep #deepsleep

  • setInterval() didn't work as well

  • Did you check that getTime was working when you plugged in? It should be reading in seconds since the board was started - so it'd be interesting to see if that was off somehow.

  • When I plug in getTime is working like a charm. I will test what it shows after one day running.

  • Did you find the issue with this in the end? I'm noticing similar issues with my set interval on my gsm GPS tracker. I sleep for 1 hour then wake and send coordinates. This seems to fire inconsistently and even when running lower intervals e.g. 30 mins. However 5 mi utes seems to be fine

  • Are you using latest version? Changelog suggests that v79 fixes a bug with timeouts and deepsleep.

  • As @DrAzzy says, it was only recently discovered and fixed. You may well not have had the latest version...

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Problem with setTimeout() and deep sleep mode

Posted by Avatar for net-tobi @net-tobi

Actions