From 5f26eb8c475254c497f1084ecd2e1219f3694c9c Mon Sep 17 00:00:00 2001 From: Stefan Sterz Date: Thu, 14 Jan 2016 18:01:51 +0100 Subject: [PATCH] implemented port guessing and auto upload uses the ino utility to auto upload to the arduino --- nodejs/routes/index.js | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) 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'); });