看到一篇好文,分享一下
Reducing the Power Consumption of Your ESP8266
We are now going to see how to lower the power consumption of your ESP8266 WiFi chip. To do that, we are going to use the deep sleep functions of the chip, that will simply sleep when no actions are required. As a simple example, we are going to log a simple dummy message to Dweet.io, which is a cloud service that is used to log data online. This will for example illustrate a data logger project that will only make measurements every 10 minutes for example, and sleep the rest of the time.
This is the complete code for this part:
// Library #include <ESP8266WiFi.h> // WiFi settings const char* ssid = "wifi-name"; const char* password = "wifi-password"; // Time to sleep (in seconds): const int sleepTimeS = 10; // Host const char* host = "dweet.io"; void setup() { // Serial Serial.begin(115200); Serial.println("ESP8266 in normal mode"); // Connect to WiFi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Print the IP address Serial.println(WiFi.localIP()); // Logging data to cloud Serial.print("Connecting to "); Serial.println(host); // Use WiFiClient class to create TCP connections WiFiClient client; const int httpPort = 80; if (!client.connect(host, httpPort)) { Serial.println("connection failed"); return; } // This will send the request to the server client.print(String("GET /dweet/for/myesp8266?message=lowpower") + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(10); // Read all the lines of the reply from server and print them to Serial while(client.available()){ String line = client.readStringUntil('\r'); Serial.print(line); } Serial.println(); Serial.println("closing connection"); // Sleep Serial.println("ESP8266 in sleep mode"); ESP.deepSleep(sleepTimeS * 1000000); } void loop() { }
This code is quite long, but let’s now focus on what we need for the deep sleep functions. First, we define how long we want the chip to stay in deep sleep mode. For test purposes, I set it to 10 seconds here:
const int sleepTimeS = 10;
Then, inside the setup() function of the sketch, after sending the request to Dweet.io we put the chip in deep sleep mode:
Serial.println("ESP8266 in sleep mode"); ESP.deepSleep(sleepTimeS * 1000000);
Note that here we need to put the whole code inside the setup() function of the sketch, as whenever the chip goes out of deep sleep mode, it starts again at the start of the setup() function.
You can get the whole code from the GitHub repository of the project:
https://github.com/openhomeautomation/esp8266-battery
It’s now time to test the project! First, remove the connection between DTR and XPD, so you can actually program the board. Also modify the WiFi credentials inside the code. Then, upload the code to the board, and connect the jumper cable again.
If you have a multimeter monitoring the current consumption of the ESP8266, this is what you first should see when the chip is booting:
This is the current that is used when the board is uploading data to Dweet.io, but it is also what the chip would use if we didn’t do any kind of optimisation for power. In that case, a 2500 mAh battery would last about 28.5 hours.
After a few seconds, the chip will enter deep sleep mode, and you should immediately see the power consumption going down:
As you can see, we already have a 10 times lower current consumption! At this rate, if we take the case of a data logger that just stays in sleep mode most of the time, the battery would now last 300 hours, or 12.5 days! It’s already a great improvement, but we can do much more with the SparkFun Thing.
Actually, most of this power is now used by … the power indicator LED on the board! This is great when you are developing applications on your desk, but not that useful when you are deploying your project in the field. Therefore, we are going to get rid of this LED here.
For newest versions of the SparkFun thing, you can simply unsolder the “PWR” jumper at the back of the board. For older versions like the one I have, you can simply cut the trace between the PWR LED and the nearby resistor. After that, just power the project again. The reading on the multimeter immediately changed to 77 uA, or 0.077 mA. This means that the same project will now last on the same battery for … 3.7 years! Of course, this doesn’t take into account the characteristics of the battery, so in reality you will end up with 1-2 years battery life for your project.
How to Go Further
In this article, we learned how to reduce the power consumption of the ESP8266 WiFi chip, so you can build projects that last for years on a single battery. Of course, this is an ideal situation, and it can’t be applied to all projects, and in reality probably the battery will be dead before the time I calculated in the article. However, this is a great solution for anybody interested in data logging projects where the device is spending most of the time doing nothing.
You can now use what you learned in the project, and build your own projects with it. You can simply use the code I used in this article, and just add a few lines to make measurements from sensors, and send those measurements to Dweet.io or another cloud platform of your choice.
Note that this article is a continuation of the very popular article I wrote before about How to Run an Arduino for Years on a Battery.