augx3
Posts: 13
Joined: Tue Dec 10, 2019 11:49 am

Accendere e spegnere un led con Node.js,Websockets

Thu Jan 02, 2020 2:40 pm

Buongiorno a tutti,

Sto cercando di fare ciò che ho detto nel titolo, in pratica avrei un led,un pulsante per accenderlo e uno per spegnerlo,so che potrei fare tutto con un singolo pulsante ma ho la necessità di farlo con 2 pulsanti alla fine della favola quindi preferisco fissare le fondamenta giuste.

attualmente non sono pratico di websockets quindi quello che ho fatto è stato copiare le stringhe da W3Schools :
https://www.w3schools.com/nodejs/nodejs ... socket.asp

ed effetivamente fatto così funziona,tuttavia non ci capisco molto o meglio,non capisco cosa ci sta di fisso e quali invece sono le variabili nel codice,se qualcuno me lo potesse spiegare sarebbe una bella spinta

inoltre posto i codici che ho toccato con le mie sporche mani:

HTML:

Code: Select all

<!DOCTYPE html>
<html>
<body>
<h1>Control LED ON</h1>
<p><input type="checkbox" id="chkbx1"></p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script> <!-- include socket.io client side script -->
<script>

var socket = io(); //load socket.io-client and connect to the host that serves the page (static part i guess)
window.addEventListener("load", function(){ //when page loads (static part i guess)
  var lightbox = document.getElementById("chkbx1"); //(define what happens when i check the box)
  lightbox.addEventListener("change", function() { //(define what happens when i check the box)
    socket.emit("ON", Number(this.checked)); //send button status to server (as 1 or 0)
  });
});
socket.on('ON', function (data) { //get button status from client (receive the socket ON from server)
  document.getElementById("chkbx1").checked = data; //change checkbox according to push button on Raspberry Pi (due to the received socket)
  socket.emit("ON", data); //send push button status to back to server (no clue about that)
});

</script>
</html>
</body>
</html>
Webserver:

Code: Select all

var http = require('http').createServer(handler);
var fs = require('fs');
var io = require('socket.io')(http)
var Gpio = require('onoff').Gpio;
var Rel1 = new Gpio(27, 'out'); //(im defining GPIO 27 as output,variablle Rel1)
var pushButton1OP = new Gpio(5, 'in', 'both');//(im defining GPIO 5 as input,variablle pushButton1OP)


http.listen(8080); //(listening on ip:port)

function handler (req, res) {  //(website part)
  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) {// WebSocket Connection (static i guess)
  var lightvalue1 = 0; //static variable for current status (variable for led on/off)
  pushButton1OP.watch(function (err, value) { //Watch for hardware interrupts on pushButton (static i guess)
    if (err) { //if an error (static i guess)
      console.error('There was an error', err); //output error message to console
      return;
    }
    lightvalue1 = data; //(why is there data? i guess cause it is a value that has to change)
    socket.emit('ON', lightvalue1); //send button status to client
  });
  socket.on('ON', function(data) { //get light switch status from client (receives socket when i click on webpage)
    lightvalue1 = data;
    if (lightvalue1 != Rel1.readSync()) { //only change LED if status has changed
      Rel1.writeSync(lightvalue1); //turn LED on or off
    }
  });
});


process.on('SIGINT', function () { //on ctrl+c
  Rel1.writeSync(0); // Turn LED off
  pushButton1OP.unexport(); // Unexport Button GPIO to free resources
  process.exit(); //exit completely
});

Grazie anche solo per l`attenzione.

Return to “Italiano”