ESP8266 web server saves 60% power with 1ms delay

Arduino has a library for quickly and easily setting up a simple web server on an ESP8622-based board, and [Tomaž] discovered that power consumption on an ESP-01 can be drastically reduced by simply inserting a 1 ms delay in the right place. The reason it works isn't because of some weird bug or weird feature - it's really just a side effect of the way the hardware works under the hood.

[Tomaž] uses ESP8266WebServer's "hello world" example to explain. In it, the main loop is basically calling server.handleClient() forever. This process checks incoming HTTP connections, handles them, sends responses, terminates, and then begins again. A simple web server like this spends most of its time waiting.

A much more efficient way to handle things would be to run server.handleClient() only when an incoming network connection calls it, and put the hardware to sleep each time that this does not happen. However, this level of control is simply not possible in the context of Arduino's ESP8266WebServer library.

So what? The next best thing happens to be a simple delay(1) statement right after each server.handleClient() call in the main loop.

Why does this work? Adding delay(1) causes the CPU to spend the vast majority of its time in this one millisecond loop. And counting microseconds turns out to be a much less power-intensive task than checking incoming network requests around a hundred thousand times a second. In [Tomaž]'s tests, this one millisecond delay reduced idle power consumption at 3.3V from about 230mW to about 70mW, or about 60%, while not delaying Web server response only 6-8 milliseconds.

For simple web server applications, this is definitely a good tip to keep in mind. There are also much more advanced techniques for saving power on ESP8266-based boards; from boards that draw barely a single microamp while asleep, to coin-cell powered boards that go so far as to modify the TCP/IP stack to help minimize possible power savings.

ESP8266 web server saves 60% power with 1ms delay

Arduino has a library for quickly and easily setting up a simple web server on an ESP8622-based board, and [Tomaž] discovered that power consumption on an ESP-01 can be drastically reduced by simply inserting a 1 ms delay in the right place. The reason it works isn't because of some weird bug or weird feature - it's really just a side effect of the way the hardware works under the hood.

[Tomaž] uses ESP8266WebServer's "hello world" example to explain. In it, the main loop is basically calling server.handleClient() forever. This process checks incoming HTTP connections, handles them, sends responses, terminates, and then begins again. A simple web server like this spends most of its time waiting.

A much more efficient way to handle things would be to run server.handleClient() only when an incoming network connection calls it, and put the hardware to sleep each time that this does not happen. However, this level of control is simply not possible in the context of Arduino's ESP8266WebServer library.

So what? The next best thing happens to be a simple delay(1) statement right after each server.handleClient() call in the main loop.

Why does this work? Adding delay(1) causes the CPU to spend the vast majority of its time in this one millisecond loop. And counting microseconds turns out to be a much less power-intensive task than checking incoming network requests around a hundred thousand times a second. In [Tomaž]'s tests, this one millisecond delay reduced idle power consumption at 3.3V from about 230mW to about 70mW, or about 60%, while not delaying Web server response only 6-8 milliseconds.

For simple web server applications, this is definitely a good tip to keep in mind. There are also much more advanced techniques for saving power on ESP8266-based boards; from boards that draw barely a single microamp while asleep, to coin-cell powered boards that go so far as to modify the TCP/IP stack to help minimize possible power savings.

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow