
{"id":286,"date":"2017-05-21T21:05:41","date_gmt":"2017-05-21T11:05:41","guid":{"rendered":"http:\/\/bakke.online\/?p=286"},"modified":"2022-01-22T20:10:56","modified_gmt":"2022-01-22T09:10:56","slug":"reducing-wifi-power-consumption-on-esp8266-part-2","status":"publish","type":"post","link":"https:\/\/www.bakke.online\/index.php\/2017\/05\/21\/reducing-wifi-power-consumption-on-esp8266-part-2\/","title":{"rendered":"Reducing WiFi power consumption on ESP8266, part 2"},"content":{"rendered":"<p>In my <a href=\"https:\/\/www.bakke.online\/index.php\/2017\/05\/21\/reducing-wifi-power-consumption-on-esp8266-part-1\/\">previous\u00a0post<\/a> I showed the baseline power consumption data for one of my ESP8266-based weather monitors.<\/p>\n<p>The device wakes up from deep sleep, reads some sensors, connects to a WiFi network and transmits the readings over MQTT, it will then go back to sleep for 5 minutes. \u00a0One such reporting cycle would consume 0.164 mAh.<\/p>\n<p>This time I&#8217;ll show the first steps on the way to reducing this to less than half.<\/p>\n<p><!--more--><\/p>\n<h3>Disabling WiFi when waking up<\/h3>\n<p>As the WiFi radio is on when the ESP wakes up, we wake up with 70 mA current even if\u00a0we&#8217;re not using the WiFi yet.<\/p>\n<p>To try to reduce this, let&#8217;s switch off the WiFi radio at the beginning of the setup() function, keep it off while we&#8217;re reading the sensors, and switch it back on when we are ready to send the results to the server.<\/p>\n<pre><span style=\"color: #008080;\">void<\/span> <span style=\"color: #808000;\">setup<\/span>() {\n  <span style=\"color: #ff6600;\"><strong>WiFi<\/strong><\/span>.<span style=\"color: #ff6600;\">mode<\/span>( WIFI_OFF );\n  <span style=\"color: #ff6600;\"><strong>WiFi<\/strong><\/span>.<span style=\"color: #ff6600;\">forceSleepBegin<\/span>();\n  <span style=\"color: #ff6600;\">delay<\/span>( 1 );<\/pre>\n<p>In my experiments, I have found that both WiFi.mode() and WiFi.forceSleepBegin() were required in order to switch off the radio. \u00a0The forceSleepBegin() call will set the flags and modes necessary, but the radio will not actually switch off until control returns to the ESP ROM. \u00a0To do that we&#8217;re adding a delay( 1 ), but I suppose a yield() would work as well.<\/p>\n<p>Then, just before the calls to establish the WiFi connection, we switch the radio back on:<\/p>\n<pre><span style=\"color: #ff6600;\"><strong>WiFi<\/strong><\/span>.<span style=\"color: #ff6600;\">forceSleepWake<\/span>();\n<span style=\"color: #ff6600;\">delay<\/span>( 1 );\n\n<span style=\"color: #999999;\">\/\/ Bring up the WiFi connection<\/span>\n<span style=\"color: #ff6600;\"><strong>WiFi<\/strong><\/span>.<span style=\"color: #ff6600;\">mode<\/span>( <span style=\"color: #008080;\">WIFI_STA<\/span> );\n<span style=\"color: #ff6600;\"><strong>WiFi<\/strong><\/span>.<span style=\"color: #ff6600;\">begin<\/span>( WLAN_SSID, WLAN_PASSWD );<\/pre>\n<p>As above, forceSleepWake() will set the correct flags and modes, but the change will not take effect until control returns to the ESP ROM, so we add a delay( 1 ) call here as well.<\/p>\n<p>Let&#8217;s have a look at the result&#8230;<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/05\/version2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-287\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/05\/version2-1024x514.png\" alt=\"\" width=\"840\" height=\"422\" \/><\/a><\/p>\n<p>Ah, we can clearly see the radio switching off at the beginning of the sketch and it coming back after the 1.2 seconds it takes to read the sensors.<\/p>\n<p>(Un)fortunately, the DHCP ended up being faster this time, so we can&#8217;t look at the total energy usage, but will have to calculate how much was saved while the radio was off. \u00a0The current has dropped from 71 mA to 17 mA, giving us a 54 mA saving. \u00a0Over 1.2 seconds that gives us 0.018 mAh. \u00a0Not much, but on the right track.<\/p>\n<h3>Using WAKE_RF_DISABLED<\/h3>\n<p>There&#8217;s still a sharp power peak as the ESP wakes up, and by the time the setup() is called, we will have used 0.008 mAh already.<\/p>\n<p>To avoid this we can\u00a0go to sleep using the WAKE_RF_DISABLED flag. \u00a0This configures the chip to keep the radio disabled until told to enable it.<\/p>\n<p>So we&#8217;ll change the call to ESP.deepSleep:<\/p>\n<pre><span style=\"color: #ff6600;\"><strong>WiFi<\/strong><\/span>.<span style=\"color: #ff6600;\">disconnect<\/span>( <span style=\"color: #33cccc;\">true<\/span> );\n<span style=\"color: #ff6600;\">delay<\/span>( 1 );\n\n<span style=\"color: #808080;\">\/\/ WAKE_RF_DISABLED to keep the WiFi radio disabled when we wake up<\/span>\nESP.deepSleep( SLEEPTIME, WAKE_RF_DISABLED );<\/pre>\n<p><span style=\"color: #1a1a1a;\"><span style=\"font-family: Merriweather, Georgia, serif;\"><span style=\"font-size: medium;\">The calls to WiFi.disconnect() and delay() are needed in order to ensure the chip goes into proper deep sleep. \u00a0Without them, the chip usually ends up consuming about 1.2 mA of current while sleeping. \u00a0This indicates that deep sleep was not achieved and that the chip is in Power Save DTIM3 mode. \u00a0(0.86 mA according to\u00a0<\/span><\/span><\/span><a href=\"http:\/\/www.esp8266.com\/wiki\/doku.php?id=esp8266_power_usage\"><span style=\"color: #007acc;\"><span style=\"font-family: Merriweather, Georgia, serif;\"><span style=\"font-size: medium;\">this<\/span><\/span><\/span><\/a><span style=\"color: #1a1a1a;\"><span style=\"font-family: Merriweather, Georgia, serif;\"><span style=\"font-size: medium;\">, a little bit for the voltage regulator and another little bit for the faint sleep LED.)<\/span><\/span><\/span><\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/05\/version3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-288\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/05\/version3-1024x483.png\" alt=\"\" width=\"840\" height=\"396\" \/><\/a><\/p>\n<p><span style=\"color: #1a1a1a;\"><span style=\"font-family: Merriweather, Georgia, serif;\"><span style=\"font-size: medium;\">OK, the ESP now wakes up with the radio disabled, it stays disabled until after the sensors have been read and we can collect another 0.006 mAh reduction for a total of 0.024 mAh. \u00a0Not much, but there are bigger gains to be had next time by <a href=\"https:\/\/www.bakke.online\/index.php\/2017\/05\/22\/reducing-wifi-power-consumption-on-esp8266-part-3\/\">changing how the WiFi connection is being managed<\/a>.<\/span><\/span><\/span><\/p>\n<h3><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>In my previous\u00a0post I showed the baseline power consumption data for one of my ESP8266-based weather monitors. The device wakes up from deep sleep, reads some sensors, connects to a WiFi network and transmits the readings over MQTT, it will then go back to sleep for 5 minutes. \u00a0One such reporting cycle would consume 0.164 &hellip; <a href=\"https:\/\/www.bakke.online\/index.php\/2017\/05\/21\/reducing-wifi-power-consumption-on-esp8266-part-2\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Reducing WiFi power consumption on ESP8266, part 2&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,25,1],"tags":[8,27,9,23,24],"class_list":["post-286","post","type-post","status-publish","format-standard","hentry","category-arduino","category-the-notwork","category-uncategorized","tag-arduino","tag-energy-efficience","tag-esp8266","tag-internet-of-things","tag-notwork"],"_links":{"self":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/comments?post=286"}],"version-history":[{"count":1,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/286\/revisions"}],"predecessor-version":[{"id":1339,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/286\/revisions\/1339"}],"wp:attachment":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/media?parent=286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/categories?post=286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/tags?post=286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}