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
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
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 10, 2020
commit 11f193143fc40e1bf4ee013db88fa7f7f5ede8d3
95 changes: 45 additions & 50 deletions libraries/esp8266/examples/LowPowerDemo/LowPowerDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@

WiFi connections will be made over twice as fast if you can use a static IP address.

This example code is in the public domain, and was inspired by code from numerous sources */
This example is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This example is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this example; if not, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */

#include <ESP8266WiFi.h>
#include <coredecls.h> // crc32()
Expand Down Expand Up @@ -54,10 +66,6 @@ IPAddress subnet(0, 0, 0, 0);
IPAddress dns1(0, 0, 0, 0);
IPAddress dns2(0, 0, 0, 0);
uint32_t wifiTimeout = 30E3; // 30 second timeout on the WiFi connection
bool wifiOK; // used to skip tests if WiFi never connected

// CRC function used to ensure data validity of RTC User Memory
uint32_t calculateCRC32(const uint8_t *data, size_t length);

// This structure will be stored in RTC memory to remember the reset count (number of Deep Sleeps).
// First field is CRC32, which is calculated based on the rest of the structure contents.
Expand Down Expand Up @@ -95,7 +103,7 @@ void setup() {

// Read struct from RTC memory
if (ESP.rtcUserMemoryRead(64, (uint32_t*) &rtcData, sizeof(rtcData))) {
uint32_t crcOfData = calculateCRC32((uint8_t*) &rtcData.data[0], sizeof(rtcData.data));
uint32_t crcOfData = crc32((uint8_t*) &rtcData.data[0], sizeof(rtcData.data));
if (crcOfData != rtcData.crc32) { // if the CRC is invalid
resetCount = 0; // set first test loop since power on or external reset
} else {
Expand All @@ -109,49 +117,51 @@ void loop() {
// 1st test - running with WiFi unconfigured, reads ~67 mA minimum
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 );
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
Serial.println(F("press the button to continue"));
waitPushbutton(false, blinkDelay);

// 2nd test - Automatic Modem Sleep 7 seconds after WiFi is connected (LED flashes)
Serial.println(F("\n2nd test - Automatic Modem Sleep"));
Serial.println(F("connecting WiFi, please wait until the LED blinks"));
init_WiFi();
if (WiFi.localIP()) {
if (WiFi.localIP()) { // won't go into Automatic Sleep without an active WiFi connection
Serial.println(F("The current will drop in 7 seconds."));
volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000 );
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
Serial.println(F("press the button to continue"));
waitPushbutton(true, longDelay);
} else {
Serial.println(F("test skipped"));
Serial.println(F("no WiFi connection, test skipped"));
}

// 3rd test - Forced Modem Sleep
Serial.println(F("\n3rd test - Forced Modem Sleep"));
WiFi.forceSleepBegin();
delay(10); // it doesn't always go to sleep unless you delay(10)
volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000 );
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
Serial.println(F("press the button to continue"));
waitPushbutton(false, blinkDelay);

// 4th test - Automatic Light Sleep
Serial.println(F("\n4th test - Automatic Light Sleep"));
if (WiFi.localIP()) {
Serial.println(F("reconnecting WiFi"));
Serial.println(F("it will be in Automatic Light Sleep once WiFi connects (LED blinks)"));
digitalWrite(LED, LOW); // visual cue that we're reconnecting
WiFi.setSleepMode(WIFI_LIGHT_SLEEP, 5); // Automatic Light Sleep
WiFi.forceSleepWake(); // reconnect with previous STA mode and connection settings
while (!WiFi.localIP())
delay(50);
volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000 );
Serial.println(F("press the button to continue"));
waitPushbutton(true, longDelay);
uint32_t wifiStart = millis();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use polledTimeout here to not reinvent the wheel, like so:

using oneShot=esp8266::polledTimeout::oneShot; //shorthand for the oneShot type (this is the same as a typedef)
oneShot timeout(wifiTimeout);
while(!WiFi.localIP && !timeout) {
  delay(50);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even simpler: I replaced various delay()s in the WiFi inits with yield().

while ((!WiFi.localIP() ) && ( millis() - wifiStart < wifiTimeout )) {
delay(50);
}
if (WiFi.localIP()) { // won't go into Automatic Sleep without an active WiFi connection
volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
Serial.println(F("press the button to continue"));
waitPushbutton(true, longDelay);
} else {
Serial.println(F("test skipped"));
Serial.println(F("no WiFi connection, test skipped"));
}

// 5th test - Forced Light Sleep using Non-OS SDK calls
Expand All @@ -160,7 +170,7 @@ if (WiFi.localIP()) {
yield();
digitalWrite(LED, HIGH); // turn the LED off so they know the CPU isn't running
volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000 );
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
Serial.println(F("CPU going to sleep, pull WAKE_UP_PIN low to wake it (press the button)"));
delay(100); // needs a brief delay after the print or it may print the whole message
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
Expand All @@ -178,10 +188,11 @@ if (WiFi.localIP()) {
resetCount = 1; // advance to the next Deep Sleep test after the reset
updateRTC(); // save the current test state in RTC memory
volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000 );
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
Serial.println(F("press the button to continue"));
while (!digitalRead(WAKE_UP_PIN)) // wait for them to release the button from the last test
while (!digitalRead(WAKE_UP_PIN)) { // wait for them to release the button from the last test
delay(10);
}
delay(50); // debounce time for the switch, button released
waitPushbutton(false, blinkDelay);
digitalWrite(LED, LOW); // turn the LED on, at least briefly
Expand All @@ -205,7 +216,7 @@ if (WiFi.localIP()) {
updateRTC(); // save the current test state in RTC memory
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 );
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
Serial.println(F("press the button to continue"));
waitPushbutton(false, blinkDelay);
Serial.println(F("going into Deep Sleep now..."));
Expand All @@ -221,7 +232,7 @@ if (WiFi.localIP()) {
updateRTC(); // save the current test state in RTC memory
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 );
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
Serial.println(F("press the button to continue"));
waitPushbutton(false, blinkDelay);
Serial.println(F("going into Deep Sleep now..."));
Expand All @@ -237,7 +248,7 @@ if (WiFi.localIP()) {
updateRTC(); // save the current test state in RTC memory
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 );
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
Serial.println(F("press the button to continue"));
waitPushbutton(false, blinkDelay);
Serial.println(F("going into Deep Sleep now..."));
Expand All @@ -248,10 +259,10 @@ if (WiFi.localIP()) {
}

if (resetCount == 4) {
resetCount = 5; // start all over
resetCount = 5; // start all over, do an ESP.restart to insure it's a clean state
updateRTC(); // save the current test state in RTC memory
float volts = ESP.getVcc();
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000 );
Serial.printf("The internal VCC reads %1.3f volts\n", volts / 1000);
Serial.println(F("\nTests completed, in RF_DISABLED, press the button to do an ESP.restart()"));
waitPushbutton(false, 1000);
ESP.restart();
Expand All @@ -260,7 +271,7 @@ if (WiFi.localIP()) {

void waitPushbutton(bool usesDelay, unsigned int delayTime) { // loop until they press the button
// note: 2 different modes, as both of the AUTOMATIC power saving modes need a long delay()
if (!usesDelay) { // quick interception of button press, no delay()
if (!usesDelay) { // quick interception of button press, no delay() used
blinkLED.reset(delayTime);
while (digitalRead(WAKE_UP_PIN)) { // wait for a button press
if (blinkLED) {
Expand All @@ -275,35 +286,19 @@ void waitPushbutton(bool usesDelay, unsigned int delayTime) { // loop until the
}
}
delay(50); // debounce time for the switch, button pressed
while (!digitalRead(WAKE_UP_PIN)) // now wait for them to release the button
while (!digitalRead(WAKE_UP_PIN)) { // now wait for them to release the button
delay(10);
delay(50); // debounce time for the switch, button released
}

uint32_t calculateCRC32(const uint8_t *data, size_t length) {
uint32_t crc = 0xffffffff;
while (length--) {
uint8_t c = *data++;
for (uint32_t i = 0x80; i > 0; i >>= 1) {
bool bit = crc & 0x80000000;
if (c & i) {
bit = !bit;
}
crc <<= 1;
if (bit) {
crc ^= 0x04c11db7;
}
}
}
return crc;
delay(50); // debounce time for the switch, button released
}

void updateRTC() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would get replaced by the RTCData::writeToRTC() method, declared and defined within the class at the start of the sketch.

rtcData.data[3] = resetCount; // save the loop count for the next reset
// Update CRC32 of data
rtcData.crc32 = calculateCRC32((uint8_t*) &rtcData.data[0], sizeof(rtcData.data));
if (resetCount == 5) // wipe the CRC in RTC memory when we're done with all tests
rtcData.crc32 = crc32((uint8_t*) &rtcData.data[0], sizeof(rtcData.data));
if (resetCount == 5) { // wipe the CRC in RTC memory when we're done with all tests
rtcData.crc32 = 0;
}
// Write struct to RTC memory
ESP.rtcUserMemoryWrite(64, (uint32_t*) &rtcData, sizeof(rtcData));
}
Expand All @@ -322,7 +317,7 @@ void init_WiFi() {
DEBUG_PRINT(F("my MAC: "));
DEBUG_PRINTLN(WiFi.macAddress());
uint32_t wifiStart = millis();
while (( WiFi.status() != WL_CONNECTED ) && ( millis() - wifiStart < wifiTimeout )) {
while ((WiFi.status() != WL_CONNECTED) && (millis() - wifiStart < wifiTimeout)) {
delay(50);
}
if (WiFi.status() == WL_CONNECTED) {
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy