Can you save variable data into flash?

Posted on
  • Hi everyone.
    I have a strange issue that I am trying to solve.
    My puck.js gets pulses from my power meter, increments a variable value for day and hour totals and sends it via BLE. Now, I have noticed that on the receiving end the graph falls to zero and starts incrementing again, which tells me that my puck got restarted.
    Is there a way to save the current values into non volatile memory, so that if it restarts, it will continue from the value it had?


    1 Attachment

    • pulses.JPG
  • OK, worked out the storage part =)
    Now, I was going to set the timer function to update Date.now() to the storage, so that I can reference it after reboot and set the time to the previous value, but for some reason get an error:

    >Date.setTime(1642303291816.30810546875)­
    Uncaught Error: Function "setTime" not found!
     at line 1 col 6
    Date.setTime(1642303291816.30810546875)
    
  • Glad you got that sorted! I think you just need to do setTime and not Date.setTime and you should be fine :)

  • @Gordon - thank you, I got that part sorted =)
    Now, I have another annoyance - after some time of working ok, I get some random errors like this:

    >
    Uncaught ReferenceError: "pin" is not defined
     at line 1 col 1
    pin changes
    ^
    in function called from system
    Connected to PM
    Uncaught ReferenceError: "pin" is not defined
     at line 1 col 1
    pin changes
    ^
    in function called from system
    Uncaught ReferenceError: "unction" is not defined
     at line 1 col 1
    unction() { // every 10 seconds
    ^
    in function called from system
    Uncaught ReferenceError: "pin" is not defined
     at line 1 col 1
    pin changes
    ^
    in function called from system
    
    

    Its like its of memory or something. I just dont know what to check as after restart it starts working again... for some time

  • Do you think you could post up your full code? Because it looks like it's just trying to execute the code pin changes

  • it was just an example. Before it was giving similar errors but from a different sections of the code
    Usually it was coming up with errors around to where the comments are in the code.
    When this happens, it will actually be in the half working state - all functions and variables are not defined, but it was still sending BLE advertising using the last known values
    I have shared it on Gist here:

  • Ahh, right - what firmware version are you using?

    What's happened is the code is executed direct from flash, and you've written enough data to flash that the flash then has to be 'compacted' and things move around, but the function still points to the old area of flash which doesn't contain the right code any more.

    I'm pretty sure that on the latest firmwares (2v11, if not before) this is fixed and the function pointers are automatically updated.

  • Just to add - another fix is to make sure you run require("Storage").eraseAll() on the LHS of the IDE before upload. Now the code will be the first thing in flash memory so even if it gets compacted it won't move and won't cause problems.

  • At the moment I am just rebooting it every 15 mins by cutting the power. But that makes the time drift and no longer in sync
    How do I check the version?

  • Current firmware is 2v09.87
    Updated it to 2v11. Lets see how it goes

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

Can you save variable data into flash?

Posted by Avatar for user130485 @user130485

Actions