Skip to content

Commit 6de498d

Browse files
committed
More examples
1 parent 5d7bdde commit 6de498d

6 files changed

Lines changed: 131 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
6-
- No changes yet.
6+
### Added
7+
- `examples/manual_sampling` shows manual `sampleNow()` usage without the esp_timer while keeping calibration/history stable.
8+
- `examples/json_export` streams newline-delimited JSON via `toJson()` + ArduinoJson for telemetry pipelines.
79

810
## [1.0.1] - 2025-12-03
911
### Fixed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ ESPCpuMonitor is a tiny C++17 helper that estimates per-core CPU usage on ESP32
1616
- Optional ArduinoJson export helper to stream samples over HTTP/MQTT/WebSockets alongside the rest of ESPToolKit.
1717

1818
## Examples
19+
- `examples/basic_monitor` – timer-driven sampling with per-core logs and optional temperature readings.
20+
- `examples/manual_sampling` – drives `sampleNow()` manually (no esp_timer), shows how to log history depth and keep calibration/averaging stable during bursty work.
21+
- `examples/json_export` – streams newline-delimited JSON via `toJson()` + ArduinoJson for dashboards/MQTT/serial telemetry.
22+
1923
Basic Arduino sketch that prints CPU usage once calibrated:
2024

2125
```cpp
@@ -89,7 +93,7 @@ If temperature is enabled, `getLastTemperature(current, average)` returns the la
8993
- C++17 required; tested with dual-core chips but handles `portNUM_PROCESSORS == 1`.
9094

9195
## Tests
92-
- Build and run `examples/basic_monitor` via PlatformIO CI or Arduino CLI for a quick smoke test on ESP32 dev boards.
96+
- Build and run `examples/basic_monitor`, `examples/manual_sampling`, and `examples/json_export` via PlatformIO CI or Arduino CLI for a quick smoke test on ESP32 dev boards.
9397

9498
## License
9599
MIT — see [LICENSE.md](LICENSE.md).
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <Arduino.h>
2+
#include <ESPCpuMonitor.h>
3+
4+
#if !ESPCM_HAS_ARDUINOJSON
5+
#error "Install ArduinoJson to run this example."
6+
#endif
7+
8+
#include <ArduinoJson.h>
9+
#include <cmath>
10+
11+
static const uint32_t HISTORY_LOG_PERIOD_MS = 5000;
12+
13+
void setup() {
14+
Serial.begin(115200);
15+
16+
CpuMonitorConfig cfg;
17+
cfg.sampleIntervalMs = 1000; // 1s cadence for telemetry
18+
cfg.historySize = 12; // keep a handful of points for charts/debug
19+
cfg.enableTemperature = true;
20+
cpuMonitor.init(cfg);
21+
22+
cpuMonitor.onSample([](const CpuUsageSample &sample) {
23+
StaticJsonDocument<256> doc;
24+
cpuMonitor.toJson(sample, doc);
25+
doc["avgRounded"] = static_cast<int>(roundf(sample.average)); // handy for dashboards
26+
27+
serializeJson(doc, Serial);
28+
Serial.println(); // newline-delimited JSON (NDJSON) stream
29+
});
30+
}
31+
32+
void loop() {
33+
static uint32_t lastHistoryMs = 0;
34+
35+
if (cpuMonitor.isReady() && millis() - lastHistoryMs >= HISTORY_LOG_PERIOD_MS) {
36+
lastHistoryMs = millis();
37+
const auto hist = cpuMonitor.history();
38+
if (!hist.empty()) {
39+
const auto &latest = hist.back();
40+
Serial.printf("[json_export] history_depth=%u last_avg=%.1f%% last_temp=%.1fC\n",
41+
static_cast<unsigned>(hist.size()),
42+
latest.average,
43+
std::isnan(latest.temperatureC) ? -1.0f : latest.temperatureC);
44+
}
45+
}
46+
47+
delay(50);
48+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[env:esp32dev]
2+
platform = espressif32
3+
board = esp32dev
4+
framework = arduino
5+
monitor_speed = 115200
6+
7+
lib_deps =
8+
ArduinoJson @ ^7
9+
ESPCpuMonitor
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <Arduino.h>
2+
#include <ESPCpuMonitor.h>
3+
#include <esp_log.h>
4+
5+
static const uint32_t SAMPLE_PERIOD_MS = 500;
6+
7+
// Simple spin to create a small CPU bump for the manual sampler.
8+
static void spinWork(uint32_t durationMicros) {
9+
const uint32_t start = micros();
10+
while ((uint32_t)(micros() - start) < durationMicros) {
11+
asm volatile("nop");
12+
}
13+
}
14+
15+
void setup() {
16+
Serial.begin(115200);
17+
18+
CpuMonitorConfig cfg;
19+
cfg.sampleIntervalMs = 0; // manual sampling only
20+
cfg.calibrationSamples = 8; // give the idle baseline a few readings
21+
cfg.historySize = 10;
22+
cfg.enablePerCore = false; // focus on averaged load
23+
cfg.enableTemperature = false; // keep it portable on targets without temp sensor
24+
cpuMonitor.init(cfg);
25+
26+
cpuMonitor.onSample([](const CpuUsageSample &sample) {
27+
ESP_LOGI("CPU", "avg=%.1f%% ts=%llu",
28+
sample.average,
29+
static_cast<unsigned long long>(sample.timestampUs));
30+
});
31+
}
32+
33+
void loop() {
34+
static uint32_t lastSampleMs = 0;
35+
36+
// Simulate bursty work after calibration: spin for ~5ms while in a "busy" window.
37+
const bool busyWindow = cpuMonitor.isReady() && (millis() / 3000) % 2 == 0;
38+
if (busyWindow) {
39+
spinWork(5000);
40+
}
41+
42+
if (millis() - lastSampleMs >= SAMPLE_PERIOD_MS) {
43+
lastSampleMs = millis();
44+
45+
CpuUsageSample sample{};
46+
bool ready = cpuMonitor.sampleNow(sample); // drive sampling yourself
47+
if (!ready) {
48+
Serial.println("[manual_sampling] calibrating baseline...");
49+
} else {
50+
Serial.printf("[manual_sampling] avg=%.1f%% history_depth=%u\n",
51+
sample.average,
52+
static_cast<unsigned>(cpuMonitor.history().size()));
53+
}
54+
}
55+
56+
delay(10);
57+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[env:esp32dev]
2+
platform = espressif32
3+
board = esp32dev
4+
framework = arduino
5+
monitor_speed = 115200
6+
7+
lib_deps =
8+
ArduinoJson @ ^7
9+
ESPCpuMonitor

0 commit comments

Comments
 (0)