webok of things

WEBOK OF THINGS

Articolo 1: Come implementare l’https nella rete casalinga ( wifi di casa )

Ecco il codice per il progetto.


/*   

Comando per generare il keystore, utilizzando keytool: Il computer si chiama "popolo" nella rete locale. E' individuabile anche con il nome "popolo.lan" e corrisponde, attualmente, all'ip 192.168.1.228

keytool -genkeypair -keystore popolo.pfx -dname "CN=popolo" -storepass miapasswordsegreta -keypass miapasswordsegreta -alias popolo -validity 10000 -keyalg RSA -sigalg SHA256withRSA -keysize 2048 -ext san=dns:popolo.lan


*/

const porta = 443; // porta "standard" per https

const hostname = 'popolo.lan';

var fs = require('fs');

var contenuto;

var carattereinviare;

var mypassphrase = 'miapasswordsegreta';

var messaggioarduino = "";

const options = {
  pfx: fs.readFileSync('./popolo.pfx'),
  passphrase: mypassphrase,
                };

var { SerialPort, SerialPortMock, path, port } = require('serialport');

var port = new SerialPort({ path: "COM3", baudRate: 9600, autoOpen: true });

var c1 = "<html><body><br><br><textarea rows='15' cols='20' >";

var c2 = "</textarea><br><br><a href='/leggiporta' ><button> leggi porta seriale </button></a><br><br><a href='/spegni' ><button> spegni led </button></a><br><br><a href='/accendi' ><button> accendi led </button></a><br><br></body><html>";

var https = require('https');

https.createServer(options, function (richiesta, risposta) {

carattereinviare = 'x';

                   risposta.writeHead(200, {'Content-Type': 'text/html'});

    // console.log(richiesta.url);

// console.log(risposta);

     if ( richiesta.url == "/accendi" ) { 

     carattereinviare = 'A';


                                        };


     if ( richiesta.url == "/spegni" ) {

    carattereinviare = 'S'; 

   
      
                                       };

      

     if ( richiesta.url == "/leggiporta" ) { messaggioarduino = messaggioarduino + port.read() + "\n"; };

port.write(carattereinviare);

messaggioarduino = messaggioarduino + port.read() + "\n";

contenuto = c1 + messaggioarduino + c2;

risposta.end(contenuto);


}).listen(porta,hostname);