• Hey Folks,

    so I'm working on a project, as part of which some code has to be executed on every wake up or power on but NOT after saving, mainly because a save can be the result of whats executed in onInit().

    My first approach was to just check within the onInit function if the content should be executed via a boolean. It looks something like this.

    var esp = require("ESP8266");
    var runInit = true;
    var sleepTime = 60000000;
    
    function qsave() {
       runInit = false;
       save();
    }
    
    function stuffToDo() {
    //Some code/changing variables
    qsave();
    esp.deepSleep(sleepTime, 1);
    }
    
    function onInit() {
       if (runInit) {
           stuffToDo();
       }
       else
          runInit = true;
    }
    

    So I think the problem here is pretty obvious. Once the runInit is set false it can effectively not be set back to true as it is not saved and thus lost as soon as the chip restarts.

    Right now i can't really think of any way around this, so if anyone has got an idea, i'd be very thankful.

    P.S. I already tried overwriting the reset reason propertie when saving so when reseting it would be set back to an actual reset reason. But it seems (and rightfully so, i guess) espruino doesn't allow the propertie to overwriten by the script.

    EDIT: Oh by the way, please tell me if there's any way to store single values directly without the use of an additional module because always overwriting the whole flash evertime i have to save some variable changes is pretty inefective in the long run anyway and of cause that would solve my problem with onInit()

  • You might be able to use E.setBootCode('var reset=true') and then test for existence in onInit().

  • But would a save() erease that? Because otherwise that would only work once.

  • so i just made a short test and it seems that while save leaves the set boot code untouched E.setBootCode ereases everything that has been saved prior

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

Detecting wether onInit is called by save() or Reset (Power On/Pin)

Posted by Avatar for Stipser @Stipser

Actions