Skip to content

Instantly share code, notes, and snippets.

@Bolukan
Created June 10, 2018 08:39
Show Gist options
  • Save Bolukan/2aab897844692192aac1f3d635949d65 to your computer and use it in GitHub Desktop.
Save Bolukan/2aab897844692192aac1f3d635949d65 to your computer and use it in GitHub Desktop.
All tricks to minimize energy for WiFi on ESP
/*
* Efficient WiFi
* https://www.bakke.online/index.php/2017/05/21/reducing-wifi-power-consumption-on-esp8266-part-2/
*
*/
/* WiFi settings */
#include "secrets.h" // WIFI SSID and password
#include <WiFi>
String MACdefaultAddress(void) {
uint8_t mac[6];
char macStr[18] = { 0 };
esp_err_t error;
error = esp_efuse_mac_get_default(mac);
if (error==ESP_OK)
{
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
return String(macStr);
}
void setup(void)
{
// WiFi
WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin(); // only works if WIFI_OFF
delay(1);
// Serial
Serial.begin(115200);
while (!Serial) { }
Serial.println("Booted");
Serial.println("Status: WiFi disabled");
}
void loop() {
// DO STUFF
// Wake WiFi
WiFi.forceSleepWake();
delay(1);
WiFi.persistent(false); // use WiFi.config()
WiFi.mode(WIFI_STA);
WiFi.config(WLAN_staticIP, WLAN_gateway, WLAN_subnet, WLAN_dns1, WLAN_dns2);
WiFi.begin(WLAN_SSID, WLAN_PASSWD);
while (WiFi.status() != WL_CONNECTED) {
delay(1);
}
Serial.println("WiFi connected");
// SEND STUFF
// Prepare for deep sleep
WiFi.disconnect(true);
delay(1);
WiFi.forceSleepBegin();
delay(1);
// WAKE_RF_DISABLED to keep the WiFi radio disabled when we wake up
ESP.deepSleep( 60000000, WAKE_RF_DISABLED ); // 1 minute
}
@Bolukan
Copy link
Author

Bolukan commented Jun 10, 2018

secrets.h

// WiFi
const char* WLAN_SSID = "SSID";
const char* WLAN_PASSWD = "PASSWD";

// Use Static IP to get faster connection, thus less energy consumption
IPAddress WLAN_staticIP ( 192, 168, 3, 200 );
IPAddress WLAN_gateway ( 192, 168, 3, 1 );
IPAddress WLAN_subnet ( 255, 255, 255, 0 );
IPAddress WLAN_dns1 ( 192, 168, 3, 1 );
IPAddress WLAN_dns2 ( 8, 8, 8, 8 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment