Skip to content

Commit

Permalink
Add experimental WebSockets support
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson Neves committed Aug 30, 2012
1 parent a6f1e8b commit 2737bc4
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 107 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ or (for quick testing)
npm start
```

Experimental testing with WebSockets protocol
```bash
sudo node server.js 8080 websockets
```

License
=======
Copyright (C) 2012 Nelson Neves
Expand Down
118 changes: 110 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var flatiron = require('flatiron'),
ecstatic = require('ecstatic'),
app = flatiron.app,
rpi_gpio = require('rpi-gpio'),
tcpport = 8080;
tcpport = 8080,
flag_use_websockets = false;

// Processing parameters
if(process.argv[2] !== undefined && process.argv[2].trim() !== '') {
Expand All @@ -18,6 +19,14 @@ if(process.argv[2] !== undefined && process.argv[2].trim() !== '') {
else {
console.log("Using default tcp/ip port: "+tcpport);
}
// websockets param
if(process.argv[3] !== undefined && process.argv[3].trim() !== '' && process.argv[3].trim() === 'websockets') {
flag_use_websockets = true;;
console.log("Use WebSockets protocol!");
}
else {
console.log("Using default Http REST protocol!");
}

// maps / exports gpio pins
rpi_gpio.setup(7, rpi_gpio.DIR_OUT, gpioWrite4);
Expand Down Expand Up @@ -97,13 +106,13 @@ app.http.before = [
// flatiron router - API for GCODE commands
app.router.get('/gpio/:cmd', function (cmd) {

console.log('\r\nParsing REST gpio command: '+cmd);
console.log('\r\nParsing REST gpio command: '+cmd);

// decode SPACE and ; chars (previously encoded in client)
var gpio = cmd.replace(/_/g, " ");
gpio = gpio.replace(/--/g, ";");
// decode SPACE and ; chars (previously encoded in client)
var gpio = cmd.replace(/_/g, " ");
gpio = gpio.replace(/--/g, ";");

console.log('Decoded gpio command: '+gpio);
console.log('Decoded gpio command: '+gpio);

// check if it contains a multiple gpio commands (will use ; as separator)
if(gpio.indexOf(";") != -1) {
Expand Down Expand Up @@ -203,17 +212,110 @@ app.router.get('/gpio/:cmd', function (cmd) {
this.res.writeHead(200, {'Content-Type':'text/plain'});
this.res.write('ACK');
this.res.end();
});
});

// launch app on tcpoprt
app.start(tcpport);
console.log('Raspberry Pi GPIO WebInterface Server running on port '+tcpport);

// verify if websockets is active
if(flag_use_websockets === true) {

var io = require('socket.io').listen(app.server);
io.sockets.on('connection', function(socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function(data) {
console.log(data);
});
socket.on('gpiodata', function(data) {
console.log('Received GPIO Data: '+data.wsdata);
var gpio0=data.wsdata;
console.log("GPIO="+gpio0);
if(gpio0 === "SET_GPIO_04")
{
gpioWrite4(true);
console.log("GPIO4_VALUE: true");
}
else if(gpio0 === "RESET_GPIO_04")
{
gpioWrite4(false);
console.log("GPIO4_VALUE: false");
}

if(gpio0 === "SET_GPIO_17")
{
gpioWrite17(true);
console.log("GPI17_VALUE: true");
}
else if(gpio0 === "RESET_GPIO_17")
{
gpioWrite17(false);
console.log("GPI17_VALUE: false");
}

if(gpio0 === "SET_GPIO_21")
{
gpioWrite21(true);
console.log("GPI21_VALUE: true");
}
else if(gpio0 === "RESET_GPIO_21")
{
gpioWrite21(false);
console.log("GPI21_VALUE: false");
}

if(gpio0 === "SET_GPIO_22")
{
gpioWrite22(true);
console.log("GPI22_VALUE: true");
}
else if(gpio0 === "RESET_GPIO_22")
{
gpioWrite22(false);
console.log("GPI22_VALUE: false");
}

if(gpio0 === "SET_GPIO_23")
{
gpioWrite23(true);
console.log("GPI23_VALUE: true");
}
else if(gpio0 === "RESET_GPIO_23")
{
gpioWrite23(false);
console.log("GPI23_VALUE: false");
}

if(gpio0 === "SET_GPIO_24")
{
gpioWrite24(true);
console.log("GPI24_VALUE: true");
}
else if(gpio0 === "RESET_GPIO_24")
{
gpioWrite24(false);
console.log("GPI24_VALUE: false");
}

if(gpio0 === "SET_GPIO_25")
{
gpioWrite25(true);
console.log("GPI25_VALUE: true");
}
else if(gpio0 === "RESET_GPIO_25")
{
gpioWrite25(false);
console.log("GPI25_VALUE: false");
}
});
});
}

// CTRL+C (sigint)
process.on( 'SIGINT', function() {
console.log("Unexport GPIO!");
gpioClose();

console.log( "Gracefully shutting down from SIGINT (Crtl-C)");
process.exit( )
});
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"union": "0.3.0",
"flatiron": "0.2.8",
"ecstatic": "0.1.6",
"rpi-gpio": "0.0.3"
"rpi-gpio": "0.0.3",
"socket.io": "0.9.10"
},
"devDependenices": {
"api-easy": "0.3.2",
Expand Down
26 changes: 26 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,35 @@
<button type="button" onclick="rpi_gpio.sendCmd('SET_GPIO_25');">Set Output 07</button><button type="button" onclick="rpi_gpio.sendCmd('RESET_GPIO_25');">Reset Output 07</button>
</div>

<div id="block02" class="block02">
<button type="button" onclick="socket.emit('gpiodata', { wsdata: 'SET_GPIO_04' });">Set Output 01 WS</button>
<button type="button" onclick="socket.emit('gpiodata', { wsdata: 'RESET_GPIO_04' });">Reset Output 01 WS</button><br>
<button type="button" onclick="socket.emit('gpiodata', { wsdata: 'SET_GPIO_17' });">Set Output 02 WS</button>
<button type="button" onclick="socket.emit('gpiodata', { wsdata: 'RESET_GPIO_17' });">Reset Output 02 WS</button><br>
<button type="button" onclick="socket.emit('gpiodata', { wsdata: 'SET_GPIO_21' });">Set Output 03 WS</button>
<button type="button" onclick="socket.emit('gpiodata', { wsdata: 'RESET_GPIO_21' });">Reset Output 03 WS</button><br>
<button type="button" onclick="socket.emit('gpiodata', { wsdata: 'SET_GPIO_22' });">Set Output 04 WS</button>
<button type="button" onclick="socket.emit('gpiodata', { wsdata: 'RESET_GPIO_22' });">Reset Output 04 WS</button><br>
<button type="button" onclick="socket.emit('gpiodata', { wsdata: 'SET_GPIO_23' });">Set Output 05 WS</button>
<button type="button" onclick="socket.emit('gpiodata', { wsdata: 'RESET_GPIO_23' });">Reset Output 05 WS</button><br>
<button type="button" onclick="socket.emit('gpiodata', { wsdata: 'SET_GPIO_24' });">Set Output 06 WS</button>
<button type="button" onclick="socket.emit('gpiodata', { wsdata: 'RESET_GPIO_24' });">Reset Output 06 WS</button><br>
<button type="button" onclick="socket.emit('gpiodata', { wsdata: 'SET_GPIO_25' });">Set Output 07 WS</button>
<button type="button" onclick="socket.emit('gpiodata', { wsdata: 'RESET_GPIO_25' });">Reset Output 07 WS</button><br>
</div>

<script type="text/javascript" src="javascripts/gpio.js"></script>
<script type="text/javascript">
var rpi_gpio = RPI.Gpio();
</script>

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://workbench01');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</body>
</html>
104 changes: 6 additions & 98 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,108 +8,16 @@ a {
color: #00B7FF;
}

.header {
background-color:#494949;
width:auto;
height:150px;
//text-align:center;
}

.header_image {
position:relative;
top:15px;
left:20px;

}

.block01 {

position:absolute;
left:20px;
top:20px;
width:400px;
}

.block02 {
position:absolute;
left:60px;
top:400px;
left:20px;
top:200px;
width:400px;
}

.block03 {
position:absolute;
left:60px;
top:350px;
width:100px;
height:40px;
background-color: #DEDEDE;
}

.block04 {
position:absolute;
left:160px;
top:350px;
width:140px;
height:40px;
background-color: #DEDEDE;
}

.block05 {
position:absolute;
left:300px;
top:350px;
width:130px;
height:40px;
background-color: #DEDEDE;
}

.xplus {
position:absolute;
left:220px;
top: 220px;
}

.xminus {
position:absolute;
left:80px;
top: 220px;
}

.yplus {
position:absolute;
left:147px;
top: 170px;
}

.yminus {
position:absolute;
left:147px;
top: 277px;
}

.zplus {
position:absolute;
left:350px;
top: 170px;
}

.zminus {
position:absolute;
left:350px;
top: 277px;
}

.home {
position:absolute;
left:142px;
top: 231px;
}

.jogmode {
width:85px;
}

.xyfeedrate {
width:125px;
}

.zfeedrate {
width:120px;
}

0 comments on commit 2737bc4

Please sign in to comment.