parsing number and message as one

This commit is contained in:
Lukas Bachschwell 2016-03-08 10:06:36 +01:00
parent e1d0eac6a5
commit 59d98f94af
1 changed files with 22 additions and 18 deletions

View File

@ -10,6 +10,7 @@ var socketServer;
var serialPort; var serialPort;
var portName = '/dev/tty.usbmodemFD121'; //change this to your Arduino port var portName = '/dev/tty.usbmodemFD121'; //change this to your Arduino port
var sendData = ""; var sendData = "";
var numberRecieved = "";
// handle contains locations to browse to (vote and poll); pathnames. // handle contains locations to browse to (vote and poll); pathnames.
function startServer(route,handle,debug) function startServer(route,handle,debug)
@ -63,30 +64,33 @@ function serialListener(debug)
serialPort.on("open", function () { serialPort.on("open", function () {
console.log('opened serial communication'); console.log('opened serial communication');
// Listens to incoming data
serialPort.on('data', function(data) { serialPort.on('data', function(data) {
//receivedData += data.toString(); console.log('Incomming serial data...\r');
//if (receivedData .indexOf('E') >= 0 && receivedData .indexOf('B') >= 0) { console.log(data);
sendData = data; //.substring(receivedData .indexOf('B') + 1, receivedData .indexOf('E')); console.log("\r"); //+data.length
receivedData = ''; // send the incoming data to clients using the socket.
console.log('server event happening..' + data + '.\r'); if(data.startsWith("+CMT:")){ // if message ok
//} numberRecieved = data;
// send the incoming data to browser with websockets. } else if (data.length > 1) {
//socketServer.emit('update', sendData); //add a new message to the board directly
if(numberRecieved){
console.log("emit");
socketServer.emit('newMessage', numberRecieved+" "+data);
numberRecieved = null;
}
else {
console.log("nothing");
}
} else {
//debugMessages trigger an alert on the clients
//socketServer.emit('debugMessage', data);
}
});
//if is message and is parsed
socketServer.emit('newMessage', sendData);
});
//setTimeout(sendPin,500);
serialPort.write('AT+CPIN=3797\r'); serialPort.write('AT+CPIN=3797\r');
console.log("Sent Pincode..."); console.log("Sent Pincode...");
}); });
} }
function sendPin(){
serialPort.write('AT+CPIN=3797\r');
console.log("Sent Pincode...");
}
exports.start = startServer; exports.start = startServer;