-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from urusai-me/master
node.js temperature module for raspberry pi series
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Raspberry PI Series (Linux) Temperature CPU | ||
* | ||
* @usage `node ./raspberrypi.js` | ||
*/ | ||
|
||
//import sf and http modules | ||
const fs = require('fs') | ||
const http = require('http') | ||
let temp | ||
|
||
//read CPU temperature every second | ||
setInterval(() => { | ||
temp = fs.readFileSync('/sys/class/thermal/thermal_zone0/temp') | ||
}, 1000) | ||
|
||
//creat http server | ||
const server = http.createServer((request, response) => { | ||
//write response header | ||
response.writeHead(200, { | ||
//defining document type then charset "utf-8" | ||
'Content-Type': 'application/json;charset="utf-8"', | ||
//CORS | ||
'Access-Control-Allow-Origin': "*" | ||
}) | ||
const items = [{ | ||
id: 'cpu', | ||
name: 'CPU Temperature', | ||
celsius: temp / 1000, | ||
}] | ||
//transform object into string, and output with .end() method | ||
response.end(JSON.stringify(items)) | ||
}) | ||
//listen port and IP address | ||
server.listen(4096, '127.0.0.1') |