|
| 1 | +var http = require("http").createServer(handler); |
| 2 | +var firmata = require("firmata"); |
| 3 | +var fs = require("fs"); |
| 4 | +var io = require("socket.io").listen(http); |
| 5 | + |
| 6 | +console.log("Connecting to Arduino"); |
| 7 | + |
| 8 | +var board = new firmata.Board("/dev/ttyACM0", function(){ |
| 9 | + console.log("Activation of pin 2"); |
| 10 | + board.pinMode(2, board.MODES.OUTPUT); |
| 11 | + console.log("Activation of pin 3"); |
| 12 | + board.pinMode(3, board.MODES.PWM); |
| 13 | +}); |
| 14 | + |
| 15 | +function handler(req, res) { |
| 16 | + fs.readFile(__dirname + "/example09.html", |
| 17 | + function(err, data) { |
| 18 | + if (err) { |
| 19 | + res.writeHead(500, {"Content-Type": "text/plain"}); |
| 20 | + return res.end("You didn't connect html file!"); |
| 21 | + } |
| 22 | + res.writeHead(200); |
| 23 | + res.end(data); |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +http.listen(8080); |
| 28 | + |
| 29 | +var desiredValue = 0; |
| 30 | + |
| 31 | +console.log("Running the system"); |
| 32 | + |
| 33 | +board.on("ready", function(){ |
| 34 | + console.log("Board is ready!"); |
| 35 | + board.analogRead(0, function(value){ |
| 36 | + desiredValue = value; |
| 37 | + }); |
| 38 | + |
| 39 | + io.sockets.on("connection", function(socket){ |
| 40 | + |
| 41 | + socket.on("sendPWM", function(pwm){ |
| 42 | + board.analogWrite(3,pwm); |
| 43 | + console.log("PWM value:" + pwm); |
| 44 | + }); |
| 45 | + |
| 46 | + socket.on("left", function(value) { |
| 47 | + board.digitalWrite(2,value); |
| 48 | + }); |
| 49 | + |
| 50 | + socket.on("right", function(value) { |
| 51 | + board.digitalWrite(2,value); |
| 52 | + }); |
| 53 | + |
| 54 | + socket.on("stop", function(value) { |
| 55 | + board.analogWrite(3,value); |
| 56 | + }); |
| 57 | + |
| 58 | + }); |
| 59 | + |
| 60 | +}); |
0 commit comments