Created
January 8, 2026 22:22
-
-
Save JanMalch/866c0502171a48d33b778c98cc3b001f to your computer and use it in GitHub Desktop.
NodeJS server to respond with Raspberry Pi CPU temperature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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}`); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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