Here is the approaches to minimizing the power consumption of your ESP8266/ESP32 by coding to extend the battery life of your peripheral devices. The key aspect of this mode is that it requires a little overhead to enter and exit unto sleeping mode of the chip. There is another ways to have a better power saving techniques by using interrupts signal you can just invoke the idle mode to reduce the power consumption to the peripherals, however when external interrupt is used it allows you to receive data from the peripherals without invoking and without polling. Interrupt occurs the ESP8266 will automatically start executing the code changing the value of the digital input pin. The code which demonstrated on this article was adopted from open house automations using Arduino Core Library. For wiring and bootloading ESP8266 / ESP32 please refer to link.
Required Firmware
The deepSleep Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
#include <ESP8266WiFi.h> // WiFi settings const char* ssid = "14CORE"; //Your Wifi SSID const char* password = "0123456789"; //Your wifi password // Time to sleep (in seconds): const int sleepTimeS = 10; // Host const char* host = "CORE"; //Host void setup() { // Serial Serial.begin(115200); Serial.println("ESP Normal "); // Connect to WiFi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("..........."); Serial.println("WiFi connected"); Serial.println(WiFi.localIP()); // Print the IP address Serial.print("Connecting to > "); // Logging data to cloud Serial.println(host); WiFiClient client; // Use WiFiClient class to create TCP connections 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 /stringfor/esp?message=lowpower") + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(10); while(client.available()){ String line = client.readStringUntil('\r'); // Read all the lines of the reply from server and print them to Serial Serial.print(line); } Serial.println(); Serial.println("closing connection"); // Sleep Serial.println("ESP8266 in sleep mode"); ESP.deepSleep(sleepTimeS * 1000000); // deepSleep } void loop() { } |
Interrupt Example Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
const byte interruptPin = 13; volatile byte interruptCounter = 0; int numberOfInterrupts = 0; void setup() { Serial.begin(115200); pinMode(interruptPin, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING); } void handleInterrupt() { interruptCounter++; } void loop() { if(interruptCounter>0){ interruptCounter--; numberOfInterrupts++; Serial.print("Interrupt has occurred. Total: "); Serial.println(numberOfInterrupts); } } |