3 X AA batteries lasted 24 hours on 30 second polls!

As the title need to know how to increase my lifespan to over a week on batteries?
I can poll at 30 minute intervals but i changed it to that after the 30 second polls and the batteries lasted about the same so im guessing the wifi is constantly on no matter the amount of time between polls?

Any help is appreciated as im meant to be putting my project into the hull of a ship on Wednesday and only have access once a week???


How does the imp connect to WiFi on the ship? Is there like a reliable hot-spot that is always on? Or, what kind of ship?

If you want batteries to last a long time, you need to make sure you’re optimizing in a few ways:

  1. Hardware - make sure you don’t have parts of your circuit that are constantly pulling power
  2. Wifi - make sure you’re using powersave mode, and ONLY connect when you need to connect
  3. Deep Sleep - deepsleep as much as possible.

The TempBug code is generally a good example of this.

The TempBug uses an NTC thermistor, but it’s hooked up so that current only flows when we trigger an ‘enable’ pin. This saves us tremendous amounts of power, as otherwise we would ALWAYS be drawing power for thermistor, even if we’re not reading it.

The TempBug also uses server.sleepfor, which puts the imp into its low power sleep mode.

We could further improve battery life by not sending the temperature every time we wake up. We could write our code so that every 5 minutes we wake up, sense the temperature, store it in the nv-table, then go back to sleep. Along with this, we could track the last time we sent the data up to the server, if it’s been more than an hour (or 6 hours, or a day, or whatever time period you want), we would send the contents of the nv-table.

Hopefully this gives you some ideas - if you want some more help, feel free to ask :slight_smile: This is probably a good forum discussion, as I think there are lots of people trying to do this.

Here’s some code I just remembered I wrote that wakes up every 15 seconds to sample data (in this case the battery voltage), saves it in the NV table, and then sends the contents of the NV table to it’s agent every 15 minutes.

`const SAMPLE_FREQ = 15; // sample every 15 seconds
const SEND_FREQ = 900; // send data to server every 15 minutes

function voltage(samples = 1) {
samples = samples * 1.0; // convert to float

if (samples <= 1) return hardware.voltage();

local total = 0.0;
for (local i = 0; i < samples; i++) total += hardware.voltage();
return total / samples;


function sample() {
// current timestamp
local _t = time();
local _v = voltage(10);

// get nv-table
if (!("nv" in getroottable() && "lastsend" in nv && "data" in nv)) nv <- { lastsend = _t, data = [] };

// add data point
nv.data.push({ t = _t, v = _v });

// if we need to send upstream
if (_t - nv.lastsend >= SEND_FREQ) {
    local r = agent.send("data", nv.data);
    if (r == 0) nv <- { lastsend = _t, data = {} };


imp.onidle(function() { server.sleepfor(SAMPLE_FREQ); });`

It does sound like you’re actually connected to wifi all the time. You should be using deep sleep if you want to use less power, as in beardedinventor’s example.

So how do i enable the powersave mode in my code. I dont need the batteries to last for months on end just a few weeks in between changes. Polling once every 10 mins.

Do i just enable the powesave at the end of my device code or do i have to do agent stuff to.?.


The way i see it…currently im drawing 60-70 Mah at nominal voltage. Which on my alkaline batts will last me about 24 hours…which is what i have been witnessing!, quite shocked…!

But using some decent 2000 Mah AA x 3 for 3.6v total. And using the powersave mode at around 5Mah. The batteries should last me around 2 weeks.?? Is that right or is my math way off!??

Anyway if its not i just need to enable that mode and hey presto!!

Ill post my code later and see what you guys think on where i can save power!,

This is fascinating!, i could do my dissertation on batteries alone at this stage.

Thanks guys

Great forum

Oh by the way yes the ship doesnt have wifi but it doesnt move and there i a hot spot close by tyatthe kind people have let me use for free

oh i think this has worked? i think?


// pin 9 is the middle of the voltage divider formed by the NTC - read the analog voltage to determine temperature

// all calculations are done in Kelvin

const b_therm = 3988;
const t0_therm = 298.15;

function getTemp() {
// scale the ADC reading to a voltage by dividing by the full-scale value and multiplying by the supply voltage
local v_therm = 3.3 * hardware.pin9.read() / 65535.0;
// calculate the resistance of the thermistor at the current temperature
local r_therm = 10000.0 / ( (3.3 / v_therm) - 1);
local ln_therm = math.log(10000.0 / r_therm);

local t_therm = (t0_therm * b_therm) / (b_therm - t0_therm * ln_therm) - 273.15;

// convert to fahrenheit for the less-scientific among us
local f = (t_therm) + 34 ;
// format into a string for the string output port
local f_str = format("%.01f", f)
server.log(“Current temp is “+f_str+” C”);

agent.send(“temp”, f);
agent.send(“rssi”, imp.rssi());

imp.onidle(function() { server.sleepfor(900); }); // 15 minutes


imp.configure(“Thermocouple MT Kent”, [], []);
server.log(format(“rssi %ddBm”,imp.rssi()));

Yeah, this is sleeping and should work fine.

There’s no need to be associated to wifi when you’re not using it (even in power save mode, this is a waste of power - it’s 3 orders of magnitude more power to stay online vs in powersave mode, about 7mA vs 6uA). Taking a reading, sending it, then sleeping again is the most power efficient way and your batteries could last years.

So sleeping like this my lithium AA batteries X 3 should last years?. Well its way surpassed my expectations already well two days as opposed to 1 day so far and still going strong…fantastic.

Yeah, I suspect, given that code up there, you should be lasting maybe 3+ years…

Oh, but looks like there will be power burnt though your NTC (you have one end to 3.3v, one end to ground, and are reading the middle… if you attached the ground end to a GPIO, then drove this pin low before you wanted to read it, then you wouldn’t burn power when the imp wasn’t reading the temperature). Right now, if you have a 10k thermistor and 10k bias resistor, then you’re burning about 27x as much power as the imp in the thermistor when asleep :slight_smile: