Wednesday, July 16, 2014

Node.js to read Raspberry Pi temperature

Last post show that we can get Raspberry Pi temperature via /sys/class/thermal/thermal_zone0/temp. This example show how to read it in Node.js.




var http = require("http");
var fs =  require("fs");

var server = http.createServer(function(request, response) {
    
    var temp = fs.readFileSync("/sys/class/thermal/thermal_zone0/temp");
    var temp_c = temp/1000;
    
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Raspberry Pi cpu temperature: ");
    response.write("\n" + temp);
    response.write("\n" + temp_c);
    response.end();
});
server.listen(8080);


No comments: