Skip to content

Instantly share code, notes, and snippets.

@ElectricImpSampleCode
Last active September 10, 2018 10:26
Show Gist options
  • Save ElectricImpSampleCode/bf084bd592f594bcce44 to your computer and use it in GitHub Desktop.
Save ElectricImpSampleCode/bf084bd592f594bcce44 to your computer and use it in GitHub Desktop.
This example shows how the imp API's nv table is used to preserve a single value across periodic deep sleeps.
// On wake from deep sleep or cold boot, Squirrel starts here
// Check if nv table doesn't exist or, if it does, whether it lacks the key 'count'
if (!("nv" in getroottable()) || !("count" in nv)) {
// Either the nv table hasn't been instantiated or it has but lacks a 'count' slot,
// so create them. If nv table exists, this code just adds 'count' to it. If nv
// doesn't exist yet, impOS will automatically create it when we add 'count'
nv <- { "count": 0 };
}
// Do some work...
// Increment the value of 'count' and log the result
nv.count++;
server.log("Device code pass: " + nv.count);
// Go to deep sleep for 10 seconds
imp.onidle(function(){
server.sleepfor(10.0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment