Memory Configuration
The Plexus C SDK uses compile-time flags to control buffer sizes and feature toggles. You can reduce RAM usage from the default ~17 KB down to ~1.5 KB by disabling features you do not need.
Configuration Options
Override any of these defaults by defining them before including plexus.h or via compiler flags (e.g., -DPLEXUS_MAX_METRICS=8).
Buffer Sizes
| Flag | Default | Description |
|---|---|---|
PLEXUS_MAX_METRICS | 32 | Max metrics per flush |
PLEXUS_MAX_METRIC_NAME_LEN | 64 | Max metric name length (bytes) |
PLEXUS_MAX_STRING_VALUE_LEN | 128 | Max string value length (bytes) |
PLEXUS_MAX_SOURCE_ID_LEN | 64 | Max source ID length |
PLEXUS_MAX_SESSION_ID_LEN | 64 | Max session ID length |
PLEXUS_MAX_API_KEY_LEN | 128 | Max API key length |
PLEXUS_MAX_ENDPOINT_LEN | 256 | Max endpoint URL length |
PLEXUS_JSON_BUFFER_SIZE | 2048 | JSON serialization buffer |
Tag Settings
| Flag | Default | Description |
|---|---|---|
PLEXUS_MAX_TAG_LEN | 32 | Max tag key/value length |
PLEXUS_MAX_TAGS | 4 | Max tags per metric |
Network Settings
| Flag | Default | Description |
|---|---|---|
PLEXUS_DEFAULT_ENDPOINT | "https://app.plexus.company/api/ingest" | Default ingest URL |
PLEXUS_HTTP_TIMEOUT_MS | 10000 | HTTP request timeout |
PLEXUS_MAX_RETRIES | 3 | Retry count on failure |
PLEXUS_RETRY_BASE_MS | 500 | Initial retry delay |
PLEXUS_RETRY_MAX_MS | 8000 | Maximum retry delay |
PLEXUS_RATE_LIMIT_COOLDOWN_MS | 30000 | Cooldown after 429 response |
Auto-Flush Settings
| Flag | Default | Description |
|---|---|---|
PLEXUS_AUTO_FLUSH_COUNT | 16 | Auto-flush after N metrics |
PLEXUS_AUTO_FLUSH_INTERVAL_MS | 5000 | Auto-flush interval (0 = disabled) |
Feature Toggles
| Flag | Default | Description |
|---|---|---|
PLEXUS_ENABLE_TAGS | 1 | Enable metric tags |
PLEXUS_ENABLE_STRING_VALUES | 1 | Enable string value support |
PLEXUS_ENABLE_BOOL_VALUES | 1 | Enable boolean value support |
PLEXUS_ENABLE_PERSISTENT_BUFFER | 0 | Enable flash-backed buffer on flush failure |
PLEXUS_ENABLE_STATUS_CALLBACK | 0 | Enable connection status notifications |
PLEXUS_ENABLE_THREAD_SAFE | 0 | Enable mutex-protected client access |
PLEXUS_DEBUG | 0 | Enable debug logging |
RAM Usage Breakdown
The client struct (plexus_client_t) is the only RAM allocation. Its size depends on your configuration:
| Component | Size Formula | Default |
|---|---|---|
| Metrics buffer | PLEXUS_MAX_METRICS * sizeof(plexus_metric_t) | ~14.8 KB (32 × 472 B) |
| JSON buffer | PLEXUS_JSON_BUFFER_SIZE | 2048 B |
| Fixed fields | API key + source ID + session ID + endpoint + counters | ~512 B |
| Total | ~17 KB |
The size of plexus_metric_t is 472 bytes with all features enabled (strings, bools, tags). Disabling string values removes 128 bytes per metric and disabling tags removes 257 bytes per metric — this is the main lever for reducing RAM. Use plexus_client_size() at runtime or PLEXUS_CLIENT_STATIC_SIZE at compile time to get the exact size for your configuration.
Default vs Minimal
| Configuration | Approximate RAM |
|---|---|
| Default (all features) | ~17 KB |
| Minimal (numbers only) | ~1.5 KB |
Default Configuration
All features enabled, 32 metrics per flush:
// No flags needed — these are the defaults
plexus_client_t* px = plexus_init("plx_xxxxx", "device-001");
printf("Client size: %u bytes\n", (unsigned)plexus_client_size());
// => ~17704 bytesMinimal Configuration
Numbers only, 8 metrics per flush, small buffers:
-DPLEXUS_MAX_METRICS=8
-DPLEXUS_ENABLE_TAGS=0
-DPLEXUS_ENABLE_STRING_VALUES=0
-DPLEXUS_ENABLE_BOOL_VALUES=0
-DPLEXUS_JSON_BUFFER_SIZE=512
-DPLEXUS_MAX_ENDPOINT_LEN=128
-DPLEXUS_MAX_API_KEY_LEN=64This reduces plexus_client_size() to approximately 1.5 KB.
In CMake:
target_compile_definitions(your_target PRIVATE
PLEXUS_MAX_METRICS=8
PLEXUS_ENABLE_TAGS=0
PLEXUS_ENABLE_STRING_VALUES=0
PLEXUS_ENABLE_BOOL_VALUES=0
PLEXUS_JSON_BUFFER_SIZE=512
PLEXUS_MAX_ENDPOINT_LEN=128
PLEXUS_MAX_API_KEY_LEN=64
)In Arduino/PlatformIO (platformio.ini):
build_flags =
-DPLEXUS_MAX_METRICS=8
-DPLEXUS_ENABLE_TAGS=0
-DPLEXUS_ENABLE_STRING_VALUES=0
-DPLEXUS_ENABLE_BOOL_VALUES=0
-DPLEXUS_JSON_BUFFER_SIZE=512
-DPLEXUS_MAX_ENDPOINT_LEN=128
-DPLEXUS_MAX_API_KEY_LEN=64Persistent Buffering
Enable flash-backed storage so unsent data survives power cycles and reboots:
-DPLEXUS_ENABLE_PERSISTENT_BUFFER=1When a flush fails, the SDK writes queued metrics to persistent storage (NVS on ESP32). On the next successful connection, stored data is sent first.
| Flag | Default | Description |
|---|---|---|
PLEXUS_PERSIST_MAX_BATCHES | 8 | Number of batch slots in the persistent ring buffer |
The persistent buffer HAL must be implemented for your platform. The ESP32 HAL uses NVS (nvs_flash). For STM32, you would implement plexus_hal_storage_write/read/clear using your flash storage driver.
Thread Safety
Enable mutex-protected client access for multi-threaded environments:
-DPLEXUS_ENABLE_THREAD_SAFE=1When enabled, all public API calls acquire a mutex before accessing client state. The mutex HAL must be implemented for your platform:
| Platform | Implementation |
|---|---|
| ESP32 (FreeRTOS) | xSemaphoreCreateRecursiveMutex() |
| STM32 (FreeRTOS) | osMutexCreate() |
| STM32 (bare-metal) | No-op stubs |
Without PLEXUS_ENABLE_THREAD_SAFE, the SDK is not thread-safe. Confine all calls for a given client to a single thread. Multiple clients in separate threads is always safe.
Blocking Behavior
plexus_flush() can block during retries. Here is the worst-case timing:
| Attempt | Delay | Cumulative |
|---|---|---|
| 1st try | 0 ms | 0 ms |
| 2nd try (retry 1) | 500 ms | ~500 ms |
| 3rd try (retry 2) | 1000 ms | ~1500 ms |
| Give up | — | ~1.5 s + HTTP timeouts (with jitter) |
| Platform | Behavior During Delay |
|---|---|
| FreeRTOS (ESP32/STM32) | vTaskDelay() / osDelay() — other tasks continue |
| Bare-metal (Arduino) | delay() — blocks the entire MCU |
| Bare-metal (STM32) | HAL_Delay() — blocks the entire MCU |
To avoid blocking your main loop, reduce PLEXUS_MAX_RETRIES or use FreeRTOS with a dedicated telemetry task.
Next Steps
- API Reference -- Full function and macro reference
- ESP32 Guide -- Complete ESP-IDF walkthrough
- STM32 Guide -- STM32 with FreeRTOS and LwIP