无法通过单个 html 文件和强尼五号控制 LED
Can't control the LED by a single html file and johnny five
我是这方面的新手。我找到了一些代码来控制由单个 html 文件引导的 Arduino。他们说一定要用johnny-five和node-js协议来控制。但是我发现这种方式有问题,我成功连接到Arduino并打开了本地主机。但是,我只找到了几个按钮,控制led没用。我试图解决它,但什么也没发生。
这是我的源代码
The code
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
$(document).ready(function() {
var socket = io.connect('http://localhost');
$('#button').click(function(e){
socket.emit('click');
e.preventDefault();
});
});
</script>
</head>
<body>
<button id="button" href="#">LED ON/OFF</button>
</body>
</html>
和
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs'),
five = require('johnny-five');
app.listen(8080);
function handler(req, res) {
fs.readFile(__dirname + '/index.html',
function(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
board = new five.Board();
board.on("ready", function() {
led = new five.Led(13);
io.sockets.on('connection', function(socket) {
socket.on('click', function() {
led.toggle();
});
});
});
您 运行 您的服务器在端口 '8080' 上,但您在默认端口 (80) 上连接到 socket.io。我相信这可能是问题所在。尝试将 html 中的行更改为:
var socket = io.connect('http://localhost:8080');
此外,如果您的浏览器控制台出现任何错误,显示此消息会很有帮助。
我是这方面的新手。我找到了一些代码来控制由单个 html 文件引导的 Arduino。他们说一定要用johnny-five和node-js协议来控制。但是我发现这种方式有问题,我成功连接到Arduino并打开了本地主机。但是,我只找到了几个按钮,控制led没用。我试图解决它,但什么也没发生。
这是我的源代码 The code
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
$(document).ready(function() {
var socket = io.connect('http://localhost');
$('#button').click(function(e){
socket.emit('click');
e.preventDefault();
});
});
</script>
</head>
<body>
<button id="button" href="#">LED ON/OFF</button>
</body>
</html>
和
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs'),
five = require('johnny-five');
app.listen(8080);
function handler(req, res) {
fs.readFile(__dirname + '/index.html',
function(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
board = new five.Board();
board.on("ready", function() {
led = new five.Led(13);
io.sockets.on('connection', function(socket) {
socket.on('click', function() {
led.toggle();
});
});
});
您 运行 您的服务器在端口 '8080' 上,但您在默认端口 (80) 上连接到 socket.io。我相信这可能是问题所在。尝试将 html 中的行更改为:
var socket = io.connect('http://localhost:8080');
此外,如果您的浏览器控制台出现任何错误,显示此消息会很有帮助。