Skip to content

Instantly share code, notes, and snippets.

Created August 21, 2013 22:08
Show Gist options
  • Save anonymous/6300898 to your computer and use it in GitHub Desktop.
Save anonymous/6300898 to your computer and use it in GitHub Desktop.
Estos son los componentes del aplicativo que controla nuestro sistema de switch por relay. @espumalab
var express = require('express')
, app = express()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server)
, fs = require('fs')
, path = require('path')
, gpio = require('rpi-gpio')
server.listen(8000);
app.configure(function() {
app.use(express.static(path.join(__dirname, 'public')));
});
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
gpio.on('change', function(channel, value) {
console.log('Channel ' + channel + ' value is now ' + value);
});
//gpio.setup(4, gpio.DIR_IN);
gpio.setup(7, gpio.DIR_OUT);
gpio.setup(11, gpio.DIR_OUT);
gpio.setup(12, gpio.DIR_OUT);
gpio.setup(16, gpio.DIR_OUT);
gpio.setup(18, gpio.DIR_OUT);
gpio.setup(22, gpio.DIR_OUT);
function write(port, value, socket) {
console.log(port + " " + value);
switch (port) {
case '11':
gpio.write(12, value, function() {
socket.broadcast.emit('statusChanged', {port: 12, value: value, id: port});
console.log('PIN 12 changed to ' + value);
});
break;
case '12':
gpio.write(22, value, function() {
socket.broadcast.emit('statusChanged', {port: 22, value: value, id: port});
console.log('PIN 22 changed to ' + value);
});
break;
case '21':
gpio.write(7, value, function() {
socket.broadcast.emit('statusChanged', {port: 7, value: value, id: port});
console.log('PIN 7 changed to ' + value);
});
break;
case '22':
gpio.write(18, value, function() {
socket.broadcast.emit('statusChanged', {port: 18, value: value, id: port});
console.log('PIN 18 changed to ' + value);
});
break;
case '31':
gpio.write(11, value, function() {
socket.broadcast.emit('statusChanged', {port: 11, value: value, id: port});
console.log('PIN 11 changed to ' + value);
});
break;
case '32':
gpio.write(16, value, function() {
socket.broadcast.emit('statusChanged', {port: 16, value: value, id: port});
console.log('PIN 16 changed to ' + value);
});
break;
}
}
io.sockets.on('connection', function (socket) {
socket.on('write', function (data) {
write(data.port, data.value, socket);
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Domótica</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!--link rel="stylesheet/less" href="less/bootstrap.less" type="text/css" /-->
<!--link rel="stylesheet/less" href="less/responsive.less" type="text/css" /-->
<!--script src="js/less-1.3.3.min.js"></script-->
<!--append ‘#!watch’ to the browser URL, then refresh the page. -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<![endif]-->
<!-- Fav and touch icons -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="img/apple-touch-icon-144-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="img/apple-touch-icon-114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="img/apple-touch-icon-72-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="img/apple-touch-icon-57-precomposed.png">
<link rel="shortcut icon" href="img/favicon.png">
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/scripts.js"></script>
<script type="text/javascript" src="js/iphone-style-checkboxes.js"></script>
</head>
<body>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div class="navbar">
<div class="navbar-inner">
<div class="container-fluid">
<a data-target=".navbar-responsive-collapse" data-toggle="collapse" class="btn btn-navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a> <a href="#" class="brand">Domótica - RPi</a>
<div class="nav-collapse collapse navbar-responsive-collapse">
<ul class="nav pull-right">
<li>
<a href="http://www.nethexa.com" target="_blank">Grupo NetHexa</a>
</li>
<li>
<a href="http://espuma.co" target="_blank">EspumaLab</a>
</li>
<li class="divider-vertical">
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="row-fluid">
<div class="span9" style="text-align:center;">
<div class="row-fluid">
<div class="span6">
<h3 contenteditable>
1x1
</h3>
<input type="checkbox" value="11">
</div>
<div class="span6">
<h3 contenteditable>
1x2
</h3>
<input type="checkbox" value="12">
</div>
</div>
<div class="row-fluid">
<div class="span6">
<h3 contenteditable>
2x1
</h3>
<input type="checkbox" value="21">
</div>
<div class="span6">
<h3 contenteditable>
2x2
</h3>
<input type="checkbox" value="22">
</div>
</div>
<div class="row-fluid">
<div class="span6">
<h3 contenteditable>
3x1
</h3>
<input type="checkbox" value="31">
</div>
<div class="span6">
<h3 contenteditable>
3x2
</h3>
<input type="checkbox" value="32">
</div>
</div>
<div class="row-fluid">
<div class="span12" style="margin-top:100px;">
<h4>Powered by</h4>
<div><img src="img/logo150.png" style="height:75px;"><img src="img/Espuma.png" style="height:125px;"></div>
</div>
</div>
</div>
<div class="span3">
<textarea id="log" disabled></textarea>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function() {
var socket = io.connect('http://192.168.10.147:8000/');
socket.on('statusChanged', function(data) {
$('#log').prepend("PIN " + data.port + " valor " + data.value + "\n");
var checked = $(':checked[value='+data.id+']').is(':checked');
if (data.value == true && checked == false) {
console.log("turn on");
$(':checked[value='+data.id+']').trigger('click');
} else if (data.value == false && checked == true) {
console.log("turn off");
$(':checked[value='+data.id+']').trigger('click');
}
});
function sendSignal(elem, value) {
socket.emit('write', {port: elem[0].defaultValue, value: value});
}
$(':checkbox').iphoneStyle({ onChange : function(elem, value) { sendSignal(elem, value); } });
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment