41 lines
1.2 KiB
JavaScript
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;
|