This repository has been archived on 2024-01-30. You can view files and clone it, but cannot push or open issues or pull requests.
trev/nodejs/routes/index.js
Stefan Sterz 5f26eb8c47 implemented port guessing and auto upload
uses the ino utility to auto upload to the arduino
2016-01-14 18:02:45 +01:00

41 lines
1.2 KiB
JavaScript

var express = require('express');
var router = express.Router();
var child = require("child_process");
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(success) { serialPort.write(new Buffer('~','ascii')); } else {console.log("arduino not connected")}
res.send('switch light');
});
module.exports = router;