Skip to content

Commit 3277229

Browse files
committed
feat(wifi): Add WiFi automatic LightSleep example
1 parent 1511552 commit 3277229

File tree

4 files changed

+142
-2
lines changed

4 files changed

+142
-2
lines changed

cores/esp32/esp32-hal-cpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ uint32_t getXtalFrequencyMhz(); // In MHz
4242
uint32_t getApbFrequency(); // In Hz
4343

4444
/**
45-
* @brief Set automatic light sleep state. CPU will fo into light sleep if no ongoing activity (active task, peripheral activity etc.)
45+
* @brief Set automatic light sleep state. CPU will go into light sleep if no ongoing activity (active task, peripheral activity etc.)
4646
* @param enabled true to enable automatic lightSleep
4747
* @return
4848
* - ESP_OK on success
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Cpu automatic LightSleep
3+
=====================================
4+
This code displays how to use automatic light sleep
5+
and demonstrates a difference between busywait and
6+
sleep-aware wait using delay().
7+
Another examples of a 'sleep-friendly' blocking might be
8+
FreeRTOS mutexes/semaphores, queues.
9+
10+
This code is under Public Domain License.
11+
12+
Hardware Connections (optional)
13+
======================
14+
Use an ammeter/scope connected in series with a CPU/DevKit board to measure power consumption
15+
16+
Author:
17+
Taras Shcherban <[email protected]>
18+
*/
19+
20+
#include "Arduino.h"
21+
22+
void setup()
23+
{
24+
Serial.begin(115200);
25+
while (!Serial)
26+
; // wait for serial port to connect
27+
28+
// CPU will automatically go into light sleep if no ongoing activity (active task, peripheral activity etc.)
29+
setAutomaticLightSleep(true);
30+
}
31+
32+
void loop()
33+
{
34+
Serial.printf("Time since boot: %ld ms; going to block for a 5 seconds...\n", millis());
35+
36+
// Serial output is being suspended during sleeping, so without a Flush call logs
37+
// will be printed to the terminal with a delay depending on how much CPU spends in a sleep state
38+
Serial.flush();
39+
40+
// This is a sleep-aware waiting using delay(). Blocking in this manner
41+
// allows CPU to go into light sleep mode until there is some task to do.
42+
// if you remove that delay completely - CPU will have to call loop() function constantly,
43+
// so no power saving will be available
44+
delay(5000);
45+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
Cpu automatic LightSleep
3+
=====================================
4+
This code displays how to use automatic light sleep with an active WiFi connection
5+
and tune WiFi modem sleep modes
6+
7+
This code is under Public Domain License.
8+
9+
Hardware Connections (optional)
10+
======================
11+
Use an ammeter/scope connected in series with a CPU/DevKit board to measure power consumption
12+
13+
Author:
14+
Taras Shcherban <[email protected]>
15+
*/
16+
17+
#include "Arduino.h"
18+
19+
#include <WiFi.h>
20+
#include <HTTPClient.h>
21+
22+
const char *wifi_ssid = "kriegsmaschine";
23+
const char *wifi_password = "ap3NhxtcmszTdok36ijka";
24+
25+
void doHttpRequest();
26+
27+
void setup()
28+
{
29+
Serial.begin(115200);
30+
while (!Serial)
31+
; // wait for serial port to connect
32+
33+
// CPU will automatically go into light sleep if no ongoing activity (active task, peripheral activity etc.)
34+
setAutomaticLightSleep(true);
35+
36+
WiFi.begin(wifi_ssid, wifi_password);
37+
38+
// Additionally to automatic CPU sleep a modem can also be setup for a power saving.
39+
// If a WiFi is active - selected modem sleep mode will determine how much CPU will be sleeping.
40+
// There are two functions available:setSleep(mode) and setSleep(mode, listenInterval)
41+
// mode - supports one of three values:
42+
// * WIFI_PS_NONE - No power save
43+
// * WIFI_PS_MIN_MODEM - Minimum modem power saving. In this mode, station wakes up to receive beacon every DTIM period
44+
// * WIFI_PS_MAX_MODEM - Maximum modem power saving. In this mode, interval to receive beacons is determined by the listenInterval parameter
45+
// listenInterval - effective only with a WIFI_PS_MAX_MODEM mode. determines how often will modem (and consequently CPU) wakeup to catch WiFi beacon packets.
46+
// The larger the interval - the less power needed for WiFi connection support. However that comes at a cost of increased networking latency, i.e.
47+
// If your device responds to some external requests (web-server, ping etc.) - larger listenInterval means device takes more to respond.
48+
// Reasonable range is 2..9, going larger would not benefit too much in terms of power consumption. Too large value might result in WiFi connection drop.
49+
// listenInterval is measured in DTIM intervals, which is determined by your access point (typically ~100ms).
50+
WiFi.setSleep(WIFI_PS_MAX_MODEM, 4);
51+
}
52+
53+
void loop()
54+
{
55+
doHttpRequest();
56+
57+
// Serial output is being suspended during sleeping, so without a Flush call logs
58+
// will be printed to the terminal with a delay depending on how much CPU spends in a sleep state
59+
Serial.flush();
60+
61+
// This is a sleep-aware waiting using delay(). Blocking in this manner
62+
// allows CPU to go into light sleep mode until there is some task to do.
63+
// if you remove that delay completely - CPU will have to call loop() function constantly,
64+
// so no power saving will be available
65+
delay(5000);
66+
}
67+
68+
// simulate some job - make an HTTP GET request
69+
void doHttpRequest()
70+
{
71+
if (!WiFi.isConnected())
72+
return;
73+
74+
HTTPClient http;
75+
76+
Serial.print("[HTTP] request...\n");
77+
http.begin("http://example.com/index.html");
78+
79+
int httpCode = http.GET();
80+
if (httpCode > 0)
81+
{
82+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
83+
84+
if (httpCode == HTTP_CODE_OK)
85+
{
86+
Serial.printf("[HTTP] GET... payload size: %d\n", http.getSize());
87+
}
88+
}
89+
else
90+
{
91+
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
92+
}
93+
94+
http.end();
95+
}

libraries/WiFi/src/WiFiGeneric.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class WiFiGenericClass
186186
/**
187187
* @brief Set modem sleep state
188188
* @param sleepType Modem sleep type, one of WIFI_PS_NONE, WIFI_PS_MIN_MODEM, WIFI_PS_MAX_MODEM
189-
* @param listenInterval Listen interval for ESP32 station to receive beacon when WIFI_PS_MAX_MODEM is set. Units: AP beacon intervals. Defaults to 3 if set to 0.
189+
* @param listenInterval Listen interval for ESP32 station to receive beacon when WIFI_PS_MAX_MODEM is set. Units: AP beacon intervals. Defaults to 3 if set to 0.
190190
* @return
191191
* - true on success
192192
* - false on internal error when parameters combination is not valid

0 commit comments

Comments
 (0)