Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ar express = require('express');
- var delay = require('delay');
- var mysql = require('mysql');
- var app = express();
- var server = require('http').Server(app);
- var io = require('socket.io').listen(server);
- var serialport = require('serialport');
- var valor = "";
- var con = mysql.createConnection({
- host: "localhost",
- user: "weather",
- password: "weather",
- database: "weather"
- });
- io.on('connection',function(socket){
- console.log("Alguien se conecto");
- });
- var myPort = new serialport("COM4",{
- baudRate: 9600,
- parser: serialport.parsers.Readline
- });
- myPort.on('open',onOpen);
- myPort.on('data',onData);
- function onData(dato){
- valor += dato.toString();
- var ultima = "";
- for (d of dato) {
- if (d == 10) {
- valor = valor.replace('\n', '');
- console.log(valor);
- datos = valor.split(' ');
- addData("1", "temperatura", parseFloat(datos[0]));
- addData("2", "humedad", parseFloat(datos[1]));
- //var listatemp = getData(1);
- //var listahum = getData(2);
- //io.sockets.emit('lecturatemp', listatemp[listatemp.length-1]);
- //io.sockets.emit('lecturahum', listahum[listahum.length-1]);
- io.sockets.emit('lectura', datos);
- valor = "";
- }
- }
- }
- function addData(nSensor, nombre, valor){
- var valores = [nSensor, nombre];
- con.query('INSERT IGNORE INTO sensor (id_sensor, nombre) VALUES (?, ?)', valores,
- function (err) {
- if (err) throw err;
- con.query('INSERT INTO dato (valor) VALUES (?)', valor,
- function (err, result) {
- if (err) throw err;
- valores = [nSensor, result.insertId];
- con.query('INSERT INTO dato_sensor (id_sensor, id_dato) VALUES (?, ?)', valores,
- function (err) {
- if (err) throw err;
- }
- );
- }
- );
- }
- );
- }
- function getData(nSensor){
- var array = [];
- con.query('SELECT valor FROM dato AS d, dato_sensor AS ds WHERE d.id_dato = ds.id_dato AND ds.id_sensor = ?', nSensor,
- function (err, result) {
- if (err) throw err;
- var i;
- for (i = 0; i < result.length; i++) {
- array.push(result[i].valor);
- }
- console.log(array);
- return array;
- }
- );
- }
- function onOpen(){
- console.log("Arduino conectado");
- };
- app.use(express.static(__dirname+'/weatherApp'));
- /*app.get('/',function(req,res){
- res.sendFile(__dirname+'/weatherApp/index.html');
- });*/
- server.listen(8000,function(){
- console.log("Servidor iniciado");
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement