Skip to content

Instantly share code, notes, and snippets.

@vettorazi
Forked from dugdaniels/index.html
Created May 20, 2018 19:38
Show Gist options
  • Save vettorazi/8d695f2b7697e37210481e3308c1987d to your computer and use it in GitHub Desktop.
Save vettorazi/8d695f2b7697e37210481e3308c1987d 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.
<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>
{
"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"
}
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