add better arduino error handling

Server won't just crash when no arduino is available.
This commit is contained in:
Shannon Sterz 2016-01-13 23:47:51 +01:00
parent 3cea3bf323
commit c3a37ff63f

View file

@ -1,9 +1,14 @@
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
var sucess = true;
var SerialPort = require("serialport").SerialPort var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("/dev/ttyACM0", { serialPort = new SerialPort("/dev/ttyACM0", {
baudrate: 9600 baudrate: 9600
}); }, false).open(function (error) {
if ( error ) {
sucess = false;
}
});
/* GET home page. */ /* GET home page. */
router.get('/', function(req, res, next) { router.get('/', function(req, res, next) {
@ -11,7 +16,7 @@ router.get('/', function(req, res, next) {
}); });
router.get('/toggleLight', function (req, res) { router.get('/toggleLight', function (req, res) {
serialPort.write(new Buffer('~','ascii')); if(sucess) { serialPort.write(new Buffer('~','ascii')); } else {console.log("arduino not connected")}
res.send('switch light'); res.send('switch light');
}); });