Web server

ESP8266 web server saves 60% power with 1Ms delay

Arduino has a library to quickly and easily set up a simple web server on an ESP8622-based board, and [Tomaž] found that power consumption on an ESP-01 can be drastically reduced by simply inserting a 1ms 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ž] use ESP8266WebServer’s “hello world” example to explain. In it, the main loop is basically calling server.handleClient() still. 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 puts the hardware to sleep whenever it doesn’t. However, this level of control is simply not possible in the context of Arduino’s ESP8266WebServer library.

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

Why does this work? To add delay(1) actually causes the processor 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ž]this one millisecond delay reduced idle power consumption at 3.3V from about 230mW to about 70mW, or about 60%, while only delaying web server response times by 6 to 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 barely sip 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.