In this tutorial we are using Node.js for controlling a LED using Raspberry Pi. Node.js is a very popular JavaScript-based environment, originally developed for the Google Chrome browser but now it is open source. This language runs on various platforms like Windows, Linux, Unix, Mac OS X, etc. We are going to Control LED with Raspberry Pi using Nodejs Webpage for this project. We use Express for web server. Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. We will create an HTML page that has two buttons, one for turning led on and second for turning led off and a JavaScript file. Using this HTML page we can control led using any web browser.

Requirements

Raspberry Pi

LED

Breadboard

250-ohm resistor

Jumper Wires

Circuit Diagram

Start with installing Node.js on your Raspberry Pi using the below command, if you have not installed yet.

sudo apt-get update

curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash - sudo apt-get install -y nodejs

sudo apt-get install build-essential

Now create a new directory and enter into the directory and create a JS file.

mkdir rpinodeled cd rpinodeled sudo nano nodeled.js

Copy and paste the below given code inside this file

var express = require('express'); var app = express(); var path = require('path'); var gpio = require('rpi-gpio'); gpio.setup(12, gpio.DIR_OUT); app.set('view engine', 'ejs'); app.use(express.static(path.join(__dirname, 'public'))); console.log(path.join(__dirname, 'public')); app.get('/', function(req, res){ res.render('index',{status:"Press Button To change Status of Led !!"}); }); app.post('/led/on', function(req, res){ gpio.write(12, true, function(err) { if (err) throw err; console.log('Written True to pin'); console.log(path.join(__dirname, 'public')); return res.render('index', {status: "Led is On"}); }); }); app.post('/led/off', function(req, res){ gpio.write(12, false, function(err) { if (err) throw err; console.log('Written False to pin'); console.log(path.join(__dirname, 'public')); return res.render('index',{status: "Led is Off"}); }); }); app.listen(4000, function () { console.log('Simple LED Control Server Started on Port: 4000!') })

Now make another folder inside the rpinodeled directory and go inside the folder:

mkdir views cd views

Now create an HTML page inside the folder

sudo nano index.ejs

Then, paste the following HTML code inside this file:

<meta name="viewport" content="width=500, initial-scale=1"> <!--This line of code is for mobile responsive --> <div class="BorderMargin"> <h1>Welcome to Nodejs LED Control Server - IoTbyHVM.OOO</h1> <p> Visit <a href="https://iotbyhvm.ooo"> IoTbyHVM.ooo </a> For more Raspberry Pi Tutorials and IoT Tutorials. <form action="/led/on" method="post"> <button type="submit" class="button">LED On </button> <button type="submit" formmethod="post" formaction="/led/off" class="button button3">LED Off</button> </form> <a>Led Status: <%=status %></a> </div> <!-- The below code is only for look and styling --> <style> .button { background-color: #4CAF50; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; } .button2 {background-color: #008CBA;} /* Blue */ .button3 {background-color: #f44336;} /* Red */ .button4 {background-color: #e7e7e7; color: black;} /* Gray */ .button5 {background-color: #555555;} /* Black */ .BorderMargin { margin: 10px; padding: 10px; max-width: 300px; height: 300px; border: 1px solid black; } </style>

Installing Packages for Node.js in Raspberry Pi

Now go to the rpinodeled directory and install packages for Node.js using the following command:

npm install

If this command shows an error then try installing the packages using their names:

npm install ‘package name’

For example npm install expression, npm install rpi-gpio etc.

npm install rpi-gpio npm install express npm install ejs npm install path

Now after installing all the packages, run the JavaScript code:

sudo node nodeled.js

Now, navigate to your browser and search for the web page using 192.168.xx.xx:4000. Where 192.168.xx.xx is Pi’s IP address that you should replace with your Pi’s IP address and 4000 is port number.

Find IP address of Rpi, using the following command

ifconfig

Now open rpinodeled folder and run nodeled.js

node nodeled.js

Hence, we have successfully controlled the LED using Node.js and Raspberry Pi. Also, check out our Other IoT projects using Raspberry Pi: