|
1 | 1 | #include <Arduino.h> |
2 | 2 | #include <ESPCpuMonitor.h> |
3 | | -#include <esp_log.h> |
4 | 3 | #include <cmath> |
| 4 | +#include <esp_log.h> |
5 | 5 |
|
6 | 6 | ESPCpuMonitor cpuMonitor; |
7 | 7 |
|
8 | 8 | void setup() { |
9 | | - Serial.begin(115200); |
| 9 | + Serial.begin(115200); |
10 | 10 |
|
11 | | - CpuMonitorConfig cfg; |
12 | | - cfg.sampleIntervalMs = 1000; // 1s sampling cadence |
13 | | - cfg.calibrationSamples = 5; // treat first 5 periods as 100% idle baseline |
14 | | - cfg.historySize = 20; |
15 | | - cfg.enableTemperature = true; // toggle temperature readings if your board supports it |
16 | | - cfg.smoothingMode = CpuSmoothingMode::Ewma; |
17 | | - cfg.smoothingAlpha = 0.25f; // smaller alpha => smoother trend |
18 | | - cpuMonitor.init(cfg); |
| 11 | + CpuMonitorConfig cfg; |
| 12 | + cfg.sampleIntervalMs = 1000; // 1s sampling cadence |
| 13 | + cfg.calibrationSamples = 5; // treat first 5 periods as 100% idle baseline |
| 14 | + cfg.historySize = 20; |
| 15 | + cfg.enableTemperature = true; // toggle temperature readings if your board supports it |
| 16 | + cfg.smoothingMode = CpuSmoothingMode::Ewma; |
| 17 | + cfg.smoothingAlpha = 0.25f; // smaller alpha => smoother trend |
| 18 | + cpuMonitor.init(cfg); |
19 | 19 |
|
20 | | - cpuMonitor.onSample([](const CpuUsageSample &sample) { |
| 20 | + cpuMonitor.onSample([](const CpuUsageSample &sample) { |
21 | 21 | #if portNUM_PROCESSORS > 1 |
22 | | - ESP_LOGI("CPU", "core0=%.1f%% core1=%.1f%% avg=%.1f%% smoothed=%.1f%% temp=%.1fC (avg %.1fC)", |
23 | | - sample.perCore[0], sample.perCore[1], sample.average, |
24 | | - std::isnan(sample.smoothedAverage) ? -1.0f : sample.smoothedAverage, |
25 | | - std::isnan(sample.temperatureC) ? -1.0f : sample.temperatureC, |
26 | | - std::isnan(sample.temperatureAvgC) ? -1.0f : sample.temperatureAvgC); |
| 22 | + ESP_LOGI( |
| 23 | + "CPU", |
| 24 | + "core0=%.1f%% core1=%.1f%% avg=%.1f%% smoothed=%.1f%% temp=%.1fC (avg %.1fC)", |
| 25 | + sample.perCore[0], |
| 26 | + sample.perCore[1], |
| 27 | + sample.average, |
| 28 | + std::isnan(sample.smoothedAverage) ? -1.0f : sample.smoothedAverage, |
| 29 | + std::isnan(sample.temperatureC) ? -1.0f : sample.temperatureC, |
| 30 | + std::isnan(sample.temperatureAvgC) ? -1.0f : sample.temperatureAvgC |
| 31 | + ); |
27 | 32 | #else |
28 | | - ESP_LOGI("CPU", "core0=%.1f%% avg=%.1f%% smoothed=%.1f%% temp=%.1fC (avg %.1fC)", |
29 | | - sample.perCore[0], sample.average, |
30 | | - std::isnan(sample.smoothedAverage) ? -1.0f : sample.smoothedAverage, |
31 | | - std::isnan(sample.temperatureC) ? -1.0f : sample.temperatureC, |
32 | | - std::isnan(sample.temperatureAvgC) ? -1.0f : sample.temperatureAvgC); |
| 33 | + ESP_LOGI( |
| 34 | + "CPU", |
| 35 | + "core0=%.1f%% avg=%.1f%% smoothed=%.1f%% temp=%.1fC (avg %.1fC)", |
| 36 | + sample.perCore[0], |
| 37 | + sample.average, |
| 38 | + std::isnan(sample.smoothedAverage) ? -1.0f : sample.smoothedAverage, |
| 39 | + std::isnan(sample.temperatureC) ? -1.0f : sample.temperatureC, |
| 40 | + std::isnan(sample.temperatureAvgC) ? -1.0f : sample.temperatureAvgC |
| 41 | + ); |
33 | 42 | #endif |
34 | | - }); |
| 43 | + }); |
35 | 44 | } |
36 | 45 |
|
37 | 46 | void loop() { |
38 | | - // Optional: pull the latest average every few seconds for a quick check |
39 | | - static uint32_t lastPrint = 0; |
40 | | - if (millis() - lastPrint > 3000) { |
41 | | - lastPrint = millis(); |
42 | | - float avg = cpuMonitor.getLastAverage(); |
43 | | - float smoothed = cpuMonitor.getLastSmoothedAverage(); |
44 | | - if (avg >= 0.0f) { |
45 | | - float temp = 0.0f; |
46 | | - float tempAvg = 0.0f; |
47 | | - if (cpuMonitor.getLastTemperature(temp, tempAvg)) { |
48 | | - Serial.printf("[loop] CPU avg: %.1f%% smoothed: %.1f%% temp: %.1fC (avg %.1fC)\n", |
49 | | - avg, smoothed, temp, tempAvg); |
50 | | - } else { |
51 | | - Serial.printf("[loop] CPU avg: %.1f%% smoothed: %.1f%% temp: n/a\n", avg, smoothed); |
52 | | - } |
53 | | - } else { |
54 | | - Serial.println("[loop] calibrating..."); |
55 | | - } |
56 | | - } |
57 | | - delay(250); |
| 47 | + // Optional: pull the latest average every few seconds for a quick check |
| 48 | + static uint32_t lastPrint = 0; |
| 49 | + if (millis() - lastPrint > 3000) { |
| 50 | + lastPrint = millis(); |
| 51 | + float avg = cpuMonitor.getLastAverage(); |
| 52 | + float smoothed = cpuMonitor.getLastSmoothedAverage(); |
| 53 | + if (avg >= 0.0f) { |
| 54 | + float temp = 0.0f; |
| 55 | + float tempAvg = 0.0f; |
| 56 | + if (cpuMonitor.getLastTemperature(temp, tempAvg)) { |
| 57 | + Serial.printf( |
| 58 | + "[loop] CPU avg: %.1f%% smoothed: %.1f%% temp: %.1fC (avg %.1fC)\n", |
| 59 | + avg, |
| 60 | + smoothed, |
| 61 | + temp, |
| 62 | + tempAvg |
| 63 | + ); |
| 64 | + } else { |
| 65 | + Serial.printf("[loop] CPU avg: %.1f%% smoothed: %.1f%% temp: n/a\n", avg, smoothed); |
| 66 | + } |
| 67 | + } else { |
| 68 | + Serial.println("[loop] calibrating..."); |
| 69 | + } |
| 70 | + } |
| 71 | + delay(250); |
58 | 72 | } |
0 commit comments