manniru
5/30/2018 - 3:40 PM

Raspberry PI Socket IO

Raspberry PI Socket IO

var http = require('http').createServer(handler);
var fs = require('fs');
var io = require('socket.io')(http)
var Gpio = require('onoff').Gpio;

var LED1 = new Gpio(17, 'out');
var LED2 = new Gpio(27, 'out');
var LED3 = new Gpio(22, 'out');
var LED4 = new Gpio(23, 'out');
var LED5 = new Gpio(24, 'out');
var LED6 = new Gpio(26, 'out');
var LED7 = new Gpio(12, 'out');
var LED8 = new Gpio(16, 'out');

http.listen(8080);

function handler (req, res) {
  fs.readFile(__dirname + '/public/index.html', function(err, data) {
    if (err) {
      res.writeHead(404, {'Content-Type': 'text/html'});
      return res.end("404 Not Found");
    } 
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    return res.end();
  });
}

io.sockets.on('connection', function (socket) {

  var value1 = 0;
  var value2 = 0;
  var value3 = 0;
  var value4 = 0;
  var value5 = 0;
  var value6 = 0;
  var value7 = 0;
  var value8 = 0;

  socket.on('room1', function(data) {
    value1 = data;
    if (value1 != LED1.readSync()) {
      LED1.writeSync(value1);
    }
  });

  socket.on('room2', function(data) {
    value2 = data;
    if (value2 != LED2.readSync()) {
      LED2.writeSync(value2);
    }
  });

  socket.on('room3', function(data) {
    value3 = data;
    if (value3 != LED3.readSync()) {
      LED3.writeSync(value3);
    }
  });

  socket.on('room4', function(data) {
    value4 = data;
    if (value4 != LED4.readSync()) {
      LED4.writeSync(value4);
    }
  });

  socket.on('room5', function(data) {
    value5 = data;
    if (value5 != LED5.readSync()) {
      LED5.writeSync(value5);
    }
  });

  socket.on('room6', function(data) {
    value6 = data;
    if (value6 != LED6.readSync()) {
      LED6.writeSync(value6);
    }
  });

  socket.on('room7', function(data) {
    value7 = data;
    if (value7 != LED7.readSync()) {
      LED7.writeSync(value7);
    }
  });

  socket.on('room8', function(data) {
    value8 = data;
    if (value8 != LED8.readSync()) {
      LED8.writeSync(value8);
    }
  });

});

process.on('SIGINT', function () { //on ctrl+c
  LED1.writeSync(0);
  LED1.unexport();
  LED2.writeSync(0);
  LED2.unexport();
  LED3.writeSync(0);
  LED3.unexport();
  LED4.writeSync(0);
  LED4.unexport();
  LED5.writeSync(0);
  LED5.unexport();
  LED6.writeSync(0);
  LED6.unexport();
  LED7.writeSync(0);
  LED7.unexport();
  LED8.writeSync(0);
  LED8.unexport();
  //pushButton.unexport();
  process.exit();
});
<!DOCTYPE html>
<html>
<body>
<h2>Mannir Home Automation System</h2>
<p>
Room1: <input type="checkbox" id="room1" checked><br />
Room2: <input type="checkbox" id="room2"><br />
Room3: <input type="checkbox" id="room3"><br />
Room4: <input type="checkbox" id="room4"><br />
Room5: <input type="checkbox" id="room5"><br />
Room6: <input type="checkbox" id="room6"><br />
Room7: <input type="checkbox" id="room7"><br />
Room8: <input type="checkbox" id="room8"><br />
</p>


<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>
<script>
var socket = io();
window.addEventListener("load", function(){

  var room1 = document.getElementById("room1"); 
  room1.addEventListener("change", function() {
    socket.emit("room1", Number(this.checked));
  });

  var room2 = document.getElementById("room2"); 
  room2.addEventListener("change", function() {
    socket.emit("room2", Number(this.checked));
  });

  var room3 = document.getElementById("room3"); 
  room3.addEventListener("change", function() {
    socket.emit("room3", Number(this.checked));
  });

  var room4 = document.getElementById("room4"); 
  room4.addEventListener("change", function() {
    socket.emit("room4", Number(this.checked));
  });

  var room5 = document.getElementById("room5"); 
  room5.addEventListener("change", function() {
    socket.emit("room5", Number(this.checked));
  });

  var room6 = document.getElementById("room6"); 
  room6.addEventListener("change", function() {
    socket.emit("room6", Number(this.checked));
  });

  var room7 = document.getElementById("room7"); 
  room7.addEventListener("change", function() {
    socket.emit("room7", Number(this.checked));
  });

  var room8 = document.getElementById("room8"); 
  room8.addEventListener("change", function() {
    socket.emit("room8", Number(this.checked));
  });


});

socket.on('room1', function (data) {
  document.getElementById("room1").checked = data;
  socket.emit("room1", data);
});


socket.on('room2', function (data) {
  document.getElementById("room2").checked = data;
  socket.emit("room2", data);
});

socket.on('room3', function (data) {
  document.getElementById("room3").checked = data;
  socket.emit("room3", data);
});

socket.on('room4', function (data) {
  document.getElementById("room4").checked = data;
  socket.emit("room4", data);
});

socket.on('room5', function (data) {
  document.getElementById("room5").checked = data;
  socket.emit("room5", data);
});

socket.on('room6', function (data) {
  document.getElementById("room6").checked = data;
  socket.emit("room6", data);
});

socket.on('room7', function (data) {
  document.getElementById("room7").checked = data;
  socket.emit("room7", data);
});

socket.on('room8', function (data) {
  document.getElementById("room8").checked = data;
  socket.emit("room8", data);
});
</script>
</html>
</body>
</html>