CraigDubya
Posts: 3
Joined: Tue Mar 28, 2017 11:34 am

Running a terminal command from within Nodejs

Tue Mar 28, 2017 11:39 am

I would like to run the following terminal command from within a javascript file:

echo 0=50% >/dev/servoblaster

I have trawled the internet and must confess that everything I see is confusing me.

I would also like to make the 50% a variable within the javascript that can be passed to the terminal command.

CraigDubya
Posts: 3
Joined: Tue Mar 28, 2017 11:34 am

Re: Running a terminal command from within Nodejs

Tue Mar 28, 2017 6:59 pm

After finding the answer I thought I would share here for anyone interested.

Code: Select all

#!/bin/bash
var exec = require('child_process').exec;

var Blynk = require('/usr/lib/node_modules/blynk-library');

var AUTH = 'YOUR AUTH TOKEN GOES HERE';

var blynk = new Blynk.Blynk(AUTH);

var v2 = new blynk.VirtualPin(2);

v2.on('write', function(param) {
exec('echo 0='+param[0]+'% >/dev/servoblaster', function (err,stdout,stderr) {})
});
As you can see I am using blynk to control a servo (which is attached to a webcam)

The code line I was looking for was:

exec('echo 0='+param[0]+'% >/dev/servoblaster', function (err,stdout,stderr) {})

I also needed to import the child_process exec command using:

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

Return to “Java”

Who is online

Users browsing this forum: No registered users and 0 guests