Rasp Pi RGB Led Web Driver - Node js Script


Hi, finally I have time to post program ... :)
For this first phase I made a simple program with NodeJS language that listen on local port 8000 and drive RGB led strip by simple http request with string parameters. For drive the RGB led I made simple circuit that you can read in previous post.

In my last video below, you can see final build of raspberry prototype shield that it drive a 10W RGB led lamp.

 

Istruction:
  • Install nodejs  application, there are many guide on internet,
    on recent raspberry OS you can install it by apt

  • Install with npm  express and gpio library
    npm install express
    npm install gpio

  • Copy and paste the script below into a file rgbweb.js

  • on terminal execute  <prompt>$ node rgbweb,js

  • Now open web browser and insert this url 
    "http://<IP_raspberry>:8000/Lamp/1/0/1/0/1/0"

  • See what happen :)

In this other video you can see how command string work




This is my first js script and I thinked that now is easy add static html page with button to send specific string command. I'm working for this but before I want look how work socket.io library ;)

Below the program server.

============  rgbweb.js  ==============
//Controllo remoto via http x striscia led RGB
//Raspberry Pi2 by SgaboLab 09/2016 www.sgabolab.com


var express = require('express');
var webserver = express();
var Gpio = require('onoff').Gpio,
  ledR = new Gpio(16, 'out'),
  ledG = new Gpio(20, 'out'),
  ledB = new Gpio(21, 'out');
var timerR, timerG, timerB

// Reset iniziale led
  ledR.writeSync(Number(0));
  ledG.writeSync(Number(0));
  ledB.writeSync(Number(0));


webserver.get('/Lamp/:R/:G/:B/:LR/:LG/:LB', function(req, res){
  // chiamata get: /Lamp/0/0/0/0/0/0  0 spento 1 acceso/lampeggiante

    if (req.params.LR == 1){
        clearInterval(timerR);
        timerR = setInterval(blinkR, 1000);
    } else {
        clearInterval(timerR);
        ledR.writeSync(Number(req.params.R));
    }

    if (req.params.LG == 1){
        clearInterval(timerG);
        timerG = setInterval(blinkG, 1000);
    } else {
        clearInterval(timerG);
        ledG.writeSync(Number(req.params.G));
    }

    if (req.params.LB == 1){
        clearInterval(timerB);
        timerB = setInterval(blinkB, 1000);
    } else {
        clearInterval(timerB);
        ledB.writeSync(Number(req.params.B));
    }

    res.send('Ricevuto');
});


// funzioni timer per colore
function blinkR() { 
         var state = ledR.readSync();
         ledR.writeSync(Number(!state));
}

function blinkG() { 
         var state = ledG.readSync();
         ledG.writeSync(Number(!state));
}

function blinkB() { 
         var state = ledB.readSync();
         ledB.writeSync(Number(!state));
}


webserver.listen(8000);
console.log("Server running at http://127.0.0.1:8000/");


========== END rgbweb.js =============