首先,我不擅长说英语。
我正在尝试在Raspberry Pi中基于Fingersquared将代码编写为Node.js.
所以,我想控制我的RPi'GPIO。但是当我点击按钮时,console.log
显示得很慢或根本没有(但是当我第一次点击每个按钮时很快就会反应)
当我检查cpu率时,它是正常的(小于2%)。
我的错是什么? :(
app.js
var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path')
, gpio_led = require('./routes/led');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 52237);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
app.get('/led/:num/:switch', function(req,res){
var num = req.params.num
, sw = req.params.switch;
console.log('LED ' + num + ' is ' + sw + '.');
//gpio_led(num,sw);
})
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
index.jade
extends layout
block content
.row
.six.columns.centered
.panel
.row
h4.text-center LED_01
.six.columns.text-center
a.button.large.right.LED1_ON ON
.six.columns
a.button.large.left.LED1_OFF OFF
.row
hr
h4.text-center LED_02
.six.columns
a.button.large.right.LED2_ON ON
.six.columns
a.button.large.left.LED2_OFF OFF
.row
script(type='text/javascript')
$('.LED1_ON').on('click', function(){
$.get('/led/1/on');
});
$('.LED1_OFF').on('click', function(){
$.get('/led/1/off');
});
答案 0 :(得分:1)
向处理程序添加res.send('ok')
。浏览器正在等待对第一个按钮点击的响应,并且永远不会得到它。这可能是造成延误的原因。