Imp going offline, when I want it to stay awake/online all the time

I have a simple imp arrangement that takes a temperature reading from a thermistor every minute and sleeps in between. I’ve recently modified the code to stay awake all the time so it can detect a button press on pin9. However, the device only functions as I’d expect for 6-7 minutes then it starts to report that it’s offline intermittently. It stays offline for up to 3 minutes before sparking back into life. To keep the imp alive I’m simply requeueing my mainloop as the onidle callback function. The device is USB powered. Is it overheating? I’m using the imp-card with a breakout board. I only have a thermistor and push button attached to it. If remove the button functionality and put the imp to sleep for 60s after reading the temperature, it functions for days without fault. Any advice?

`
function buttonChange(){
local buttonState = hardware.pin9.read();
if (buttonState==1){
server.log("Button Press: “+hardware.millis()+” "+nextReadTime);
}
else {
server.log(“Button Release”);
}
}

function mainLoop(){
local thisTime = hardware.millis();
if (thisTime>=nextReadTime){
readTemp(); // this reads the temperature and supply voltage (10 readings, 10ms apart)
nextReadTime = thisTime+60000; // read again in a minute
}
imp.onidle(mainLoop); // call mainLoop again, once other queued events are handled
}

hardware.pin9.configure(DIGITAL_IN,buttonChange);
nextReadTime <- 0;

`

That method should work, but obviously isn’t (which is a bug, which we’ll look into) - but it’s also a very inefficient way to do it.

If you want to call something again in a minute, do this:

imp.wakeup(60, thingtocall);

…that way the imp will quiesce everything into the lowest power state in the meantime. Your method is calling the main loop likely tens of thousands of times per second, which isn’t awfully efficient.

Generally imp.onidle() is used to ensure everything is done before you’re going to go to sleep. It should be allowing network traffic to be processed - not doing this is generally the issue if the server marks the imp offline - but maybe it doesn’t work right if you’re doing this so often.

What version is your imp running? (imp.getsoftwareversion())

Thanks Hugo, the imp.wakeup(60,callback) was how I coded it earlier. However, I’d assumed that the device wouldn’t wakeup from sleep to detect a change of state on pin9. If I attach a callback to an input pin, can I always assume that the device will wake from sleep upon change-of-state? I had thought it was only possible on pin1. The imp is running 27.9

imp.wakeup(60, callback) does not actually put the imp to sleep for 60 seconds - it tells the imp that in 60 seconds it should execute callback. imp.wakeup doesn’t put the imp to sleep, it just schedules something to run.

What are you are thinking of is imp.deepsleepfor which puts the imp into a deep sleep.

Thanks for the info. Based on that, imp.wakeup() is definitely what I need. It still doesn’t change the fact the my unit was going offline because I was continually calling imp.onidle().

Yep, could you confirm that this still happens? (I’m assuming you’re now on 27.9). If we can replicate it then we can fix it…