CrhisDLM

Untitled

May 10th, 2020
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Importando librerias
  2. var Cylon = require('cylon');   //npm install -g cylon
  3. var fs = require('fs');
  4. const express = require('express')  //npm install express
  5.  
  6. var index = fs.readFileSync('index.html');  //cargar archivo con el formulario html
  7.  
  8.  
  9. var robot = Cylon.robot({
  10.   connections: {
  11.     arduino: { adaptor: 'firmata', port: 'COM14' }
  12.   },
  13.  
  14.   devices: {
  15.     sensor: { driver: 'analog-sensor', pin: 0, lowerLimit: 100, upperLimit: 900 },
  16.     led: { driver: 'led', pin: 13 },
  17.     led2: { driver: 'led', pin: 11 }
  18.   },
  19.  
  20.   work: function(my) {
  21.     //puede estar vacia
  22.   },
  23.   custom: function(value){ //funcion personalizada
  24.     this.devices.led.toggle();//Cambia de estado pin 13
  25.     this.devices.led2.brightness(parseInt(value)); //aplico al led el valor de intensidad dado.
  26.   }
  27. });
  28. robot.start();
  29.  
  30. const app = express()
  31. const port = 3000
  32.  
  33. app.get('/', (req, res) => {
  34.     res.format({
  35.  
  36.       'text/html': function () {
  37.         res.send(index)
  38.       }
  39.     })
  40.     //console.log(res);
  41.  
  42. })
  43. app.get('/led', (req, res) => {
  44.     console.log("valor: "+req.query.valor);
  45.     robot.custom(req.query.valor); //Aplico la nueva intensidad al LED
  46.     res.format({
  47.       'text/html': function () {
  48.         res.send(index) //devuelve la pagina html
  49.       }
  50.     })
  51. })
  52.  
  53. app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
Add Comment
Please, Sign In to add comment