Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS and Raspberry Pi

I have now been running Raspbian on Raspberry Pi and I would like to make a control panel for it, so I can control my Raspberry Pi in a web browser. But how do I execute commands in NodeJS?

like image 668
DazDylz Avatar asked Mar 08 '14 17:03

DazDylz


1 Answers

You can use this node.js code to run commands on raspberry pi (the below is a sample to execute a reboot command on raspberry pi)

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

function execute(command, callback) {
  var cmd = exec(command, function(error, stdout, stderr) {
    console.log("error: " + error);
    callback(stdout);
  });
}

function reStart() {
  try {
    console.log("Reboot");
    execute('sudo reboot', function(callback) {
    });
  }
  catch (err) {
    console.log(err);
  }
}
like image 102
Rolwin Crasta Avatar answered Nov 30 '22 17:11

Rolwin Crasta