raspberry-stats v3.0.1
Raspberry Stats
A Node.js module for retrieving system information from a Raspberry Pi.
It uses the child_process.spawn command to read various parameters, including CPU temperature, memory usage, disk usage, voltage, and clock frequencies.
Synchronous (callback-based) and asynchronous (promise-based) APIs are provided for each method.
Note: This module relies on
vcgencmdand other system commands that are typically available on Raspberry Pi OS. It may not work on other platforms or distributions where these commands are unavailable.
Table of Contents
- Raspberry Stats
- Table of Contents
- Installation
- Usage
- API
- Return Format Overview
- getCPUTemperature / getCPUTemperatureAsync
- getCPUUsage / getCPUUsageAsync
- getMemoryUsage / getMemoryUsageAsync
- getDiskUsage / getDiskUsageAsync
- getVoltage / getVoltageAsync
- getClockFrequencies / getClockFrequenciesAsync
- getClockFrequency / getClockFrequencyAsync
- getUptime / getUptimeAsync
- Error Handling
- License
Installation
With npm:
npm install raspberry-statsWith yarn:
yarn add raspberry-statsWith pnpm:
pnpm add raspberry-statsWith bun:
bun add raspberry-statsUsage
Example using both callback-based and async/await methods:
import {
getCPUTemperature,
getCPUTemperatureAsync,
getMemoryUsage,
getMemoryUsageAsync,
// ... other imports
} from "raspberry-stats";
// --- Using callback-based methods ---
// Each callback receives a SystemInfo<T> object containing { data, error }
// Example 1: CPU Temperature
getCPUTemperature((info) => {
if (info.error) {
console.error("Failed to retrieve CPU temperature:", info.error);
} else {
console.log(`CPU Temperature: ${info.data}°C`);
}
});
// Example 2: Memory Usage
getMemoryUsage((info) => {
if (info.error) {
console.error("Failed to retrieve memory usage:", info.error);
} else {
console.log("Memory usage:", info.data);
}
});
// --- Using promise-based methods (Async/Await) ---
// Each async function returns a Promise<SystemInfo<T>>
(async () => {
try {
// Example 1: CPU Temperature
const tempInfo = await getCPUTemperatureAsync();
if (tempInfo.error) {
console.error("Failed to retrieve CPU temperature:", tempInfo.error);
} else {
console.log(`CPU Temperature: ${tempInfo.data}°C`);
}
// Example 2: Memory Usage
const memInfo = await getMemoryUsageAsync();
if (memInfo.error) {
console.error("Failed to retrieve memory usage:", memInfo.error);
} else {
console.log("Memory usage:", memInfo.data);
}
} catch (error) {
console.error(error);
}
})();API
Return Format Overview
All callback-based functions receive a single parameter of type SystemInfo<T>:
interface SystemInfo<T> {
data: T | null;
error: string | null;
}datacontains the successfully retrieved value (e.g.number,object, orarray) if everything went well, otherwisenull.erroris a string describing the problem if something failed, otherwisenull.
Similarly, all promise-based functions resolve with SystemInfo<T> instead of just the raw data or throwing an error.
Hence, if error is non-null, you can handle it accordingly in your async flow.
getCPUTemperature / getCPUTemperatureAsync
Signature (Callback):
getCPUTemperature(callback: (info: SystemInfo<number>) => void): void;- description
Reads the CPU temperature by runningvcgencmd measure_temp. - callback
Receives an object of typeSystemInfo<number>.info.datais the temperature (in °C) if successful, otherwisenull.info.errorcontains an error message if something went wrong, otherwisenull.
Signature (Async):
getCPUTemperatureAsync(): Promise<SystemInfo<number>>;- description
Asynchronous/promise-based version of the above function.
Resolves withSystemInfo<number>—checkinfo.errorto see if it succeeded.
getCPUUsage / getCPUUsageAsync
Signature (Callback):
getCPUUsage(callback: (info: SystemInfo<number>) => void): void;- description
Runstoprepeatedly (e.g., 10 iterations at 0.1-second intervals) and gathers the CPU usage values.
It parses the CPU usage, calculates an average from the samples, and then invokes the callback with the final usage percentage (e.g., 17.2 for ~17.2% usage). - callback
Receives an object of typeSystemInfo<number>.info.datais the CPU usage ornullif it fails.info.erroris an error message if it fails.
Signature (Async):
getCPUUsageAsync(): Promise<SystemInfo<number>>;- description
Promise-based version.
Resolves withSystemInfo<number>—checkinfo.errorto see if it succeeded.
getMemoryUsage / getMemoryUsageAsync
Signature (Callback):
getMemoryUsage(callback: (info: SystemInfo<MemoryUsageResult>) => void): void;Where MemoryUsageResult is:
interface MemoryUsageResult {
total: number;
used: number;
free: number;
shared: number;
buffCache: number;
available: number;
}- description
Reads memory usage via thefreecommand. - callback
Receives an object of typeSystemInfo<MemoryUsageResult>.info.datais aMemoryUsageResultobject if successful, otherwisenull.info.erroris an error message if something went wrong, otherwisenull.
Signature (Async):
getMemoryUsageAsync(): Promise<SystemInfo<MemoryUsageResult>>;- description
Promise-based version.
Resolves withSystemInfo<MemoryUsageResult>—checkinfo.errorto see if it succeeded.
getDiskUsage / getDiskUsageAsync
Signature (Callback):
getDiskUsage(callback: (info: SystemInfo<DiskUsageResult[]>) => void): void;Where DiskUsageResult is:
interface DiskUsageResult {
filesystem: string;
oneKBlocks: number;
used: number;
available: number;
usePercentage: number;
mountedOn: string;
}- description
Reads disk usage info via thedfcommand. - callback
Receives an object of typeSystemInfo<DiskUsageResult[]>.info.datais an array of disk usage objects if successful, otherwisenull.info.erroris an error message if it fails.
Signature (Async):
getDiskUsageAsync(): Promise<SystemInfo<DiskUsageResult[]>>;- description
Promise-based version.
Resolves withSystemInfo<DiskUsageResult[]>—checkinfo.erroron result.
getVoltage / getVoltageAsync
Signature (Callback):
getVoltage(callback: (info: SystemInfo<number>) => void): void;- description
Reads the core voltage by runningvcgencmd measure_volts. - callback
Receives an object of typeSystemInfo<number>.info.datais the voltage (in Volts) if successful, otherwisenull.info.erroris an error message if it fails.
Signature (Async):
getVoltageAsync(): Promise<SystemInfo<number>>;- description
Promise-based version.
Resolves withSystemInfo<number>—checkinfo.errorto see if it succeeded.
getClockFrequencies / getClockFrequenciesAsync
Signature (Callback):
getClockFrequencies(
callback: (info: SystemInfo<ClockFrequencyResult[]>) => void
): void;Where ClockFrequencyResult is:
interface ClockFrequencyResult {
clock: string;
frequency: number | null; // in Hz
}- description
Reads multiple clock frequencies (arm, core, h264, etc.) by runningvcgencmd measure_clock. - callback
Receives an object of typeSystemInfo<ClockFrequencyResult[]>.info.datais an array of clock/frequency objects if successful, otherwisenull.info.erroris an error message if it fails.
Signature (Async):
getClockFrequenciesAsync(): Promise<SystemInfo<ClockFrequencyResult[]>>;- description
Promise-based version.
Resolves withSystemInfo<ClockFrequencyResult[]>—checkinfo.erroron result.
getClockFrequency / getClockFrequencyAsync
Signature (Callback):
getClockFrequency(
clock: Clock,
callback: (info: SystemInfo<number>) => void
): void;Where Clock is an enum:
enum Clock {
ARM = "arm",
CORE = "core",
H264 = "h264",
ISP = "isp",
V3D = "v3d",
UART = "uart",
PWM = "pwm",
EMMC = "emmc",
PIXEL = "pixel",
VEC = "vec",
HDMI = "hdmi",
DPI = "dpi",
}- description
Reads the specified clock frequency by runningvcgencmd measure_clock. - callback
Receives an object of typeSystemInfo<number>.info.datais the frequency (in Hz) if successful, otherwisenull.info.erroris an error message if it fails.
Signature (Async):
getClockFrequencyAsync(clock: Clock): Promise<SystemInfo<number>>;- description
Promise-based version.
Resolves withSystemInfo<number>—checkinfo.erroron result.
getUptime / getUptimeAsync
Signature (Callback):
getUptime(callback: (info: SystemInfo<number>) => void): void;- description
Reads the system uptime by runningawk '{print $1 * 1000}' /proc/uptime. - callback
Receives an object of typeSystemInfo<number>.info.datais the uptime in milliseconds if successful, otherwisenull.info.erroris an error message if something went wrong.
Signature (Async):
getUptimeAsync(): Promise<SystemInfo<number>>;- description
Promise-based version.
Resolves withSystemInfo<number>—checkinfo.erroron result.
Note: The uptime is returned in milliseconds but is based on the system’s uptime in seconds.
Error Handling
Because all the functions return a SystemInfo<T> object, there is no more "rejecting" or passing null—the callback or promise resolves with an object which may contain an error message:
- For callback-based methods, check
info.error.
Ifinfo.erroris notnull, an error occurred; otherwiseinfo.datacontains the good value. - For async methods, check the returned object’s
.errorproperty.
Iferroris non-empty, handle it accordingly; if not,.datacontains the requested information.
Example:
// Async example: getCPUTemperatureAsync
(async () => {
const info = await getCPUTemperatureAsync();
if (info.error) {
// something went wrong
console.error("Failed to read CPU temperature:", info.error);
} else {
// success
console.log(`CPU Temperature: ${info.data}°C`);
}
})();License
Feel free to open issues, PRs, or contribute in any way you see fit!