How to Get CPU Load and Memory Usage

raspberrypi3

#1

Hello,

I’m new to Resin, Electron, and javascript so I apologize if this is a dumb question. I’m currently building a smart mirror project on a Raspberry Pi 3 B+ using Resin and Electron. The issue I’m having is I want to display diagnostic information on the mirror (as of right now just CPU load and memory usage). The problem I’m having arrises when I try to read from the /proc directory to get the live stats.

To start out, my directory structure looks like this (on my local machine before I push to resin):

resin-electronjs
|
|----Dockerfile.template
|----LICENSE
|----README.md
/app
    |
    |---bower.json
    |---main.js
    |---package-lock.json
    |---package.json
    |---start.sh
    /node_modules
    /data
        |--index.html
        |--clock.css
        |--main_window.css
        /js
            |--bundle.js
            |--clock.js
            |--core.js
            |--diagnostics.js
            |--py_stats.js

I discovered that I can print the stats to the console once if I use “var fs = require(‘fs’);” at the top of the main.js file and then call this in main.js:

fs.readFile('/proc/meminfo', 'utf8', function(err, data){
                   console.log("GETTING MEM INFO");
                   if(err){
                       console.log("MEM READ ERROR");
                       console.log(err);
                       cb(err);
                       return;
                   }
                   console.log("MEMORY_INFO: " + data);
                   
                   cb(null, memInfo);
                   });

The issue is, I want to be able to get the cpu/memory data in the core.js file so I can update the UI during runtime. To do this I used browserify to package everything into bundle.js so I can use “var fs = require(‘browserify-fs’)” at the top of the core.js file. when I do fs.readFile the same way as above though, it prints an error to the console saying that there is no file “/proc/meminfo”.

I’m not sure if this has to do with where the application is running but I can’t seem to access the /proc folder from within any of my .js files other than main.js

Thanks in advance for any help!


#4

Hi @RyanDevlin!

A Browser process would indeed not have access to the hardware on the device, the way you would make that work is by having one Node.js server running on the device that reads the data, and have the browser connect to it, and request the data & display it.

This sample project is pretty close to what I think you’re trying to achieve, so hope that’ll get you up and running!


#5

Thank you for the response Chris! I’ll attempt this and report back my findings for others to view later.