
{"id":309,"date":"2017-05-22T20:55:45","date_gmt":"2017-05-22T10:55:45","guid":{"rendered":"http:\/\/bakke.online\/?p=309"},"modified":"2022-01-22T20:10:56","modified_gmt":"2022-01-22T09:10:56","slug":"reducing-wifi-power-consumption-on-esp8266-part-3","status":"publish","type":"post","link":"https:\/\/www.bakke.online\/index.php\/2017\/05\/22\/reducing-wifi-power-consumption-on-esp8266-part-3\/","title":{"rendered":"Reducing WiFi power consumption on ESP8266, part 3"},"content":{"rendered":"<p>Welcome to part 3 of this series on reducing WiFi power consumption on ESP8266 chips.<\/p>\n<p>Earlier, I have established the <a href=\"https:\/\/www.bakke.online\/index.php\/2017\/05\/21\/reducing-wifi-power-consumption-on-esp8266-part-1\/\">baseline power consumption<\/a> and shown how to reduce this a bit by <a href=\"https:\/\/www.bakke.online\/index.php\/2017\/05\/21\/reducing-wifi-power-consumption-on-esp8266-part-2\/\">disabling the radio<\/a> when it is not needed.<\/p>\n<p>This time, I&#8217;ll take it a step further by showing how to make sure the radio is needed for a shorter period of time.<\/p>\n<p><!--more--><\/p>\n<p>In the first part I showed that in my case, where the ESP8266 wakes up every 5 minutes to read some sensors and transmit the results to the server. \u00a0This whole process takes 8.3 seconds, and a whole 6.45 seconds of this is taken up by the\u00a0ESP8266 establishing a connection to the WiFi network and configuring itself by DHCP.<\/p>\n<h3>Disabling network persistence<\/h3>\n<p>I mentioned before that the ESP8266 will persist the network connection information to flash, and then read this back when it next starts the WiFi function. \u00a0It does this every time, and in my experiments I have found that this takes at least 1.2 seconds. \u00a0There are cases where the WiFi function would crash the chip, and the WiFi would never connect.<\/p>\n<p>The chip also does this\u00a0<strong>even when you pass connection information<\/strong> to WiFi.begin(), i.e. even in the case below:<\/p>\n<pre><span style=\"color: #ff6600;\"><strong>WiFi<\/strong><\/span>.<span style=\"color: #ff6600;\">begin<\/span>( WLAN_SSID, WLAN_PASSWD );<\/pre>\n<p>This will actually load the connection information from flash, promptly ignore it and use the values you specify instead, connect to the WiFi and then finally write your values back to flash.<\/p>\n<p>This starts wearing out the flash memory after a while. \u00a0Exactly how quickly or slowly will depend on the quality of the flash memory connected to your ESP8266 chip.<\/p>\n<p>The good news is that we can disable or enable this persistence by calling WiFi.persistent(). \u00a0Our code to enable the WiFi network will then look like this:<\/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;\">\/\/ Disable the WiFi persistence.  The ESP8266 will not load and save WiFi settings in the flash memory.<\/span>\n<span style=\"color: #ff6600;\"><strong>WiFi<\/strong><\/span>.persistent( <span style=\"color: #008080;\">false<\/span> );\n\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>Let&#8217;s see how this affects the power consumption:<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/05\/version4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-311\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/05\/version4-1024x506.png\" alt=\"\" width=\"840\" height=\"415\" \/><\/a><\/p>\n<p>The cycle has become longer, but on closer inspection of the graph and network packets the main reason for that is that DHCP took longer this time. \u00a0Unfortunately, the DHCP time is quite variable on my network, probably something I need to look into at some stage but not today. \u00a0(Ah, the noble art of procrastination&#8230;)<\/p>\n<p>What is missing this time, though, is the 1.2 seconds between AP association and the start of DHCP negotiation. \u00a0That&#8217;s 1200 ms at 71 mA,\u00a0or 0.023 mAh saved. \u00a0Combined with the 0.024 mAh saved so far, we&#8217;ve saved 0.047 mAh in total. \u00a0And we&#8217;ve most likely increased the life time of the flash chip as well.<\/p>\n<h3>Configuring static IP<\/h3>\n<p>As the DHCP negotiation is taking quite a while, the next step will be to disable DHCP and configure the WiFi connection statically instead.<\/p>\n<p>Of course, depending on your network setup this may not be a practical solution, but if your goal is to reduce the power consumption as far as we can go, it&#8217;s worth looking at this as well.<\/p>\n<p>Assuming that the sensor network operates with an IP range of 192.168.0.1 &#8211; 192.168.0.255, network mask 255.255.255.0 and 192.168.0.254 as a gateway, we can configure it as follows:<\/p>\n<pre><span style=\"color: #ff6600;\"><strong>IPAddress<\/strong><\/span> ip( 192, 168, 0, 1 );\n<span style=\"color: #ff6600;\"><strong>IPAddress<\/strong><\/span> gateway( 192, 168, 0, 254 );\n<span style=\"color: #ff6600;\"><strong>IPAddress<\/strong><\/span> subnet( 255, 255, 255, 0 );\n\n<span style=\"color: #ff6600;\"><strong>WiFi<\/strong><\/span>.forceSleepWake();\n<span style=\"color: #ff6600;\">delay<\/span>( 1 );\n<strong><span style=\"color: #ff6600;\">WiFi<\/span><\/strong>.persistent( <span style=\"color: #008080;\">false<\/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;\">config<\/span>( ip, gateway, subnet );\n<span style=\"color: #ff6600;\"><strong>WiFi<\/strong><\/span>.<span style=\"color: #ff6600;\">begin<\/span>( WLAN_SSID, WLAN_PASSWD );<\/pre>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/05\/version5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-312\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/05\/version5-1024x505.png\" alt=\"\" width=\"840\" height=\"414\" \/><\/a><\/p>\n<p>The time taken to negotiate a DHCP lease is now completely gone, and along with it the variability in the wake\/sleep cycle.<\/p>\n<p>The total power consumption is now down to 0.07 mAh\u00a0per cycle, less than half of the 0.164 mAh we started with before optimising.<\/p>\n<p>There may be other areas to target, so I will continue researching the impact different access points and interleaving operations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to part 3 of this series on reducing WiFi power consumption on ESP8266 chips. Earlier, I have established the baseline power consumption and shown how to reduce this a bit by disabling the radio when it is not needed. This time, I&#8217;ll take it a step further by showing how to make sure the &hellip; <a href=\"https:\/\/www.bakke.online\/index.php\/2017\/05\/22\/reducing-wifi-power-consumption-on-esp8266-part-3\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Reducing WiFi power consumption on ESP8266, part 3&#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-309","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\/309","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=309"}],"version-history":[{"count":1,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/309\/revisions"}],"predecessor-version":[{"id":1338,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/309\/revisions\/1338"}],"wp:attachment":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/media?parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/categories?post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/tags?post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}