diff --git a/nodejs/routes/index.js b/nodejs/routes/index.js index 5170d5b..f796f0f 100644 --- a/nodejs/routes/index.js +++ b/nodejs/routes/index.js @@ -1,24 +1,40 @@ var express = require('express'); var router = express.Router(); -var sucess = true; -var SerialPort = require("serialport").SerialPort -serialPort = new SerialPort("/dev/ttyACM0", { - baudrate: 9600 -}, false); +var child = require("child_process"); -serialPort.open(function (error) { - if ( error ) { - sucess = false; - } +var SerialPort = require("serialport").SerialPort +var success; +child.exec('cd ../arduino && ino build && ino upload', function(err, stdout, stderr){ + + if(err){console.log('Please install the ino utility and connect arduino!\nIf there are other open programs that communicate with the arduino, close them!'); return success = false;} + + var buff = stdout.slice(stdout.indexOf('Guessing serial port ... ')+25); + buff = buff.slice(0, buff.indexOf('\n')); + console.log('Guessing serial port ... ' + buff); + + serialPort = new SerialPort(buff, { + baudrate: 9600 + }, false); + + serialPort.open(function (error) { + if ( error ) { + console.log('Please check arduino!') + return successfalse; + } + }); + + return success = true; }); +//console.log(success); + /* GET home page. */ router.get('/', function(req, res, next) { res.render('index', { title: 'Express' }); }); router.get('/toggleLight', function (req, res) { - if(sucess) { serialPort.write(new Buffer('~','ascii')); } else {console.log("arduino not connected")} + if(success) { serialPort.write(new Buffer('~','ascii')); } else {console.log("arduino not connected")} res.send('switch light'); });