Book Image

Internet of Things Programming with JavaScript

Book Image

Internet of Things Programming with JavaScript

Overview of this book

The Internet of Things is taking the tech world by storm, and JavaScript is at its helm. This book will get you to grips with this exciting new technology. Where do Node.js, HTML5 and Windows 10 IoT Core come in with JavaScript and IoT? Why Raspberry Pi Zero rather than Arduino? How do you configure and build an IoT network from scratch? All your IoT JavaScript questions are answered in this book.
Table of Contents (15 chapters)
Internet of Things Programming with JavaScript
Credits
About the Author
www.packtpub.com
Customer Feedback
Preface

Controlling the relay using aREST commands from a web browser


In the next section, we will see how to control a digital output from a web browser using Rest commands. Let's dive into it, to see more details:

Configuring the web server

You can now either copy the code inside a file called outputcontrol.js, or just get the complete code from the folder for this project and execute it with Node.js. Open the terminal on your Raspberry Pi and type the following:

sudo node output control.js

We define the GPIO of the device importing the commands, by using the following:

var gpio = require('rpi-gpio'); 

Now we will create our web server using Node.js using the following lines.

We import the require packages that are necessary to run. We declare the libraries using the following:

var express = require('express'); 
var app = express(); 

Define the body parser and open the port, in this case, 8099:

var Parser = require('body-parser'); 
var port = 8099; 

Use the body-parser:

app.use...