Created
January 7, 2013 00:34
-
-
Save dugdaniels/4471350 to your computer and use it in GitHub Desktop.
An example of a browser-based input for Johnny-Five. Clicking the button in index.html turns on and off an LED installed on the Arduino board.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "johnny-five-io", | |
"version": "0.0.0", | |
"description": "Browser interface for Johnny-Five", | |
"main": "script.js", | |
"dependencies": { | |
"johnny-five": "~0.5.14", | |
"socket.io": "~0.9.13" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"repository": "", | |
"author": "Corey Daniels", | |
"license": "BSD" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Daphne98 Ill try to cover a few bases just in case you are new like me.
this line like this
board = new five.Board({port: "COM5"});
<<obviously your COM maybe different if you have lots of devicesnpm install http socket.io fs johnny-five
to get all the packages you need or if you are not sure you have them.node scripts.js
<<this will establish the connection to the board and start the web page.Hopefully this helped