Lux monitoring – LDR Arduino and Nodejs


Hello guys! How are you? I hope everything’s ok!

So, in this post I’ll try explain, how you can do the lux monitoring system, using:

– Arduino Uno,

Photo-resistor LDR,

Two led (green and red) to visual status indicator

Nodejs and chart.js module,  to show the lux range in real time.

Step 0 > Install Nodejs and NPM

To make this task you need install before the nodejs and npm

Step 1 > Electrical connections

See the electrical connections bellow:

protoboard

Step 2 > Installation of dependencies:

Create the folder the folder Node_LDR;

Into this folder create the packagejson.js:

Into the packagejson.js file, write the code below:

{
    "name": "LDR_Aduino_Nodejs",
    "version": "0.0.1",
    "dependencies": {
      "chart.js": "^2.2.1",
      "express": "^4.14.0",
      "serialport": "^4.0.1",
      "socket.io": "^1.4.8"
    }
 }

Save and close!

After, open your prompt command and run npm install:

npminstall

When the installation is finished look into the folder   Node_LDR:

– Node_LDR

— node_modules

— package.json

If your folder it’s the same that format, everything’s ok!!

Step 3 > Installation of Arduino IDE:

Click here and install the IDE

IDE_Arduino

Step 4 > C++ code:

Open your arduino IDE and write the follow code:

int portaledred = 10;
int portaledgreen = 11;
int pinoport = 5;
int valorport = 0;
float luminosidade = 0;

void setup() {
 Serial.begin(9600);
 pinMode(portaledred, OUTPUT);
 pinMode(portaledgreen, OUTPUT);
}

void loop() {
 valorport = analogRead(pinoport);

 luminosidade = map(valorport, 0, 1023, 0, 255);
 Serial.print("Valor do LDR : ");
 Serial.println(valorport);
 Serial.print(" = Luminosidade : ");
 Serial.println(luminosidade);
 

 if (valorport > 900)
 {
 digitalWrite(portaledred, HIGH);
 digitalWrite(portaledgreen, LOW);
 }
 else
 {
 digitalWrite(portaledred, LOW);
 digitalWrite(portaledgreen, HIGH);
 }
delay(3000);
}

Make Upload the code on Arduino board:

uploadArd

After the upload is done make a test

Step 5 > Communication between Arduino, Nodejs and Web page:

Open the text editor of your preference and create the app.js with code below:

var app = require('express')();
var express = require('express');

app.use(express.static(__dirname + '/public'));

var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get("/", function(req, res){
 res.sendFile("/cygwin64/home/pablo.binotto/node/arduino/index.html");//here is necessary the all url of folder into the c:/
});

var mySocket;

var serialport = require('serialport');
var SerialPort = serialport.SerialPort;

var mySerial = new SerialPort("COM5", {
 baudrate: 9600,
 parser: serialport.parsers.readline("\n")
});

mySerial.on('open', function(){
 console.log("Porta aberta.");
});

mySerial.on("data", function(data){
 //console.log(dados);
 io.emit("dadoArduino", {
 valor: data
 });
});

io.on("connection", function(socket){
 console.log("Usuario conectado")
});

http.listen(3000, function(){
 console.log("Server running on 3000 port");
});

Save this file on Node_LDR folder and on the same folder, create the file index.html follow the code below:

<!DOCTYPE html>
<html lang="">
 <head>
 <meta charset="utf-8">
 <meta http-equiv="X-UA-Compatiblr" content="IE=edge">
 <meta name= "viewport" content="width=device-width, initial-scale=1">
 <title>Node_Arduino_IO_SerialPort</title>
 <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20src%3D%22%2Fsocket.io%2Fsocket.io.js%22%3E%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />
 </head>
 <body>
 <canvas id="myChart" width="400" height="400"></canvas>
 <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%3D%22text%2Fjavascript%22%3E%0A%20var%20ctx%20%3D%20document.getElementById(%22myChart%22)%3B%0A%20%0A%20var%20myLineChart%20%3D%20new%20Chart(ctx%2C%20%7B%0A%20type%3A%20'line'%2C%0A%20data%3A%20data%2C%0A%20options%3A%20options%0A%20%7D)%3B%0A%20%0A%20%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />
 <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%3D%22text%2Fjavascript%22%3E%0A%20var%20socket%20%3D%20io()%3B%0A%20socket.on(%22dadoArduino%22%2C%20function(dado)%7B%0A%20console.log(dado)%3B%0A%20%7D)%3B%0A%20%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />
 </body>
</html>

On your prompt command write: node app.js and click enter…

Open a new web page and go to localhost:3000…

In this video below, you can see the communication happening

Step 6 > Show the result on line graph into the web page:

See you again!