Skip to content

Instantly share code, notes, and snippets.

@ElectricImpSampleCode
Last active September 10, 2018 14:25
Show Gist options
  • Save ElectricImpSampleCode/b32818ca626da7632b61 to your computer and use it in GitHub Desktop.
Save ElectricImpSampleCode/b32818ca626da7632b61 to your computer and use it in GitHub Desktop.
This example shows the use of Squirrel’s getroottable() function to test for the presence of the imp API nv table object at start-up.
function startup() {
// Check if nv table exists and if the key 'count' is in the nv table
if (!("nv" in getroottable() && "count" in nv)) {
// Create the nv table as a global variable with the key 'count'
nv <- { "count" : 0 };
// Add a second nv-persisted key, 'previous'
nv.previous <- "";
}
// Increment the value of 'count' and log the result
nv.count++;
local s = format("Pass %i", nv.count);
server.log("Current message: " + s);
if (nv.previous.len() > 0) server.log("Last message: " + nv.previous);
nv.previous = s;
// Go to deep sleep for ten seconds
imp.onidle(function(){
imp.deepsleepfor(10.0);
});
}
// When the code starts, call the startup() function
startup();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment