Advertisement
martin2250

LED Webserver njs

Aug 27th, 2015
5,861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express');
  2. app = express();
  3. server = require('http').createServer(app);
  4. io = require('socket.io').listen(server);
  5.  
  6. var SerialPort = require("serialport").SerialPort
  7. var serialPort = new SerialPort("/dev/ttyACM0", { baudrate: 115200 });
  8.  
  9. server.listen(8080);
  10. app.use(express.static('public'));     
  11.  
  12. var brightness = 0;
  13.  
  14. io.sockets.on('connection', function (socket) {
  15.     socket.on('led', function (data) {
  16.         brightness = data.value;
  17.        
  18.         var buf = new Buffer(1);
  19.         buf.writeUInt8(brightness, 0);
  20.         serialPort.write(buf);
  21.        
  22.         io.sockets.emit('led', {value: brightness});   
  23.     });
  24.    
  25.     socket.emit('led', {value: brightness});
  26. });
  27.  
  28. console.log("running");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement