Advertisement
Guest User

Untitled

a guest
May 14th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ar express = require('express');
  2. var delay = require('delay');
  3. var mysql = require('mysql');
  4. var app = express();
  5. var server = require('http').Server(app);
  6. var io = require('socket.io').listen(server);
  7.  
  8. var serialport = require('serialport');
  9. var valor = "";
  10.  
  11. var con = mysql.createConnection({
  12.   host: "localhost",
  13.   user: "weather",
  14.   password: "weather",
  15.   database: "weather"
  16. });
  17.  
  18. io.on('connection',function(socket){
  19.     console.log("Alguien se conecto");
  20. });
  21.  
  22. var myPort = new serialport("COM4",{
  23.     baudRate: 9600,
  24.     parser: serialport.parsers.Readline
  25. });
  26.  
  27. myPort.on('open',onOpen);
  28. myPort.on('data',onData);
  29.  
  30. function onData(dato){
  31.     valor += dato.toString();
  32.     var ultima = "";
  33.     for (d of dato) {
  34.         if (d == 10) {
  35.             valor = valor.replace('\n', '');
  36.             console.log(valor);
  37.             datos = valor.split(' ');
  38.             addData("1", "temperatura", parseFloat(datos[0]));
  39.             addData("2", "humedad", parseFloat(datos[1]));
  40.             //var listatemp = getData(1);
  41.             //var listahum = getData(2);
  42.             //io.sockets.emit('lecturatemp', listatemp[listatemp.length-1]);
  43.             //io.sockets.emit('lecturahum', listahum[listahum.length-1]);
  44.             io.sockets.emit('lectura', datos);
  45.             valor = "";
  46.         }
  47.     }
  48. }
  49.  
  50. function addData(nSensor, nombre, valor){
  51.     var valores = [nSensor, nombre];
  52.     con.query('INSERT IGNORE INTO sensor (id_sensor, nombre) VALUES (?, ?)', valores,
  53.         function (err) {
  54.             if (err) throw err;
  55.             con.query('INSERT INTO dato (valor) VALUES (?)', valor,
  56.                 function (err, result) {
  57.                     if (err) throw err;
  58.                     valores = [nSensor, result.insertId];
  59.                     con.query('INSERT INTO dato_sensor (id_sensor, id_dato) VALUES (?, ?)', valores,
  60.                         function (err) {
  61.                             if (err) throw err;
  62.                         }
  63.                     );
  64.                 }
  65.             );
  66.         }
  67.     );
  68. }
  69.  
  70. function getData(nSensor){
  71.     var array = [];
  72.     con.query('SELECT valor FROM dato AS d, dato_sensor AS ds WHERE d.id_dato = ds.id_dato AND ds.id_sensor = ?', nSensor,
  73.         function (err, result) {
  74.             if (err) throw err;
  75.             var i;
  76.             for (i = 0; i < result.length; i++) {
  77.                 array.push(result[i].valor);
  78.             }  
  79.             console.log(array);
  80.             return array;
  81.         }
  82.     );
  83. }
  84.  
  85. function onOpen(){
  86.     console.log("Arduino conectado");
  87. };
  88.  
  89. app.use(express.static(__dirname+'/weatherApp'));
  90.  
  91. /*app.get('/',function(req,res){
  92.     res.sendFile(__dirname+'/weatherApp/index.html');
  93. });*/
  94.  
  95. server.listen(8000,function(){
  96.     console.log("Servidor iniciado");
  97. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement