Node JS, Read CPU temperature
Asked Answered
R

5

13

I am currently working on a project where I want to read the temperature of my CPU but I do not know how to it properly without using external programs such as "CpuTemp" etcetera. According to some sources I should be able to use Node JS together with a package to read the CPU temperature. I managed to read total memory on my PC and uptime via the package "OS". Are there any package I can use to display the CPU temperature or should I do something else to read it?

I have been told I should use Node JS together with WMI, I dont know how to continue on that though.

Rasorial answered 6/11, 2017 at 11:42 Comment(3)
Case you are running linux you may have something like that: cat /sys/class/thermal/thermal_zone0/temp . Divide the result by 1000 and you get the CPU temperature.Stentorian
pretty sure this is a Windows question (the mention of WMI?)Raptor
Yes, I am talking about Windows since I already know how to do this on Linux. @StentorianRasorial
S
12

Here you have a NPM package for CPU temp as well as other statistics: https://www.npmjs.com/package/systeminformation
The package is well documented, and will tell you exactly what to do. Otherwhise, it's easy just to google the error codes or other problems that you might encounter.

Sauveur answered 13/10, 2018 at 19:10 Comment(0)
H
6

Simple web search will return a bunch of nodeJS packages for tracking CPU temperature. For example https://github.com/sebhildebrandt/systeminformation

Have not tested it, but does look like something you would be looking for.

Hellhole answered 6/11, 2017 at 11:58 Comment(0)
S
6

Case you are running linux it can be:

Create a file called "temp.js" and insert below code:

var spawn = require('child_process').spawn;

temp = spawn('cat', ['/sys/class/thermal/thermal_zone0/temp']);

temp.stdout.on('data', function(data) {
        console.log('Result: ' + data/1000 + ' degrees Celcius');
});

in console run:

node temp.js

Result should be something like below:

Result: 46.16 degrees Celcius
Stentorian answered 6/11, 2017 at 14:19 Comment(1)
Yes I have read about this solution but since the project is made for windows I believe I have to work via WMI or a Node Package.Rasorial
P
1

You can use the following code, it's fetching the temp at an interval of 5 seconds-

const si = require('systeminformation');
setInterval(()=>{
    si.cpuTemperature()
        .then(tmp=>{
            console.log(tmp);
        });
    },5000);

Note. The system should support this API, otherwise it'll show -1 in output

Perpetuate answered 24/2, 2020 at 12:33 Comment(1)
I am not sure if that is what you meant but, when I am running this as non admin I get -1 as output aswell?Rasorial
F
0

WINDOWS 10 users who experience problems with CPU temperature request. Get something like this { main: -1, cores: [], max: -1 }

I runned cmd as administrator and executed script trough there and it solved this problem for me.

Windows Temperature, Battery, ... wmic - which is used to determine temperature and battery sometimes needs to be run with admin privileges. So if you do not get any values, try to run it again with according privileges. If you still do not get any values, your system might not support this feature. In some cases we also discovered that wmic returned incorrect temperature values.

Fissirostral answered 13/11, 2020 at 19:17 Comment(2)
The question is not related to a WMI permissions problem.Gaptoothed
Even if this answer is not an answer it helped me with a problem I had. Maybe it should be a comment instead though?Rasorial

© 2022 - 2024 — McMap. All rights reserved.