Skip to content

Instantly share code, notes, and snippets.

@ElectricImpSampleCode
Last active January 31, 2019 12:04
Electric Imp imp API imp.deepsleepfor() example code
// Process incoming data from the device. In this instance,
// we just log the received data
device.on("hourlydata", function(data) {
// Log the data
server.log(data);
});
// Log an ADC value each minute, only tell the server once an hour
// NOTE This example is written for the imp001, but can be easily
// modified for any other imp (but not the imp005, which does
// not support deep sleep)
local t = time();
function sendDataHourly() {
// Every hour relay stored data to the agent
agent.send("hourlydata", nv.data);
nv.data = "";
}
function readData() {
// Configure the imp001's Pin 9 for analog input
// NOTE Change the pin if you are using a different imp
hardware.pin9.configure(ANALOG_IN);
// Add the value read on the pin to the data store
nv.data = nv.data + format(" %d", hardware.pin9.read());
// Is it time to contact the server?
// If we're at minute 59, call 'sendDataHourly()' to send the data
local min = (t / 60) % 60;
if (min == 59) sendDataHourly();
// Now put the imp to sleep when impOS is ready
imp.onidle(function() {
// imp.deepsleepfor() does not flush the server buffer
server.flush(10);
imp.deepsleepfor(60 - (time() % 60));
});
}
// Set up the data store in 'nv' if it has not yet been created
if (!("nv" in getroottable())) nv <- { data = "" };
// At a cold or warm boot, go and get data
readData();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment