0

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

This is my circuit screenshots:

enter image description here

4
  • what is the error message?
    – rob
    Aug 1 '14 at 8:58
  • It keep prompting me unexpected token }); and I tried to follow it before, in the end it broke all my coding logic and I'm afraid now.
    – niji
    Aug 1 '14 at 9:16
  • it looks like every time you call gpio.close you have added an unneeded '});'. But this isn't really a Pi specific question, it would be better asked on StackOverflow.com
    – rob
    Aug 1 '14 at 10:14
  • Yes I did, rob but not much luck.
    – niji
    Aug 5 '14 at 9:08
2

I've re-indented your code, and you make a mistake on two lines (at line 68 and 117). You wrote :

});

Whereas the correct way is :

);}

Here is your file with the fix ;) :

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');
var dateFormat = require('dateformat');

app.use(bodyParser.json());
app.set('port', process.env.PORT || 3030);

dateFormat(now);

var now = new Date().toLocaleString();

var conn = mysql.createConnection({
    host: 'localhost',
    user: 'user',
    password: 'password',
    database: 'rest_api'
});

app.get('/:pin/1', function(req, res) {
    var pin = req.params.pin;

    gpio.open(pin, 'input', function(err) {
        //Set switch for turning on LED as pin 12
        if (pin === '12') {
            gpio.open(pin, 'output', function(err) {
                if (pin === '7') {
                    //Set pin 7 high (1)
                        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, now);
                        gpio.close(7);
                });
                } if (pin === '11') {
                    //Set pin 11 high (1)
                        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, now);
                            gpio.close(11);
                    });
                } if (pin === '13') {
                    //Set pin 13 high (1)
                        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, now);
                            gpio.close(13);
                    });
                }
            });
        };
    });
});

app.get('/:pin/0', function(req, res) {
    var pin = req.params.pin;

    gpio.open(pin, 'input', function(err) {
        //Set switch for turning off LED as pin 15
        if (pin === '15') {
            gpio.open(pin, 'output', function(err) {
                if (pin === '7') {
                    //Set pin 7 low (0)
                        gpio.write(7, 0, function(err) {
                        conn.query('INSERT INTO sensorLog(sensorId, logTime, sensorValue) VALUES(1, current_timestamp(), 0)', function(err){
                                if (err) throw err;
                                    else {
                                    console.log("Successfully store to mySQL database");
                                }
                            });
                        res.send(200, now);
                        gpio.close(7);
                        });
                } if (pin === '11') {
                    //Set pin 11 low (0)
                        gpio.write(11, 0, function(err) {
                        conn.query('INSERT INTO sensorLog(sensorId, logTime, sensorValue) VALUES(2, current_timestamp(), 0)', function(err){
                            if (err) throw err;
                                else {
                                console.log("Successfully store to mySQL database");
                            }
                        });
                        res.send(200, now);
                            gpio.close(11);
                    });
                } if (pin === '13') {
                    //Set pin 13 low (0)
                        gpio.write(13, 0, function(err) {
                        conn.query('INSERT INTO sensorLog(sensorId, logTime, sensorValue) VALUES(3, current_timestamp(), 0)', function(err){
                            if (err) throw err;
                                else {
                                console.log("Successfully store to mySQL database");
                            }
                        });
                        res.send(200, now);
                            gpio.close(13);
                    });
                }
            });
        }
    });
});

var server = app.listen(app.get('port'), function() {
  console.log('Listening on port %d', server.address().port);
});
1
  • No more errors but no LED light up when I click my button manually, Ashbay
    – niji
    Aug 2 '14 at 3:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.