LED Switch REST API

时间:2014-08-01 08:18:17

标签: mysql node.js rest raspberry-pi gpio

我想在我的LED Switch REST API上寻求帮助。我创建了自己的REST API,并且在输入URL时能够点亮我的LED,例如:localhost:3030/7/1点亮localhost:3030/7/0点亮即可。但是,当我开始实现切换到手动打开和关闭我的LED而不键入URL 时,我的整个REST API能够启动,但是当我点击我的开关时没有LED亮起。

这是我的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);
});

0 个答案:

没有答案