Skip to content

Commit

Permalink
Merge pull request #59 from urusai-me/master
Browse files Browse the repository at this point in the history
node.js temperature module for raspberry pi series
  • Loading branch information
kmvan authored Jan 11, 2020
2 parents 512097c + 196d5cd commit ac5523d
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions temp_modules/raspberrypi.js
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')

0 comments on commit ac5523d

Please sign in to comment.