Skip to content

Instantly share code, notes, and snippets.

@JanMalch
Created January 8, 2026 22:22
Show Gist options
  • Select an option

  • Save JanMalch/866c0502171a48d33b778c98cc3b001f to your computer and use it in GitHub Desktop.

Select an option

Save JanMalch/866c0502171a48d33b778c98cc3b001f to your computer and use it in GitHub Desktop.
NodeJS server to respond with Raspberry Pi CPU temperature
#!/usr/bin/env node
const http = require("http");
const childProcess = require("child_process");
const host = process.env.HOST || '0.0.0.0'
const port = process.env.PORT || '7373'
const requestListener = function (req, res) {
try {
const result = childProcess.execSync("vcgencmd measure_temp", { encoding: 'utf-8' })
const stripped = result.trim().substring(5) // remove "temp="
res.setHeader("Content-Type", "application/json; charset=utf-8");
res.writeHead(200);
res.end(JSON.stringify({
temperature_formatted: stripped.replace("'", '°'),
temperature: Number.parseFloat(stripped.slice(0, -2)),
}));
} catch (error) {
res.setHeader("Content-Type", "application/json; charset=utf-8");
res.writeHead(500);
res.end(JSON.stringify({ error: error.message.trim() }))
}
};
http.createServer(requestListener).listen(port, host, () => {
console.log(`Server is running on http://${host}:${port}`);
});
# Homepage integration
# https://gethomepage.dev/widgets/services/customapi/
type: customapi
url: http://my.pi.ip:7373
mappings:
- field: temperature_formatted
label: CPU Temp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment