Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: pauly/robot-butler
base: 92a6333110
...
head fork: pauly/robot-butler
compare: df26b594c0
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 78 additions and 42 deletions.
  1. +2 −0  README
  2. +76 −42 server.js
View
2  README
@@ -1,3 +1,5 @@
Robot house experiments. OK so far I'm just turning LEDs on and off.
http://www.clarkeology.com/wiki/robot+house
+
+Requires http://nodejs.org/ and http://expressjs.com/ and https://github.com/ecto/duino
View
118 server.js
@@ -1,68 +1,102 @@
+/**
+ * Slightly messy arduino experiments using sockets.
+ *
+ * Requires http://nodejs.org/ and http://expressjs.com/ and https://github.com/ecto/duino
+ *
+ * @todo Templates
+ * @author PC <paul.clarke+paulclarke@holidayextras.com>
+ * @date Tue 21 Feb 2012 08:57:00 GMT
+ */
+
var port = 3000,
io = require('socket.io'),
express = require('express'),
- app = express.createServer(),
+ app = express.createServer( ),
io = io.listen(app),
arduino = require('duino'),
board,
- pins = [];
-// board = new arduino.Board( {
-// debug: true
-// } );
+ pins = { 11: null, 12: null, 13: null },
+ board = new arduino.Board( ),
+ list_of_pins = '<ul>';
+
+for ( var i in pins ) {
+ try {
+ pins[ i ] = new arduino.Led( {
+ board: board,
+ debug: true,
+ pin: i
+ } );
+ list_of_pins += '<li><a id="pin' + i + '" data-pin="' + i + '" href="/pin/' + i + '" data-bright="0">/pin/' + i + '</a></li>';
+ }
+ catch ( e ) { // probably no led plugged in
+ console.log( e );
+ }
+}
+list_of_pins += '</ul>';
io.sockets.on( 'connection', function ( socket ) {
- socket.on( 'message', function ( data ) {
- console.log( 'got an answer: ' + JSON.stringify( data ));
+ socket.on( 'pin', function ( data ) {
+ if ( typeof data.bright != 'undefined' ) {
+ switch_pin( data.pin );
+ }
} );
} );
-var htmlWrapper = function ( req, res, next ) {
+app.use( function ( req, res, next ) {
var send = res.send;
res.send = function ( foo ) {
res.send = send;
- res.send( '<html><head><style type="text/css">h1 { padding: 2em }</style><script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect("http://localhost:' + port + '"); socket.on("message", function (data) { console.log("triggered event message"); console.log(data); socket.emit("message", data ); var msg = document.createElement("p"); msg.innerHTML = JSON.stringify( data ); document.getElementsByTagName("body")[0].appendChild( msg ); }); </script> </head> <body> <h1>Try <a href="/led/12">/led/12</a> or <a href="/led/13">/led/13</a>.</h1> <div id="msgs"></div>' + foo + '</body></html>' );
+ res.send( '<html><head><style type="text/css">p, li { font-size: 2em, margin: 2em } .on { background-color: #0f0 } </style></head><body>' + list_of_pins + '<div id="msgs"></div>' + foo + '<script src="http://code.jquery.com/jquery-1.5.min.js"></script><script src="/socket.io/socket.io.js"></script> \
+ <script> \
+ $( function ( ) { \
+ var socket = io.connect("http://localhost:' + port + '"); \
+ if ( socket ) { \
+ socket.on( "message", function ( data ) { \
+ $("#msgs").prepend( "<p>" + JSON.stringify( data ) + "<\p>" ).find("p:gt(2)").remove( ); \
+ } ); \
+ socket.on( "pin", function ( data ) { \
+ var $pin = $("#pin" + data.pin).data( "bright", data.bright ); \
+ data.bright ? $pin.addClass( "on" ) : $pin.removeClass( "on" ); \
+ } ); \
+ $("a").each( function ( ) { \
+ /* quick test, get each pin, see if it is on - I still need to update the pin data, hmm */ \
+ socket.emit( "pin", { pin: $(this).data("pin") } ); \
+ } ).click( function ( ) { \
+ socket.emit( "pin", { pin: $(this).data("pin"), bright: ! $(this).data("bright") } ); \
+ return false; \
+ } ); \
+ } \
+ } ); \
+ </script></body></html>' );
}
- next();
-};
-app.use( htmlWrapper );
-
-var p = function ( ) {
- var r = '';
- for ( var i in arguments ) {
- r += '<h1>' + arguments[i] + '</h1>';
- }
- return r;
-};
-
+ next( );
+} );
-app.get( '/led/:pin(\\d{1,2})?', function( req, res, next ) {
- console.log( req.params );
- var pin = req.params.pin;
+var switch_pin = function ( pin, bright ) {
io.sockets.emit( 'message', 'Someone just switched pin ' + pin );
- if ( ! pins[ pin ] ) {
- try {
- pins[ pin ] = new arduino.Led( {
- board: board,
- debug: true,
- pin: pin
- } );
- }
- catch ( e ) {
- console.log( e );
- }
- }
if ( pins[ pin ] ) {
- console.log( pins[ pin ] );
+ if ( bright === undefined ) bright = ! pins[ pin ].bright;
pins[ pin ].bright ? pins[ pin ].off( ) : pins[ pin ].on( );
- res.send( p( 'LED ' + pin + ' is ' + ( pins[ pin ].bright ? 'on' : 'off' )));
- }
- else {
- res.send( p( 'No LED ' + pin ));
+ io.sockets.emit( "pin", { pin: pin, bright: pins[ pin ].bright } );
+ return get_pin( pin );
}
+ return false;
+};
+
+var get_pin = function ( pin ) {
+ return pins[ pin ].bright;
+};
+
+app.post( '/pin/:pin(\\d{1,2})?', function( req, res, next ) {
+ res.send( switch_pin( req.params.pin ));
+} );
+
+app.get( '/pin/:pin(\\d{1,2})?', function( req, res, next ) {
+ res.send( get_pin( req.params.pin ));
} );
app.all( /(.*)/, function( req, res ) {
- res.send( p( 'Custom 404 page, did not find ' + req.params[0] ));
+ res.send( '' );
} );
app.listen( port );

No commit comments for this range

Something went wrong with that request. Please try again.