├── .gitignore ├── README.md ├── config.json ├── index.js ├── package.json ├── result.png └── sensor-monitor.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/* 2 | config.template.json 3 | config.json 4 | .idea -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A Temperature Monitor using Nodejs and MQTT 2 | A PC temperature monitor that sends the reads using MQTT
3 | Currently only supports CPU and Nvidia graphic temperatures
4 | 5 | I'm using this to send the temperatures to the Home Assistant broker 6 | 7 | ### Prerequisites 8 | 1 - NodeJS
9 | 2 - Nvidia Graphic Drivers 10 | 11 | In Windows you probably have to add this to your PATH variable 12 | 13 | ```bash 14 | C:\Program Files\NVIDIA Corporation\NVSMI 15 | ``` 16 | 17 | An example of `configuration.yaml` for Home Assistant 18 | ```bash 19 | - platform: mqtt 20 | state_topic: 'computer/temperatures/cpu/0' 21 | name: 'CPU - Thread 0' 22 | unit_of_measurement: ' C' 23 | 24 | - platform: mqtt 25 | state_topic: 'computer/temperatures/cpu/1' 26 | name: 'CPU - Thread 1' 27 | unit_of_measurement: ' C' 28 | 29 | - platform: mqtt 30 | state_topic: 'computer/temperatures/cpu/2' 31 | name: 'CPU - Thread 2' 32 | unit_of_measurement: ' C' 33 | 34 | - platform: mqtt 35 | state_topic: 'computer/temperatures/cpu/3' 36 | name: 'CPU - Thread 3' 37 | unit_of_measurement: ' C' 38 | 39 | - platform: mqtt 40 | state_topic: 'computer/temperatures/cpu/4' 41 | name: 'CPU - Thread 4' 42 | unit_of_measurement: ' C' 43 | 44 | - platform: mqtt 45 | state_topic: 'computer/temperatures/cpu/5' 46 | name: 'CPU - Thread 5' 47 | unit_of_measurement: ' C' 48 | 49 | - platform: mqtt 50 | state_topic: 'computer/temperatures/graphic-card/1' 51 | name: 'Graphic Card' 52 | unit_of_measurement: ' C' 53 | ``` 54 | With some tweaks the result will be something like this :) 55 | 56 | ![alt text](https://raw.githubusercontent.com/freakstatic/node-temperature-mqtt/master/result.png) 57 | 58 | ### Installation 59 | Change the `config.json` file for your broker settings 60 | ```bash 61 | $ npm install 62 | ``` 63 | ### Usage 64 | ```bash 65 | $ npm start 66 | ``` 67 | or 68 | ```bash 69 | $ node index.js 70 | ``` 71 | -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "mqtt": { 3 | "host": "mqtt://192.168.1.xx", 4 | "port": "1883", 5 | "auth": true, 6 | "username": "your broker username", 7 | "password": "your broker password", 8 | "clientId": "" 9 | }, 10 | "log": true, 11 | "interval": 5000 12 | } 13 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const config = require('./config'); 4 | const SensorMonitor = require('./sensor-monitor.js'); 5 | 6 | let mqtt = require('mqtt'); 7 | 8 | let options = { 9 | port: config.mqtt.port, 10 | clientId: config.mqtt.clientId.trim().length ? config.mqtt.clientId : 'mqttjs_' + Math.random().toString(16).substr(2, 8), 11 | username: config.mqtt.username, 12 | password: config.mqtt.password, 13 | }; 14 | 15 | let client = mqtt.connect(config.mqtt.host, options); 16 | 17 | client.on('connect', () => { 18 | console.log('Connected to broker on ' + config.mqtt.host + ':' + config.mqtt.port); 19 | let oldCpuTemperatures = []; 20 | let oldGraphiCardTempeatures = []; 21 | let intervalCpuTemps = setInterval(async () => { 22 | try { 23 | let cpuTemps = await SensorMonitor.getProcessorTemps(); 24 | cpuTemps.forEach((temperature, index) => { 25 | if (!oldCpuTemperatures[index] || oldCpuTemperatures[index] !== temperature) { 26 | if (config.log) { 27 | console.log('CPU Thread ' + index + ': ' + temperature) 28 | } 29 | oldCpuTemperatures[index] = temperature; 30 | client.publish('computer/temperatures/cpu/' + index, "" + temperature); 31 | } 32 | }); 33 | 34 | let graphicTemperatures = await SensorMonitor.getGraphicTemperatures(); 35 | graphicTemperatures.forEach((temperature, index) => { 36 | if (!oldGraphiCardTempeatures[index] || oldGraphiCardTempeatures[index] !== temperature) { 37 | if (config.log) { 38 | console.log('Graphic card ' + index + ': ' + temperature); 39 | } 40 | client.publish('computer/temperatures/graphic-card/' + index, temperature); 41 | oldGraphiCardTempeatures[index] = temperature; 42 | } 43 | }); 44 | } catch (error) { 45 | console.error(error); 46 | } 47 | }, config.interval); 48 | }); 49 | 50 | client.on('close', () => { 51 | console.error('Unable to connect with broker'); 52 | }); 53 | 54 | client.on('error', (error) => { 55 | console.error('The following error has occurred ' + error); 56 | }); 57 | 58 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-temperature-mqtt", 3 | "version": "1.0.0", 4 | "description": "A temperature monitor that sends the reads using MQTT", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node index.js", 8 | "test": "node index.js" 9 | }, 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mqtt": "^2.13.0", 14 | "systeminformation": "^5.6.7" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/freakstatic/node-temperature-mqtt/af89be310d51a35e30cf67ef803cd9bff746c12f/result.png -------------------------------------------------------------------------------- /sensor-monitor.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const {promisify} = require('util'); 3 | const exec = promisify(require('child_process').exec) 4 | const si = require('systeminformation'); 5 | module.exports = class SensorMonitor { 6 | static getProcessorTemps() { 7 | return new Promise((resolve, reject) => { 8 | si.cpuTemperature().then((data) => { 9 | resolve(data.cores); 10 | }).catch(error => reject(error)); 11 | 12 | }) 13 | }; 14 | 15 | static async getGraphicTemperatures() { 16 | try { 17 | let result = await exec('nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader', { 18 | windowsHide: true 19 | }); 20 | if (result.stdout) { 21 | let temperatures = result.stdout.split('\n'); 22 | temperatures.pop(); 23 | 24 | return temperatures; 25 | } 26 | }catch (error){ 27 | console.error(error); 28 | } 29 | throw new Error('Unable to get graphic card temperature') 30 | }; 31 | }; 32 | 33 | --------------------------------------------------------------------------------