Waner Silva
Published © GPL3+

Online Monitoring For Smart Home

This system measure the power consumption of appliances plugged in outlet and can control turn on/off of some appliances.

AdvancedWork in progressOver 2 days3,755
Online Monitoring For Smart Home

Things used in this project

Hardware components

STM32F4DISCOVERY
×1
LV 20-P - Voltage Transducer of effect hall
×1
LA 55-P - Current Transducer
×1
Relay Module 5V / 4 Channel Board Shield
I used 4 channel
×1
12 Volt Power Supply
these power supply are for supply the transductors
×2

Software apps and online services

Node.js
Run on Intel Galileo
coIDE
Programming the stm32f4 discovery
jQuery Charts Plugin
criate the charts on the view page

Story

Read more

Custom parts and enclosures

img-20170127-wa0004_Mq88ZLZFBF.jpg

Schematics

circuit condictionament

system diagram

Code

smartgrid.js - webserver

JavaScript
this script will run with node.js in intel galileo
In node.js you should have installed:
-socket.io
-ejs
-express
-galileo-io
-serialport
var express = require("express");
var app = express();
app.set('view engine', 'ejs');
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var fs = require('fs');
var mraa = require('mraa');

Gpio_2 = new mraa.Gpio(2);
Gpio_3 = new mraa.Gpio(3);
Gpio_4 = new mraa.Gpio(4);
Gpio_5 = new mraa.Gpio(5);
Gpio_6 = new mraa.Gpio(6);
Gpio_7 = new mraa.Gpio(7);


Gpio_2.dir(mraa.DIR_OUT);
Gpio_3.dir(mraa.DIR_OUT);
Gpio_4.dir(mraa.DIR_OUT);
Gpio_5.dir(mraa.DIR_OUT);
Gpio_6.dir(mraa.DIR_IN);
Gpio_7.dir(mraa.DIR_IN);

Gpio_2.write(0);
Gpio_3.write(0);
Gpio_4.write(0);
Gpio_5.write(0);

An_0 = new mraa.Aio(0);
An_1 = new mraa.Aio(1);



var intervalHandler;
var dados_send = {v0: 0, v1: 0, v2: 0, v3: 0, v4: 0, v5: 0, v6: 0, v7: 0};

var com = require('serialport');
var serialPort = new com.SerialPort("/dev/ttyS0", {
                baudrate: 115200,
                dataBits: 8,
                parity: 'none',
                stopBits: 1,
                flowControl: false,
                parser: com.parsers.readline('\n')
});

serialPort.on('open', function(){
                console.log('\nSerial OK\n');
});

serialPort.on('data', function(data){
                var str = data.toString();
                var vet = str.split('x');
                dados_send.v0 = Math.sqrt(parseFloat(vet[0]));
                dados_send.v1 = Math.sqrt(parseFloat(vet[1])*0.001);
                dados_send.v2 = parseFloat(vet[2]);
                dados_send.v3 = Math.sqrt(Math.abs(parseFloat(vet[3])*0.001))*dados_send.v0;
                dados_send.v4 = Math.sqrt(Math.abs(parseFloat(vet[4])*0.001))*dados_send.v0;
                //console.log(dados_send);
                /*
                if(str.indexOf("V") == 0){
                                dados_send.v0 = parseFloat(str.replace("V","0"))/10.0;
                }else if(str.indexOf("I") == 0){
                                dados_send.v1 = parseFloat(str.replace("I","0"))/1.0;
                }else if(str.indexOf("P") == 0){
                                dados_send.v2 = parseFloat(str.replace("P","0"))/10.0;
                }else if(str.indexOf("Q") == 0){
                                dados_send.v3 = parseFloat(str.replace("Q","0"))/10.0;
                }else if(str.indexOf("D") == 0){
                                dados_send.v4 = parseFloat(str.replace("D","0"))/1.0;
                }
                */


});

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

app.get('/', function(request, response) {
                response.render('smartgrid', {
                                title: '',
                                message: ''
                });
});

app.get('/set', function(request, response) {
                response.render('smartgrid', {
                                title: '',
                                message: ''
                });
});


io.on('connection', function(socket) {
                socket.on('cmd_general',function(dados){
                        console.log('teste');
                        console.log(An_0.read());
                        console.log(Gpio_6.read());
                        fs.appendFile('leituras.txt','Nova linha ' + An_0.read()+ '\n');

                });
                 socket.on('cmd_rele',function(dados){
                         //console.log(dados);
                         Gpio_2.write(dados.r1);
                         Gpio_3.write(dados.r2);
                         Gpio_4.write(dados.r3);
                         Gpio_5.write(dados.r4);
                         //console.log(fs.readFileSync('leituras.txt')+"");
                        //serialPort.write("teste", function(){});
                });
});


var intervalFunction = function(){
                fs.readFile('/sys/class/thermal/thermal_zone0/temp','utf8',function(err,data){
                           dados_send.v5 = data.replace('\n','');

                });

                io.emit('data',dados_send);
};

function isr_Gpio_7(){
        console.log('Interrupcao');
        if(Gpio_7.read()){
                //io.emit('isr_gpio_7',dados_send);
        }
}
Gpio_7.isr(mraa.EDGE_BOTH,isr_Gpio_7);

intervalHandler = setInterval(intervalFunction,1999);
server.listen(8080);
console.log("\nServidor OK\n");

smartgrid.ejs - page view

HTML
<!DOCTYPE HTML>
<html>
<head>
        <script type="text/javascript" src="canvasjs.min.js"></script>
        <script type="text/javascript" src="/socket.io/socket.io.js"></script>

        <script type="text/javascript">
        window.onload = function () {
                var time = new Date;
                //var socket = io();
                var corrente = [
                                {x: time.getTime() , y: 0}


                                ];
                var tensao = [
                                {x: time.getTime() , y: 0}

                                ];

                var potencia = [
                {label: "S(VA)", y: 50},
                {label: "P(W)", y: 47},
                {label: "Q(VAR)", y: 55},
                {label: "D(VAR)", y: 51},
                {label: "Temp", y: 0}

                ];

                var pot_name = ["S(VA)", "P(W)", "Q(VAR)", "D(VAR)"];


                var chart_VI_2 = new CanvasJS.Chart("chartContainerVI_2",
                {
                        animationEnabled: true,
                        theme: "theme3",
                        title:{
                                text: "Monitoramento"
                        },
                        axisY:[{
                                lineColor: "#4F81BC",
                                tickColor: "#4F81BC",
                                labelFontColor: "#4F81BC",
                                titleFontColor: "#4F81BC",
                                labelFontStyle: "normal",
                                labelFontWeight: "normal",
                                labelFontFamily: "Arial",
                                  lineThickness: 2,
                                  //minimum:
                                  //maximum:
                                  //interval:
                                  title: "Tensao (V)",
                                },
                                {
                                lineColor: "#C0504E",
                                tickColor: "#C0504E",
                                labelFontColor: "#C0504E",
                                titleFontColor: "#C0504E",
                                labelFontStyle: "normal",
                                labelFontWeight: "normal",
                                labelFontFamily: "Arial",
                                lineThickness: 2,
                                title: "Corrente (mA)",

                        }],
                        data: [
                                {
                                type: "spline", //change type to bar, line, area, pie, etc
                                showInLegend: true,
                                name: "Tensao",
                                xValueType: "dateTime",
                                markerSize: 0,
                                dataPoints: tensao
                                },
                                {
                                type: "spline",
                                axisYIndex: 1,
                                showInLegend: true,
                                name: "Corrente mA",
                                xValueType: "dateTime",
                                markerSize: 0,
                                dataPoints: corrente
                                }
                        ],
                        legend: {
                                cursor: "pointer",
                                itemclick: function (e) {
                                        if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
                                                e.dataSeries.visible = false;
                                        } else {
                                                e.dataSeries.visible = true;
                                }
                                chart_VI_2.render();
                                }
                        }
                });


                chart_VI_2.render();


                var chart_potencia_2 = new CanvasJS.Chart("chartContainerPower_2", {
                        title:{
                                text:"Potencia"

                        },
                        animationEnabled: true,
                        axisX:{
                                gridThickness: 0,
                                labelFontSize: 12,
                                labelFontStyle: "normal",
                                labelFontWeight: "normal",
                                labelFontFamily: "Arial"


                        },
                        axisY2:{
                                minimum: 0,
                                maximum: 1000,
                                //interlacedColor: "rgba(1,77,101,.2)",
                                gridColor: "rgba(1,77,101,.1)"

                        },

                        data: [
                        {
                                type: "bar",
                                name: "power",
                                axisYType: "secondary",
                                color: "#014D65",
                                dataPoints: potencia
                        }

                        ]
                });

                chart_potencia_2.render();

                //Conexao servidor
                var newV = 0;
                var newI = 0;
                var newP = 0;
                var newQ = 0;
                var newD = 0;
                var newCPU_temp = 9;
                var currentTime = 1000;



                //time.setHours(9);
                //time.setMinutes(30);
                //time.setSeconds(00);
                //time.setMilliseconds(00);

                socket.on('data', function(dados) {
                                newV = (dados.v0);
                                newI = (dados.v1)*1000;
                                newP = (dados.v2);
                                newQ = (dados.v3);
                                newD = (dados.v4);
                                newCPU_temp = dados.v5/1000;
                                //$("#tempcpu").val(cpu);
                                //$("#p_s").val(msg.v0*msg.v1/1000);
                                //$("#p_p").val(msg.v2);
                                //$("#p_q").val(msg.v3);
                                //$("#p_d").val(msg.v4*msg.v1/1000);

                });

                socket.on('isr_dpin7', function(msg){
                                alert('Evento');
                });
                var boilerColor;
                var updateInterval = 1000;
                var updateChart = function () {
                        time.setTime(time.getTime()+ updateInterval);
                        currentTime = currentTime + 1000;
                        tensao.push({x: time.getTime(), y: newV});
                        corrente.push({x: time.getTime(), y: newI});
                        if (tensao.length > 59 ){
                                tensao.shift();
                                corrente.shift();
                        }


                        //Atualizacao potencia
                        var yVal = 0;
                        for (var i = 0; i < potencia.length-1; i++) {

                                // generating random variation deltaY
                                if(i == 0)
                                        yVal = Math.sqrt(newQ*newQ+newD*newD+newP*newP) ;
                                // color of dataPoint dependent upon y value.
                                if(i == 1)
                                        yVal = newP;
                                if(i == 2)
                                         yVal = newQ;
                                if(i == 3)
                                         yVal = newD;


                                potencia[i] = {label: pot_name[i] , y: yVal, color: "#6B8E23"};

                        };
                        boilerColor =
                                newCPU_temp > 59 ? "#FF2500":
                                newCPU_temp >= 55 ? "#FF6000":
                                newCPU_temp < 51 ? "#6B8E23 ":
                                null;
                        potencia[i] = {label: "CPU" , y: newCPU_temp, color: boilerColor};

                        chart_VI_2.render();


                        chart_potencia_2.render();
                };
                updateChart();
                //update chart after specified interval
                setInterval(function(){updateChart()}, updateInterval);


                socket.on('isr_gpio_7', function(msg){
                        alert('Evento');
                });
        };
        var socket = io();
        var rele_status = {r1: 0, r2: 0, r3: 0, r4: 0};

        function cmd_rele(rele){
                //$("#id_input").val();
                if(rele == 1){
                        rele_status.r1 ^= 0x01;
                }else if(rele == 2){
                        rele_status.r2 ^= 0x01;
                }else if(rele == 3){
                        rele_status.r3 ^= 0x01;
                }else if(rele == 4){
                        rele_status.r4 ^= 0x01;
                }
                socket.emit('cmd_rele',rele_status);
        };

        function cmd_general(){
                socket.emit('cmd_general',rele_status);
        };


        </script>
</head>

<body>
  <div id="chartContainerVI_2" style="height: 300px; width: 100%;"></div>
  <div id="chartContainerPower_2" style="height: 300px; width: 100%;"></div>
  <input type="button" onclick="cmd_rele(1);" value="RELE 1"> &nbsp; <input type="button" onclick="cmd_rele(2);" value="RELE 2"> &nbsp;
  <input type="button" onclick="cmd_rele(3);" value="RELE 3"> &nbsp; <input type="button" onclick="cmd_rele(4);" value="RELE 4"> &nbsp;
  <input type="button" onclick="cmd_general();" value="CMD">
</body>
</html>

Source code of smt32f4discovery (project)

C/C++
Compiled on coIDE
No preview (download only).

Credits

Waner Silva

Waner Silva

6 projects • 14 followers
I am electronic enginer

Comments