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