implemented port guessing and auto upload

uses the ino utility to auto upload to the arduino
This commit is contained in:
Shannon Sterz 2016-01-14 18:01:51 +01:00
parent 8c12d72c34
commit 5f26eb8c47

View file

@ -1,24 +1,40 @@
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
var sucess = true; var child = require("child_process");
var SerialPort = require("serialport").SerialPort
serialPort = new SerialPort("/dev/ttyACM0", {
baudrate: 9600
}, false);
serialPort.open(function (error) { 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 ) { if ( error ) {
sucess = false; console.log('Please check arduino!')
return successfalse;
} }
});
return success = true;
}); });
//console.log(success);
/* GET home page. */ /* GET home page. */
router.get('/', function(req, res, next) { router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' }); res.render('index', { title: 'Express' });
}); });
router.get('/toggleLight', function (req, res) { 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'); res.send('switch light');
}); });