GPIO Control in JavaScript

 

GPIO control in JavaScript isnt as pretty as Python but it can be done!

This tutorial will show you how to turn a pin on. You should be able to figure out the rest J

 

Before you start you will need to install Node.js and rpi-gpio

 

Node.js May already be installed on the PI check in the terminal like this: node v , this should give you a version number if it is there

 

Rpi-gpio Is a node.js based library that helps access the GPIO

 

Follow this post to get the file installed: https://www.npmjs.com/package/rpi-gpio (it also explains how everything works in more depth)

 

 

/*****************************************************************

This program sets up pin 7 as an output pin, then turns it on and off again.

 

*****************************************************************/

var gpio = require('rpi-gpio'); //gets the rpi-gpio library

gpio.setup(7, gpio.DIR_OUT, write); //set up pin 7 as an output pin (the write function deals with any errors that may arise)

gpio.write(7, true, function(err){if (err) throw err;}); //Turn pin 7 on (true)

 

function write() { //This function makes sure everything is working correctly and handles any errors.

gpio.write(7, false, function(err){

if (err) throw err;

 

console.log('Written to pin');

});

 

 

Important Notes:

 

To add additional pins you will need to repeat each of the 3 blocks of code that refer to pin 7.

 

 

Try This:

 

Incorporate this code into your web server so that an LED turns on when the client connects to it.