Saving power on an ESP8266 web server using delays(tablix.org)
tablix.org
Saving power on an ESP8266 web server using delays
https://www.tablix.org/~avian/blog/archives/2022/08/saving_power_on_an_esp8266_web_server_using_delays/
7 comments
You can actually get the wifi state as low as 2 seconds in very ideal conditions.
Average less than 4 seconds.
If rssi is extremely low then it will def take longer.
Yea the voltage regulator is very important and the ones on the nodemcu have very high quiecient current. Also on the top end you have to look for regulators that can handle the ~500mA peak current too.
Otti Arc or Nordic PPk2 is an invaluable tool!
Average less than 4 seconds.
If rssi is extremely low then it will def take longer.
Yea the voltage regulator is very important and the ones on the nodemcu have very high quiecient current. Also on the top end you have to look for regulators that can handle the ~500mA peak current too.
Otti Arc or Nordic PPk2 is an invaluable tool!
People do say that WiFi reconnect can be done in 2 seconds, but I have never been able to achieve that myself on my network, no matter the signal strength. I tried static IP, static subnet, and various configs before going to sleep (to retain WiFi signal strength? I can't remember). I always get 5-6 seconds.
I think I remember measuring the NodeMCU with a higher Deep Sleep current than the D1 Mini, but I don't remember what that value was.
The other thing I forgot to mention that consumes current is the USB-Serial converter on boards like the D1 Mini and NodeMCU, but not present on the ESP-01. I don't know which consumes more, the voltage regulator or the USB-Serial converter.
I think I remember measuring the NodeMCU with a higher Deep Sleep current than the D1 Mini, but I don't remember what that value was.
The other thing I forgot to mention that consumes current is the USB-Serial converter on boards like the D1 Mini and NodeMCU, but not present on the ESP-01. I don't know which consumes more, the voltage regulator or the USB-Serial converter.
It also could be if you're still using 8266 vs esp32 or if you're on Arduino vs esp-idf. Arduino esp32 is a wrapper on top of the idf so not sure what added things could be slowing the program? Static ip might be even faster! Also, I have it initialized station mode.
I do recall the d1 mini having a really low quiescent current.
Yea the converters can also take up power I separate that and place it on a jig.
I do recall the d1 mini having a really low quiescent current.
Yea the converters can also take up power I separate that and place it on a jig.
The current-usage on WiFi power is just nuts.
Anyone trying to do wireless with low power should be using other protocols. Zigbee, Bluetooth, Z-Wave, ANT... anything but WiFi. I mean, WiFi just wasn't designed for low-power like these other protocols.
Hell, you might do better with LoRa (long-range, ~3-miles) signals than WiFi.
> Other ESP8266 dev boards (e.g. D1 Mini, NodeMCU) do support waking up from Deep Sleep. But these dev boards often contain a voltage regulator which consumes a quiescent current. On my D1 Mini boards, I get about 0.22 mA in Forced Deep Sleep mode. In theory, that's enough for 1 year of life on a single charge of a 2000 mAh battery. But waking it up every 15 minutes, for 5-6 seconds of WiFi time, reduces the battery life to about 4-5 months. At this point, the active power usage during the 5-6 seconds of WiFi is already 2X the power consumption of the 15 minutes of Deep Sleep, so I have not attempted to reduce the idle consumption any further.
Have you tried going even slower? You can cut your current usage in half by going every 30-minutes, and then in a quarter by connecting every 60-minutes (etc. etc.) until your current consumption is as low as you want.
There are practical limits (is one connection per day sufficient?), but 15 minutes still has some leeway depending on the application.
If you need low-power but still wireless and more updates than that... having a faster-to-initialize and lower power usage to boot (10mA for those 30ms, rather than for 100mA for 2000mS), you'll grossly cut down your power consumptions.
Anyone trying to do wireless with low power should be using other protocols. Zigbee, Bluetooth, Z-Wave, ANT... anything but WiFi. I mean, WiFi just wasn't designed for low-power like these other protocols.
Hell, you might do better with LoRa (long-range, ~3-miles) signals than WiFi.
> Other ESP8266 dev boards (e.g. D1 Mini, NodeMCU) do support waking up from Deep Sleep. But these dev boards often contain a voltage regulator which consumes a quiescent current. On my D1 Mini boards, I get about 0.22 mA in Forced Deep Sleep mode. In theory, that's enough for 1 year of life on a single charge of a 2000 mAh battery. But waking it up every 15 minutes, for 5-6 seconds of WiFi time, reduces the battery life to about 4-5 months. At this point, the active power usage during the 5-6 seconds of WiFi is already 2X the power consumption of the 15 minutes of Deep Sleep, so I have not attempted to reduce the idle consumption any further.
Have you tried going even slower? You can cut your current usage in half by going every 30-minutes, and then in a quarter by connecting every 60-minutes (etc. etc.) until your current consumption is as low as you want.
There are practical limits (is one connection per day sufficient?), but 15 minutes still has some leeway depending on the application.
If you need low-power but still wireless and more updates than that... having a faster-to-initialize and lower power usage to boot (10mA for those 30ms, rather than for 100mA for 2000mS), you'll grossly cut down your power consumptions.
You are not wrong about WiFi. But sometimes worse is better. :-)
With WiFi, I don't need any other technology stack, no hubs, no bridges. I can use my existing network infrastructure. The data packets from the ESP8266 devices get delivered directly to the MQTT broker over TCP/IP over WiFi. It's cheap (~$3 for a D1 Mini), dead simple, and good enough.
Every 15 minutes seems about right for temperature/humidity sensors. Increasing it beyond 15-30 minutes becomes a diminishing return, because at that point, the Deep Sleep power usage becomes greater than active WiFi usage.
With WiFi, I don't need any other technology stack, no hubs, no bridges. I can use my existing network infrastructure. The data packets from the ESP8266 devices get delivered directly to the MQTT broker over TCP/IP over WiFi. It's cheap (~$3 for a D1 Mini), dead simple, and good enough.
Every 15 minutes seems about right for temperature/humidity sensors. Increasing it beyond 15-30 minutes becomes a diminishing return, because at that point, the Deep Sleep power usage becomes greater than active WiFi usage.
Yeah, obviously getting the project done is highest priority. So if it works, and you're at .5 mA average, its good enough.
Still though, WiFi's specs are pretty awful. It bothers me to see such huge numbers whenever people talk about WiFi. And since we're in tiny microcontroller land, its not like any of us are actually tranferring GBs or MBs of data. We're all just passing a few kB, so much slower protocols (ex: Zigbee) are very reasonable.
Things start to get silly below the 500uA levels anyway. You start worrying about aluminum capacitors with 22uA leakage current and such... and realizing you need to upgrade to Tantalum capacitors with specified 2uA or 1uA leakage.
Still though, WiFi's specs are pretty awful. It bothers me to see such huge numbers whenever people talk about WiFi. And since we're in tiny microcontroller land, its not like any of us are actually tranferring GBs or MBs of data. We're all just passing a few kB, so much slower protocols (ex: Zigbee) are very reasonable.
Things start to get silly below the 500uA levels anyway. You start worrying about aluminum capacitors with 22uA leakage current and such... and realizing you need to upgrade to Tantalum capacitors with specified 2uA or 1uA leakage.
The Arduino IDE is very limiting. It hides a lot of these important implementation details in an attempt to keep things simple and easy to switch between Arduino-like devices (which is fine for users who aren't doing complex stuff or want all the features out of their particular chip or care much about optimization). I would instead recommend to use Espressif's SDK directly (https://docs.espressif.com/projects/esp8266-rtos-sdk/en/late...) so they have access to all the API the SDK offers.
That's a pretty big difference. From less than a day on single 18650 to nearly 5 days.
What is the simplest circuit to accurately measure power use?
A current-controlled power supply will often tell you the current-usage. Obviously, it depends on your power supply, but even a $50 one these days has this feature.
If you're off of battery power or USB power instead, you'll need to build a current-sense circuit. Analog wise, its very simple: a small 0.01 ohm resistor (called a "current sense resistor") is placed in series, and you measure the voltage drop across it. 0.0001 V across a 0.01 ohm resistor -> V / R -> 0.0001 / 0.01 == .01 Amps == 10 mA being drawn.
Now the question is "how do I measure 0.0001V accurately" ?? And that's a trivial op-amp problem. So trivial, that people sell cheap chips that already do this for you, called current-sensing amplifiers. They just multiply the voltage by 200 accurately. (so 0.0001V into the amplifier comes out as 0.02V), and 0.02V can be measured by pretty much anything.
https://www.digikey.com/en/products/detail/ohmite/MCS1632R01...
https://www.digikey.com/en/products/detail/onsemi/NCS199A3RS...
Voltage-multipliers though are a trivially easy OpAmp circuit, and I'd recommend that beginners actually try to build their own current-sense amplifiers from scratch. Its good to learn how to use OpAmps, and learn all the issues (ex: Rail to Rail, positive / negative voltages, etc. etc.). I'd recommend you do a 9V project with the LM358.
If you're off of battery power or USB power instead, you'll need to build a current-sense circuit. Analog wise, its very simple: a small 0.01 ohm resistor (called a "current sense resistor") is placed in series, and you measure the voltage drop across it. 0.0001 V across a 0.01 ohm resistor -> V / R -> 0.0001 / 0.01 == .01 Amps == 10 mA being drawn.
Now the question is "how do I measure 0.0001V accurately" ?? And that's a trivial op-amp problem. So trivial, that people sell cheap chips that already do this for you, called current-sensing amplifiers. They just multiply the voltage by 200 accurately. (so 0.0001V into the amplifier comes out as 0.02V), and 0.02V can be measured by pretty much anything.
https://www.digikey.com/en/products/detail/ohmite/MCS1632R01...
https://www.digikey.com/en/products/detail/onsemi/NCS199A3RS...
Voltage-multipliers though are a trivially easy OpAmp circuit, and I'd recommend that beginners actually try to build their own current-sense amplifiers from scratch. Its good to learn how to use OpAmps, and learn all the issues (ex: Rail to Rail, positive / negative voltages, etc. etc.). I'd recommend you do a 9V project with the LM358.
I think you might want to wire that opamp as an integrator, in this case.
An integrator should be in the circuit "somewhere", but there's a lot of creativity available where I left off.
Staying in the analog world alone, you can output the voltage into a 555 Timer, and then have it flash an LED every time the capacitor filled up (and the discharge would also dump the capacitor). For example.
Or you could just wire it up to a microcontroller and busy-loop ADC-convert the input. So you can perform the "integral / summation" in microcontroller / in C-code rather than circuit magic.
The important tidbit is that we've turned 10mA current into a value we can "touch" and use. Hell, maybe the easiest solution is to just hook up a wire somewhere and shove an oscilloscope there to finish things off, depending on how lazy you wanna get.
-------
I know one professional chip did Opamp Integral -> Comparator (if integral is full, dump the charge to ground. If +V, do register++. If integral was full of negative voltage, do register--).
Then, whenever the register overflowed, it'd generate an interrupt edge on its output (for both overflow or underflow). Alas, I forgot which chip did this. But this way, you could build all your stuff out of 100kOhm resistors, 10nF capacitors, tiny digital logic circuits with micro-amps of power usage that wakes up your microcontroller out of sleep for just a "Current_Reading++" statement on overflow and you go back to sleep. (or current_reading-- on underflow)
But that's all advanced stuff that probably doesn't matter for a student making their first practical OpAmp circuit :-)
Staying in the analog world alone, you can output the voltage into a 555 Timer, and then have it flash an LED every time the capacitor filled up (and the discharge would also dump the capacitor). For example.
Or you could just wire it up to a microcontroller and busy-loop ADC-convert the input. So you can perform the "integral / summation" in microcontroller / in C-code rather than circuit magic.
The important tidbit is that we've turned 10mA current into a value we can "touch" and use. Hell, maybe the easiest solution is to just hook up a wire somewhere and shove an oscilloscope there to finish things off, depending on how lazy you wanna get.
-------
I know one professional chip did Opamp Integral -> Comparator (if integral is full, dump the charge to ground. If +V, do register++. If integral was full of negative voltage, do register--).
Then, whenever the register overflowed, it'd generate an interrupt edge on its output (for both overflow or underflow). Alas, I forgot which chip did this. But this way, you could build all your stuff out of 100kOhm resistors, 10nF capacitors, tiny digital logic circuits with micro-amps of power usage that wakes up your microcontroller out of sleep for just a "Current_Reading++" statement on overflow and you go back to sleep. (or current_reading-- on underflow)
But that's all advanced stuff that probably doesn't matter for a student making their first practical OpAmp circuit :-)
I am not sure what your search for "simplest circuit" is as there are many defintions I would need to know first, e.g AC vs DC, power level, etc. to narrow it down.
Like others mentioned it is a about measuring voltage*current.
I found this video for low power DC measurements helpful as well https://www.youtube.com/watch?v=LUB8RWzzLWc&t=15s or for AC Power Meter https://www.youtube.com/watch?v=QXNlsawaYEg&t=868s
Like others mentioned it is a about measuring voltage*current.
I found this video for low power DC measurements helpful as well https://www.youtube.com/watch?v=LUB8RWzzLWc&t=15s or for AC Power Meter https://www.youtube.com/watch?v=QXNlsawaYEg&t=868s
I dunno if you want to use a simple circuit; but, you can use a voltmeter in series ampmeter mode. However, I just use one of these: https://www.amazon.com/gp/product/B00FPU6G4E which has the advantage of being able to tune the voltage and current to be either constant current or voltage.
We charge a capacitor up and time its discharge to a certain level while running the circuit.
Then we repeat it using resistors instead, until we match the discharge rate exactly.
Then measure the resistor and do I=V/R, using the midpoint V. That accounts for all the surges and eliminates most error sources, giving you a nice and extremely realistic average current draw.
Then we repeat it using resistors instead, until we match the discharge rate exactly.
Then measure the resistor and do I=V/R, using the midpoint V. That accounts for all the surges and eliminates most error sources, giving you a nice and extremely realistic average current draw.
I have an Otii Arc and Nordic PPK 2. Ppk2 is cheaper and the Otii is more accurate.
Just a battery would be the simplest option. Just measure time until empty.
If you want higher time resolution, there are several fully integrated hall effect current measurement ICs out there, like for example the ACS723.
If you need even better accuracy or resolution and you can spare 100 bucks, just buy one of the Nordic Power Profiler Kits.
If you want higher time resolution, there are several fully integrated hall effect current measurement ICs out there, like for example the ACS723.
If you need even better accuracy or resolution and you can spare 100 bucks, just buy one of the Nordic Power Profiler Kits.
That is the way. Once you get into actual measurement there is plenty of hard problems, like your device might only wake up for 10ms, take few hundred mAs of current and go to sleep.
You can place a small resistor in series with the supply, and measure the voltage drop over it. From this and using Ohm's law you can calculate the current. Then the power is trivial.
There are special resistors for this purpose that are very low in resistance.
There are special resistors for this purpose that are very low in resistance.
How accurate can you make this approach? What if I wanted to make sure that the load was not stealing even a dollarcent of energy per year?
As accurate as resistor and measurement circuit.
The data acquisition is kinda a bitch if you want to measure accurately over long time, small IoT device can take microamps when idle and tens of even hundred+ mA when transmitting and that's 4-5 orders of magnitude of dynamic range and that's kinda hard to deal with accuracy
https://www.joulescope.com/ handles most of that edgecases but it's a pricy piece of kit if you just want to do some measurements and are not using it to make actual product.
The data acquisition is kinda a bitch if you want to measure accurately over long time, small IoT device can take microamps when idle and tens of even hundred+ mA when transmitting and that's 4-5 orders of magnitude of dynamic range and that's kinda hard to deal with accuracy
https://www.joulescope.com/ handles most of that edgecases but it's a pricy piece of kit if you just want to do some measurements and are not using it to make actual product.
I wonder if you could rely on a capacitor charging over time, that you discharge every time you poll? That way you delegate the real-time "counting" to the capacitor and can read it at approximate times (as long as it's frequent enough to avoid fully charging the capacitor since it can't count beyond that)?
Pretty much. Just need to be very stable source and a lot of generic engineering around to not incur error.
Hell, that's actually very easy way to make ADC, just start counting, charge cap to pre-determined value then trigger counter stop. There are few improvements to that technique (like dual slope ADC, where you use reference voltage to discharge the cap to get the ratio between reference and input signal) but in general it can be very accurate way to measure.
Hell, that's actually very easy way to make ADC, just start counting, charge cap to pre-determined value then trigger counter stop. There are few improvements to that technique (like dual slope ADC, where you use reference voltage to discharge the cap to get the ratio between reference and input signal) but in general it can be very accurate way to measure.
measure the current, e.g. an ampmeter in series; voltage in parellel. W=V.I
Most of the time you care about the current only. Now if you actually want power over time, that is energy. in that case you would need some logging, and sampling
Most of the time you care about the current only. Now if you actually want power over time, that is energy. in that case you would need some logging, and sampling
The sampling is the problem here. If you sample at say 1s and the chip is using say 100mA for 50ms to send some data, you might completely miss it, you might overcount that pulse
well yeah, of course, if you care about transients, you may switch to an oscilloscope.
Alternatively have a multiplemeter that records min/max, and/or has a rs-232 interface.
However with enough sampling, even 1Hz frequency should be sufficient.
However with enough sampling, even 1Hz frequency should be sufficient.
Oscilloscopes also have a dead-time. If you want to judge the power usage accurately you pretty much have to go to dedicated stuff like https://www.joulescope.com/ althought you might get close enough with using some tricks. Or the ye olde analogue way of integrating the current usage via
Like, if you know your device is on 50ms and off 10 seconds you can just record both states power usage and calculate based on ratio, most scopes memory is probably enough for 50ms with enough resolution and idle power usage will most likely be the same over the period.
> Alternatively have a multiplemeter that records min/max, and/or has a rs-232 interface.
Min/max ain't going to do you much, some WIFI-enabled devices can spike to like 500mA for few us so even if it captures it the max doesn't tell you much.
Like, if you know your device is on 50ms and off 10 seconds you can just record both states power usage and calculate based on ratio, most scopes memory is probably enough for 50ms with enough resolution and idle power usage will most likely be the same over the period.
> Alternatively have a multiplemeter that records min/max, and/or has a rs-232 interface.
Min/max ain't going to do you much, some WIFI-enabled devices can spike to like 500mA for few us so even if it captures it the max doesn't tell you much.
If I remember correctly, ESP chips are not really power efficient comparing with other alternatives. And if you want to save power without sacrificing performance, you should do things like sleep and wait for interrupt.
Can you recommend a more efficient alternative that is reasonably hobbyist-friendly?
HuwytGreen(5)
mromanuk(1)
The `delay(1)` triggers the "Automatic Modem Sleep" mode, which keeps the WiFi connection alive. It should draw about 15-16 mA, which is pretty close to the 18-24 mA measured in the TFA.
The WiFi radio consumes a lot of power. During initialization, I believe it can consume 500 mA or more. In normal usage, WiFi apparently has a continuous drain of ~70 mA. If WiFi is not needed, the ESP8266 can be placed into "Forced Deep Sleep" mode, with an idle current of 0.02 mA. Using a ~2000 mAh battery (e.g. 3xAA NiMh, or 1x18650 Li-Ion), that means over 11 years on battery, if no other work is performed. The lowest current that I have measured personally is 0.04 mA on various ESP-01 modules. But that still means about 5.5 years on a single charge.
If the WiFi is needed only intermittently, the ESP8266 can be placed into "Forced Deep Sleep" mode, with the internal RTC configured to wake it up after a certain time, e.g. 15-30 minutes. It can then connect to WiFi, transmit the data payload, then go back to Deep Sleep again. Unfortunately, the ESP-01 module does not support this (without hardware hacking), because it does not expose the GPIO16/D0 pin necessary to trigger the RESET.
Other ESP8266 dev boards (e.g. D1 Mini, NodeMCU) do support waking up from Deep Sleep. But these dev boards often contain a voltage regulator which consumes a quiescent current. On my D1 Mini boards, I get about 0.22 mA in Forced Deep Sleep mode. In theory, that's enough for 1 year of life on a single charge of a 2000 mAh battery. But waking it up every 15 minutes, for 5-6 seconds of WiFi time, reduces the battery life to about 4-5 months. At this point, the active power usage during the 5-6 seconds of WiFi is already 2X the power consumption of the 15 minutes of Deep Sleep, so I have not attempted to reduce the idle consumption any further.