add better arduino error handling
Server won't just crash when no arduino is available.
This commit is contained in:
parent
3cea3bf323
commit
c3a37ff63f
1 changed files with 9 additions and 4 deletions
|
@ -1,8 +1,13 @@
|
||||||
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. */
|
||||||
|
@ -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');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Reference in a new issue