Skip to content

Instantly share code, notes, and snippets.

@shirish47
Last active September 16, 2016 01:15
Show Gist options
  • Save shirish47/e8f8b1d0d9d76c5249b0 to your computer and use it in GitHub Desktop.
Save shirish47/e8f8b1d0d9d76c5249b0 to your computer and use it in GitHub Desktop.
ESP8266 Sleep function testing for checking power consumption
/*Author:??
found at ESP8266/arduino
*/
#include <ESP8266WiFi.h>
extern "C" {
#include "user_interface.h"
}
const char* ssid = "UNICORN";
const char* password = "Un1c()rn";
void setup() {
Serial.begin(115200);
Serial.println();
}
void loop() {
initWifi();
Serial.println("Light sleep:");
wifi_set_sleep_type(LIGHT_SLEEP_T);
doDelays();
Serial.println("None sleep:");
wifi_set_sleep_type(NONE_SLEEP_T);
doDelays();
WiFi.disconnect();
Serial.print("WiFi disconnected, IP address: "); Serial.println(WiFi.localIP());
Serial.println("Light sleep:");
wifi_set_sleep_type(LIGHT_SLEEP_T);
doDelays();
}
void doDelays() {
Serial.println("Yield for 20 sec");
long endMs = millis() + 20000;
while (millis() < endMs) {
yield();
}
Serial.println("Delay for 20 sec");
delay(20000);
}
void initWifi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while ((WiFi.status() != WL_CONNECTED)) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected, IP address: "); Serial.println(WiFi.localIP());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment