|
| 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 | +} |
0 commit comments