Reader small image

You're reading from  Raspberry Pi LED Blueprints

Product typeBook
Published inSep 2015
Publisher
ISBN-139781782175759
Edition1st Edition
Right arrow
Author (1)
Agus Kurniawan
Agus Kurniawan
author image
Agus Kurniawan

Agus Kurniawan is an independent technology consultant, author, and lecturer. He has over 18 years' experience working on various software development projects, including delivering training courses and workshops, and delivering technical writing. He has done a few research activities related to wireless networking, software, and security in multiple universities. Currently, he is pursuing a Ph.D. program in Computer Science in Germany. He has previously written five books for Packt.
Read more about Agus Kurniawan

Right arrow

Controlling LEDs through RESTful


After learning to build a RESTful app, we continue to control sensor or actuator devices from RESTful. For instance, we want to turn on an LED by calling http://<server>/led1. In this case, Express receives HTTP GET from client. If the request is /led1, Express will turn on the LED for a certain time and then turn it off.

To implement this scenario, let's start to build a program. Create a file named ledrest.js and write the following complete code:

// ledrest.js
var gpio = require('rpi-gpio');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var port = 8099;


app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

gpio.setup(11, gpio.DIR_OUT);
gpio.setup(13, gpio.DIR_OUT);
gpio.setup(15, gpio.DIR_OUT);

function off1() {
    setTimeout(function() {
        gpio.write(11, 0);
    }, 2000);
}
function off2() {
    setTimeout(function() {
        gpio.write(13, 0);
    }, 2000);
...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Raspberry Pi LED Blueprints
Published in: Sep 2015Publisher: ISBN-13: 9781782175759

Author (1)

author image
Agus Kurniawan

Agus Kurniawan is an independent technology consultant, author, and lecturer. He has over 18 years' experience working on various software development projects, including delivering training courses and workshops, and delivering technical writing. He has done a few research activities related to wireless networking, software, and security in multiple universities. Currently, he is pursuing a Ph.D. program in Computer Science in Germany. He has previously written five books for Packt.
Read more about Agus Kurniawan