8000 add Low-Power demo by Tech-TX · Pull Request #6989 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

add Low-Power demo #6989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 69 commits into from
Feb 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
0c870ae
add Low-Power demo
Tech-TX Jan 6, 2020
fd1c366
add Low-Power demo
Tech-TX Jan 6, 2020
b912664
add Low-Power demo, remove OTA and add WiFi timeout
Tech-TX Jan 9, 2020
287bc2d
add Low-Power demo
Tech-TX Jan 6, 2020
7ea68d8
add Low-Power demo
Tech-TX Jan 6, 2020
f77a2c5
add Low-Power demo, remove OTA and add WiFi timeout
Tech-TX Jan 9, 2020
387e30d
add Low-Power demo, remove OTA and add WiFi timeout
Tech-TX Jan 9, 2020
0db9254
Merge branch 'master' into add_Low-Power_demo
Tech-TX Jan 9, 2020
3f3ab30
Merge branch 'add_Low-Power_demo' of https://github.com/Tech-TX/Ardui…
Tech-TX Jan 10, 2020
df75200
add Low-Power demo
Tech-TX Jan 10, 2020
ab30950
Merge branch 'master' into add_Low-Power_demo
Tech-TX Jan 10, 2020
11f1931
add Low-Power demo
Tech-TX Jan 10, 2020
7d6a03b
Merge branch 'add_Low-Power_demo' of https://github.com/Tech-TX/Ardui…
Tech-TX Jan 10, 2020
2003ef4
add Low-Power demo
Tech-TX Jan 10, 2020
acf3cec
add Low-Power demo
Tech-TX Jan 10, 2020
2f2c508
add Low-Power demo
Tech-TX Jan 11, 2020
47b73a2
add Low-Power demo
Tech-TX Jan 11, 2020
36a8dbb
add Low-Power demo
Tech-TX Jan 11, 2020
7178362
Merge branch 'master' into add_Low-Power_demo
Tech-TX Jan 11, 2020
5d16fca
add Low-Power demo
Tech-TX Jan 12, 2020
7a74da2
Merge branch 'add_Low-Power_demo' of https://github.com/Tech-TX/Ardui…
Tech-TX Jan 12, 2020
aa4feb2
add Low-Power demo
Tech-TX Jan 12, 2020
8f4448d
add Low-Power demo
Tech-TX Jan 12, 2020
b8e42e4
add Low-Power demo
Tech-TX Jan 12, 2020
068f4bd
add Low-Power demo
Tech-TX Jan 12, 2020
3118de5
add Low-Power demo
Tech-TX Jan 12, 2020
ac40b6e
add Low-Power demo
Tech-TX Jan 12, 2020
662b1f2
add Low-Power demo
Tech-TX Jan 13, 2020
5695626
add Low-Power demo
Tech-TX Jan 16, 2020
7627080
Merge branch 'master' into add_Low-Power_demo
Tech-TX Jan 17, 2020
7b0e515
add Low-Power demo
Tech-TX Jan 17, 2020
a41c381
Merge branch 'add_Low-Power_demo' of https://github.com/Tech-TX/Ardui…
Tech-TX Jan 17, 2020
b93c2e3
add Low-Power demo
Tech-TX Jan 17, 2020
f4d00c6
add Low-Power demo
Tech-TX Jan 18, 2020
841fa1b
add Low-Power demo
Tech-TX Jan 18, 2020
efe55a0
Merge branch 'master' into add_Low-Power_demo
Tech-TX Jan 18, 2020
ebc2b0a
add Low-Power demo
Tech-TX Jan 22, 2020
c9c073a
Merge branch 'add_Low-Power_demo' of https://github.com/Tech-TX/Ardui…
Tech-TX Jan 22, 2020
222b9e8
add Low-Power demo
Tech-TX Jan 25, 2020
9ef40f8
Merge branch 'master' into add_Low-Power_demo
Tech-TX Jan 25, 2020
3ffb8ec
add Low-Power demo
Tech-TX Jan 26, 2020
b902ed2
Merge branch 'add_Low-Power_demo' of https://github.com/Tech-TX/Ardui…
Tech-TX Jan 26, 2020
6ff566d
Merge branch 'master' into add_Low-Power_demo
Tech-TX Jan 26, 2020
c8a5955
add Low-Power demo
Tech-TX Jan 26, 2020
d9ea100
Merge branch 'add_Low-Power_demo' of https://github.com/Tech-TX/Ardui…
Tech-TX Jan 26, 2020
431f5dc
add Low-Power demo
Tech-TX Jan 28, 2020
9597a12
Merge branch 'master' into add_Low-Power_demo
Tech-TX Jan 28, 2020
7d3ee8d
add Low-Power demo
Tech-TX Jan 28, 2020
701634e
Merge branch 'add_Low-Power_demo' of https://github.com/Tech-TX/Ardui…
Tech-TX Jan 28, 2020
b618e6c
add Low-Power demo
Tech-TX Jan 29, 2020
3292f9b
add Low-Power demo
Tech-TX Jan 29, 2020
1e80279
8000 add Low-Power demo
Tech-TX Jan 29, 2020
93a527b
add Low-Power demo
Tech-TX Jan 29, 2020
6018d0b
add Low-Power demo
Tech-TX Jan 30, 2020
55d95e2
Merge branch 'master' into add_Low-Power_demo
Tech-TX Jan 30, 2020
b990e59
add Low-Power demo
Tech-TX Jan 31, 2020
c107c43
add Low-Power demo
Tech-TX Jan 31, 2020
fecacfd
add Low-Power demo
Tech-TX Jan 31, 2020
601a5ae
add Low-Power demo
Tech-TX Jan 31, 2020
a3ddde1
add Low-Power demo
Tech-TX Jan 31, 2020
535d483
add Low-Power demo
Tech-TX Jan 31, 2020
3775b1b
add Low-Power demo
Tech-TX Feb 1, 2020
56cb239
add Low-Power demo
Tech-TX Feb 1, 2020
c26ce4a
add Low-Power demo
Tech-TX Feb 1, 2020
a76ec12
add Low-Power demo
Tech-TX Feb 1, 2020
a79f48c
add Low-Power demo
Tech-TX Feb 1, 2020
ee4c701
add Low-Power demo
Tech-TX Feb 1, 2020
d378102
add Low-Power demo
Tech-TX Feb 1, 2020
df1a494
Merge branch 'master' into add_Low-Power_demo
Tech-TX Feb 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add Low-Power demo
  • Loading branch information
Tech-TX committed Jan 28, 2020
commit 431f5dc65a27033f2e2906eb188ce364680e2485
138 changes: 73 additions & 65 deletions libraries/esp8266/examples/LowPowerDemo/LowPowerDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include <PolledTimeout.h>
#include <include/WiFiState.h> // WiFiState structure details


//#define DEBUG // prints WiFi connection info to serial, uncomment if you want WiFi messages
#ifdef DEBUG
#define DEBUG_PRINTLN(x) Serial.println(x)
Expand Down Expand Up @@ -97,6 +96,7 @@ void wakeupCallback() { // unlike ISRs, you can do a print() from a callback fu
#ifdef testPoint
digitalWrite(testPoint, LOW); // testPoint tracks latency from WAKE_UP_PIN LOW to testPoint LOW
#endif
printMillis(); // show time difference across sleep
Serial.println(F("Woke from Forced Light Sleep - this is the callback"));
}

Expand All @@ -113,6 +113,7 @@ void setup() {
digitalWrite(LED, LOW); // turn on the LED
pinMode(WAKE_UP_PIN, INPUT_PULLUP); // polled to advance tests, interrupt for Forced Light Sleep
Serial.begin(115200);
Serial.println();
Serial.print(F("\nReset reason = "));
String resetCause = ESP.getResetReason();
Serial.println(resetCause);
Expand All @@ -130,9 +131,8 @@ void setup() {
nv->rtcData.rstCount = resetCount; // update the reset count & CRC
updateRTCcrc();

if (resetCount == 1) { // show that millis() is cleared across Deep Sleep reset
Serial.print(F("millis() = "));
Serial.println(millis());
if (resetCount == 1) { // show that millis() is cleared across the Deep Sleep reset
printMillis(); // show time difference across sleep
}
} // end of setup()

Expand All @@ -143,40 +143,37 @@ void loop() {
runTest3();
runTest4();
runTest5();
runTest6(); // first Deep Sleep test, all these end with a RESET
runTest6();
runTest7(); // first Deep Sleep test, all these end with a RESET
}
if (resetCount < 4) {
initWiFi(); // optional re-init of WiFi for the Deep Sleep tests
}
if (resetCount == 1) {
runTest7();
} else if (resetCount == 2) {
runTest8();
} else if (resetCount == 3) {
} else if (resetCount == 2) {
runTest9();
} else if (resetCount == 3) {
runTest10();
} else if (resetCount == 4) {
resetTests();
}
} //end of loop()

// 1st test - running with WiFi unconfigured, reads ~67 mA minimum
void runTest1() {
Serial.println(F("\n1st test - running with WiFi unconfigured"));
float volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
readVoltage(); // read internal VCC
Serial.println(F("press the switch to continue"));
waitPushbutton(false, blinkDelay);
}

// 2nd test - Automatic Modem Sleep 7 seconds after WiFi is connected (LED flashes)
void runTest2() {
Serial.println(F("\n2nd test - Automatic Modem Sleep"));
Serial.println(F("connecting WiFi, please wait until the LED blinks"));
initWiFi();
if (WiFi.localIP()) { // won't go into Automatic Sleep without an active WiFi connection
Serial.println(F("The amperage will drop in 7 seconds."));
float volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
readVoltage(); // read internal VCC
Serial.println(F("press the switch to continue"));
waitPushbutton(true, 90); /* This is using a special feature: below 100 mS blink delay,
the LED blink delay is padding 100 mS time with 'program cycles' to fill the 100 mS.
Expand All @@ -190,38 +187,36 @@ void runTest2() {
}
}

// 3rd test - Forced Modem Sleep
void runTest3() {
Serial.println(F("\n3rd test - Forced Modem Sleep"));
WiFi.mode(WIFI_SHUTDOWN, &nv->wss); // shut the modem down and save the WiFi state for faster reconnection
// WiFi.forceSleepBegin(delay_in_uS); // alternate method of Forced Modem Sleep if you want a timed shutdown
// delay(10); // it doesn't always go to sleep unless you delay(10); yield() wasn't reliable
float volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
readVoltage(); // read internal VCC
Serial.println(F("press the switch to continue"));
waitPushbutton(true, 99); /* Using the same < 100 mS feature. If you drop the delay below 100, you
will see the effect of program time vs. delay() time on minimum amperage. Above ~ 97 (97% of the
time in delay) there is little change in amperage, so you need to spend maximum time in delay()
to get minimum amperage. At a high percentage of delay() you will see minimum amperage. */
}

// 4th test - Automatic Light Sleep
void runTest4() {
Serial.println(F("\n4th test - Automatic Light Sleep"));
Serial.println(F("reconnecting WiFi with forceSleepWake (slow)"));
Serial.println(F("it will be in Automatic Light Sleep when WiFi connects (LED blinks)"));
Serial.println(F("reconnecting WiFi with forceSleepWake"));
Serial.println(F("Automatic Light Sleep begins 7 seconds after WiFi connects (LED blinks)"));
digitalWrite(LED, LOW); // visual cue that we're reconnecting
// if you resume here and then set sleep mode, it takes 7 seconds to go into Automatic Modem Sleep
WiFi.setSleepMode(WIFI_LIGHT_SLEEP, 3); // Automatic Light Sleep, DTIM listen interval = 3
// at higher beacon intervals you'll have a hard time establishing and maintaining a connection
// at higher DTIM intervals you'll have a hard time establishing and maintaining a connection
WiFi.forceSleepWake(); // reconnect with previous STA mode and connection settings
uint32_t wifiStart = millis();
while ((!WiFi.localIP()) && (millis() - wifiStart < wifiTimeout)) {
while (((!WiFi.localIP()) || (WiFi.status() != WL_CONNECTED)) && (millis() - wifiStart < wifiTimeout)) {
yield();
}
if (WiFi.localIP()) { // won't go into Automatic Sleep without an active WiFi connection
float volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
float reConn = (millis() - wifiStart);
Serial.print(F("WiFi connect time = "));
Serial.printf("%1.3f seconds\n", reConn / 1000);
readVoltage(); // read internal VCC
Serial.println(F("long press of the switch to continue"));
waitPushbutton(true, 350); /* Below 100 mS delay it only goes into 'Automatic Modem Sleep',
and below ~ 350 mS delay() the 'Automatic Light Sleep' is less frequent. Above 500 mS
Expand All @@ -231,9 +226,31 @@ void runTest4() {
}
}

// 5th test - Forced Light Sleep using Non-OS SDK calls
void runTest5() {
Serial.println(F("\n5th test - Forced Light Sleep using Non-OS SDK calls"));
Serial.println(F("\n5th test - Timed Light Sleep, wake in 10 seconds"));
Serial.println(F("Press the button when you are ready to proceed"));
waitPushbutton(true, blinkDelay);
WiFi.mode(WIFI_OFF); // you must turn the modem off; using disconnect won't work
delay(10);
digitalWrite(LED, HIGH); // turn the LED off so they know the CPU isn't running
readVoltage(); // read internal VCC
printMillis(); // show time difference across sleep, including Serial.flush();
#ifdef testPoint
digitalWrite(testPoint, HIGH);
// testPoint LOW in callback tracks delay from testPoint HIGH to testPoint LOW
#endif
extern os_timer_t *timer_list;
timer_list = nullptr; // stop (but don't disable) the 4 OS timers
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
wifi_fpm_set_wakeup_cb(wakeupCallback); // Set wakeup callback (optional)
wifi_fpm_open();
wifi_fpm_do_sleep(10E6); // only 0xFFFFFFF works; any other value and it won't sleep
delay(10e3 + 1); // delay needs to be 1 mS longer than sleep or it only goes into Modem Sleep
Serial.println(F("Woke up!")); // the interrupt callback hits before this is executed
}

void runTest6() {
Serial.println(F("\n6th test - Forced Light Sleep, wake with GPIO interrupt"));
Serial.flush();
WiFi.mode(WIFI_OFF); // you must turn the modem off; using disconnect won't work
delay(10);
Expand All @@ -242,34 +259,23 @@ void runTest5() {
digitalWrite(testPoint, HIGH);
// testPoint LOW in callback tracks latency from WAKE_UP_PIN LOW to testPoint LOW
#endif
float volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
readVoltage(); // read internal VCC
Serial.println(F("CPU going to sleep, pull WAKE_UP_PIN low to wake it (press the switch)"));
Serial.print(F("millis() = "));
Serial.println(millis());
Serial.flush(); // needs a delay(100) or Serial.flush() else it doesn't print the whole message
printMillis(); // show time difference across sleep
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
gpio_pin_wakeup_enable(GPIO_ID_PIN(WAKE_UP_PIN), GPIO_PIN_INTR_LOLEVEL);
// only LOLEVEL or HILEVEL interrupts work, no edge, that's an SDK or CPU limitation
wifi_fpm_set_wakeup_cb(wakeupCallback); // Set wakeup callback (optional)
wifi_fpm_open();
wifi_fpm_do_sleep(0xFFFFFFF); // only 0xFFFFFFF works; any other value and it won't sleep
delay(10); // it goes to sleep some time during this delay() and waits for an interrupt
Serial.print(F("millis() = ")); // show that CPU / millis() is stopped in Forced Light Sleep
Serial.println(millis()); // although the CPU may run for up to 1 second before fully stopping
Serial.println(F("Woke up!")); // the interrupt callback hits before this is executed
/* Note: the Non-OS SDK API Reference says that you can do a timed Light Sleep, but due to
a bug any time delay greater than 13E3 uS will time-out and not execute the callback.
The example code in the Reference doesn't work, and the chip only goes into Modem Sleep,
not the lower-amperage Light Sleep. The only working alternative is a timed Modem Sleep. */
Serial.println(F("Woke up!")); // the interrupt callback hits before this is executed*/
}

// 6th test - Deep Sleep for 10 seconds, wake with RF_DEFAULT
void runTest6() {
Serial.println(F("\n6th test - Deep Sleep for 10 seconds, reset and wake with RF_DEFAULT"));
void runTest7() {
Serial.println(F("\n7th test - Deep Sleep for 10 seconds, reset and wake with RF_DEFAULT"));
initWiFi(); // initialize WiFi since we turned it off in the last test
float volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
readVoltage(); // read internal VCC
Serial.println(F("press the switch to continue"));
while (!digitalRead(WAKE_UP_PIN)) { // wait for them to release the switch from the previous test
delay(10);
Expand All @@ -280,9 +286,7 @@ void runTest6() {
//WiFi.mode(WIFI_SHUTDOWN, &nv->wss); // Forced Modem Sleep for a more Instant Deep Sleep,
// and no extended RFCAL as it goes into Deep Sleep
Serial.println(F("going into Deep Sleep now..."));
Serial.print(F("millis() = "));
Serial.println(millis());
Serial.flush(); // needs a delay(10) or Serial.flush() else it doesn't print the whole message
printMillis(); // show time difference across sleep
#ifdef testPoint
digitalWrite(testPoint, HIGH); // testPoint set HIGH to track Deep Sleep period, cleared at startup()
#endif
Expand All @@ -294,11 +298,9 @@ void runTest6() {
Serial.println(F("What... I'm not asleep?!?")); // it will never get here
}

// 7th test - Deep Sleep for 10 seconds, wake with RFCAL
void runTest7() {
Serial.println(F("\n7th test - in RF_DEFAULT, Deep Sleep for 10 seconds, reset and wake with RFCAL"));
float volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
void runTest8() {
Serial.println(F("\n8th test - in RF_DEFAULT, Deep Sleep for 10 seconds, reset and wake with RFCAL"));
readVoltage(); // read internal VCC
Serial.println(F("press the switch to continue"));
waitPushbutton(false, blinkDelay); // set true if you want to see Automatic Modem Sleep
//WiFi.mode(WIFI_SHUTDOWN, &nv->wss); // Forced Modem Sleep for a more Instant Deep Sleep,
Expand All @@ -312,11 +314,9 @@ void runTest7() {
Serial.println(F("What... I'm not asleep?!?")); // it will never get here
}

// 8th test - Deep Sleep Instant for 10 seconds, wake with NO_RFCAL
void runTest8() {
Serial.println(F("\n8th test - in RFCAL, Deep Sleep Instant for 10 seconds, reset and wake with NO_RFCAL"));
float volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
void runTest9() {
Serial.println(F("\n9th test - in RFCAL, Deep Sleep Instant for 10 seconds, reset and wake with NO_RFCAL"));
readVoltage(); // read internal VCC
Serial.println(F("press the switch to continue"));
waitPushbutton(false, blinkDelay); // set true if you want to see Automatic Modem Sleep
WiFi.mode(WIFI_SHUTDOWN, &nv->wss); // Forced Modem Sleep for a more Instant Deep Sleep
Expand All @@ -329,11 +329,9 @@ void runTest8() {
Serial.println(F("What... I'm not asleep?!?")); // it will never get here
}

// 9th test - Deep Sleep Instant for 10 seconds, wake with RF_DISABLED
void runTest9() {
Serial.println(F("\n9th test - in NO_RFCAL, Deep Sleep Instant for 10 seconds, reset and wake with RF_DISABLED"));
float volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
void runTest10() {
Serial.println(F("\n10th test - in NO_RFCAL, Deep Sleep Instant for 10 seconds, reset and wake with RF_DISABLED"));
readVoltage(); // read internal VCC
Serial.println(F("press the switch to continue"));
waitPushbutton(false, blinkDelay); // set true if you want to see Automatic Modem Sleep
//WiFi.mode(WIFI_SHUTDOWN); // Forced Modem Sleep for a more Instant Deep Sleep
Expand All @@ -347,10 +345,9 @@ void runTest9() {
}

void resetTests() {
float volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
readVoltage(); // read internal VCC
Serial.println(F("\nTests completed, in RF_DISABLED, press the switch to do an ESP.restart()"));
//memset(&nv->wss, 0, sizeof(nv->wss) * 2); // uncomment if you want to wipe the saved WiFi states
//memset(&nv->wss, 0, sizeof(nv->wss) * 2); // uncomment if you erase to wipe the saved WiFi states
waitPushbutton(false, 1000);
ESP.restart();
}
Expand Down Expand Up @@ -383,6 +380,17 @@ void waitPushbutton(bool usesDelay, unsigned int delayTime) { // loop until the
delay(50); // debounce time for the switch, pushbutton released
}

void readVoltage() { // read internal VCC
float volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
}

void printMillis() {
Serial.print(F("millis() = "));
Serial.println(millis());
Serial.flush(); // needs a Serial.flush() else it may not print the whole message before sleeping
}

void updateRTCcrc() { // updates the reset count CRC
nv->rtcData.crc32 = crc32((uint8_t*) &nv->rtcData.rstCount, sizeof(nv->rtcData.rstCount));
}
Expand Down
Loading
0