Well, I’m still not getting the results I’d like to, so here’s my code (which I’m assuming at this point is flawed):
`
const LIGHT_CHANGE_THRESHOLD = 1000;
local sensorOutput = OutputPort( “JSON Sensor Output”, “string” );
function readLighting() {
local adcValue = hardware.pin9.read();
if( math.abs( nv.lastValue - adcValue ) > LIGHT_CHANGE_THRESHOLD ) {
local json = {
impid = hardware.getimpeeid(),
voltage = hardware.voltage(),
signal = imp.rssi(),
light = adcValue,
time = hardware.millis()
};
// this will wake WiFi, in theory?
sensorOutput.set( json );
// stash this new value as the last one read for next time around
nv.lastValue = adcValue;
}
// schedule next read 5 seconds from now and turn off WiFi?
imp.deepsleepfor( 5 );
}
server.log( “Light sensing started” );
hardware.pin9.configure( ANALOG_IN );
imp.configure( “iPotti Number 2 Light Sensor”, [], [sensorOutput] );
imp.setpowersave( true );
if( !( (“nv” in getroottable()) && (“lastValue” in nv) ) ) {
nv <- { lastValue = 0 };
}
imp.onidle( readLighting );
`
I’m not convinced the two Imps are deep sleeping because I’m seeing log output every 5 seconds. As I think about it (writing this), is the WiFi waking up because I’m logging (to the server)? That would be very dumb of me to miss.
These forums are fantastic, BTW. Kudos to you, Hugo and your team, for a great device and support for it. Thanks, too, Impensor. I’d read that article on deepsleepfor() but for whatever reason, it wasn’t working for me in the context of my code. The example by itself works as advertised, of course.