DazDylz
DazDylz

Reputation: 1096

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?

Upvotes: 6

Views: 2428

Answers (4)

Wilson López
Wilson López

Reputation: 45

You can use apt-get install -y node npm to install Node and NPM first of all, and then manage Node versions with n (https://www.npmjs.com/package/n) after that using Raspi-Io (https://www.npmjs.com/package/raspi-io) you can control Raspberry Pi directly.

Upvotes: 0

bodokaiser
bodokaiser

Reputation: 15752

You need to check if your linux supports the sysfs interface to GPIO and PWM. In most cases this is not available on standard setup and control is going over some "proprietary" (not directly proprietary but hard to control) interface.

If you have sysfs you can use the default fs module in nodejs to export a gpio fd and write on it.

I wrote a node package for this some time ago, but I am not sure if it is fully compatible with the Raspberry Pi: https://www.npmjs.org/package/native-io

Upvotes: 0

blupacetek
blupacetek

Reputation: 165

If you want a really easy way to do this, especially for development work, take a look at node-red.

You can install it for the Raspberry Pi and if you install the UI dashboard you have a whole bunch of buttons, etc. from a web server all ready to go.

There is also the node-red library that makes putting it all together with specific functions real easy.

Upvotes: 0

Rolwin Crasta
Rolwin Crasta

Reputation: 4339

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);
  }
}

Upvotes: 4

Related Questions