I would like to seek help on my LED Switch REST API. I have created my own REST API and able to light up my LED when I type in the URL for example: localhost:3030/7/1 to light on and localhost:3030/7/0 to light off. But when I start to implement a switch to manually on and off my LED without keying URL, my whole REST API able to start up but no LEDs light up when I click my switch.
This is my switch_rest_api.js
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var gpio = require('pi-gpio');
var app = express();
var mysql = require('mysql');
app.use(bodyParser.json());
app.set('port', process.env.PORT || 3030);
var conn = mysql.createConnection({
host: 'localhost',
user: 'user',
password: 'password',
database: 'rest_api'
});
app.get('', function(req, res) {
var pin = req.params.pin;
gpio.open(pin, 'input', function(err) {
if (pin === '12') {
//Set pin 12 as SWitch to turn on LED
gpio.write(12, 1, function(err) {
if (err) throw err;
else {
gpio.open(pin, 'output', function(err) {
gpio.write(7, 1, function(err) {
conn.query('INSERT INTO sensorLog(sensorId, logTime, sensorValue) VALUES(1, current_timestamp(), 1)', function(err){
if (err) throw err;
else {
console.log("Successfully store to mySQL database");
}
});
res.send(200);
gpio.close(7);
});
gpio.write(11, 1, function(err) {
conn.query('INSERT INTO sensorLog(sensorId, logTime, sensorValue) VALUES(2, current_timestamp(), 1)', function(err){
if (err) throw err;
else {
console.log("Successfully store to mySQL database");
}
});
res.send(200);
gpio.close(11);
});
gpio.write(13, 1, function(err) {
conn.query('INSERT INTO sensorLog(sensorId, logTime, sensorValue) VALUES(3, current_timestamp(), 1)', function(err){
if (err) throw err;
else {
console.log("Successfully store to mySQL database");
}
});
res.send(200);
gpio.close(13);
});
});
console.log("Switch is on");
}
res.send(200);
gpio.close(12);
});
}
});
});
var server = app.listen(app.get('port'), function() {
console.log('Listening on port %d', server.address().port);
});