BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Need help with spawn child_process running continuously


#1

I have successfully been able to run a python script through spawn child process in NodeJS with the code below and I’m able to display the output(Raspberry Pi CPU load) in blynk value display widget.
What I can’t figure is how to read the output continuously maybe every second so I can see the CPU load in real time in Blynk widget. I tried “setInterval” but is causing it to bypass my main code and does not display anything.
Any help will be appreciated.

NodeJS code:

var Blynk = require('blynk-library');

var AUTH = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';

var blynk = new Blynk.Blynk(AUTH, options = { connector : new Blynk.TcpClient( options = { addr: "xxx.xxx.x.xx", port: 8080 } ) });

require('events').EventEmitter.defaultMaxListeners = Infinity;
var exec = require('child_process').exec, child;
var util = require("util");
var spawn = require("child_process").spawn;

var cpuload = spawn('python',["cpuload.py"]);

var v12 = new blynk.VirtualPin(12);//assigned to a label


function loop() {

cpuload.stdout.on('data',(function(load){ // executes child process
var load = load.toString('utf8') // assigns load as a string to variable
blynk.virtualWrite(12,load) // writes to Blynk label
}))};

setInterval(loop,1000);

Python code:

import os
import sys
import subprocess

cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
CPU = subprocess.check_output(cmd, shell = True )


print(CPU)

#2

Not sure how you set yours up, but it shouldn’t ‘bypass’ anything if coded properly. EDIT, unless it is your child process that is somehow locking up the process?? But that starts into “programming with different languages 101” which is a bit beyond this forums forte.

I use this to flash a virtual LED when switched ON, and it runs alongside a bunch of other concurrently running code… seems to work fine.

// Timed function to blink virtual LED
const BlinkButton = new blynk.VirtualPin(26);  // Setup Blinking Button Widget
const BlinkLED = new blynk.VirtualPin(27);  // Setup Blinking LED Widget
var intervalId;

BlinkButton.on('write', function(param) {  // Watches for virtual Blinking Button
  if (param == 1) {  // If Button OFF
		run();
		} else if (param == 0) {  // If Button ON
			stop()
			}
});


function run() {
	intervalId = setInterval(blinkON, 1000);
}

function stop() {
	clearInterval(intervalId);
}

function blinkON() {
	blynk.virtualWrite(27, 255);  // Virtual Blinking Blue LED Widget ON
	setTimeout(blinkOFF, 500);
}

function blinkOFF() {
	blynk.virtualWrite(27, 0);  // Virtual Blinking Blue LED Widget OFF
}

#3

OK… I think I understand your attempt a bit better… How can you be sure the Python code output is properly transferred into the variable load in the JS code?? They are two separate scripts in two separate languages.

Can you not just Google for a way of reading the CPU load in JS?


#4

OK, after re examining your post I see that you did get it to work… at least once :blush:

Without taking the time to try it myself, as I just don’t have the mental focus right now… I suggest you look at my code example… perhaps something will make sense for your situation.


#5

Finally got it to work. I needed to move this line

var cpuload = spawn(‘python’,[“cpuload.py”]);

in to the loop section of the code and I had to add another function and run command after the code.
Thank you Gunner for the quick responses.

function loop() {

var cpuload = spawn('python',["cpuload.py"]);
cpuload.stdout.on('data',(function(load){ // executes child process
var load = load.toString('utf8') // assigns load as a string to variable
blynk.virtualWrite(12, load) // writes to Blynk label
}))};

function run() {
  setInterval(loop, 2000);
};

run();

#6

@malvar this might be a dumb question, but why not just use an npm package for CPU load? And this is nitpicking, but I would recommend using a different name for your loop function- something more descriptive and useful… Something like getCpuUsage(). Loop makes it sound like an Arduino app :stuck_out_tongue:.


#7

Not a dumb question, I’m just starting with a basic example to make sure that I can achieve communication between python, Nodejs, and Blynk. My goal is to do the same for a current sensor (INA3221) that has very good python library support but very little documentation for NodeJS. I do like your suggestion and I will use getCpuUsage now.