var fs = require('fs'), http = require('http'), socketio = require('socket.io'), url = require("url"), serialport = require("serialport"); low = require('lowdb'), storage = require('lowdb/file-async'), db = low('db.json', { storage }); const port = 3000; var SerialPort = serialport.SerialPort; var socketServer; var serialPort; var portName = '/dev/tty.usbmodemFD121'; //change this to your Arduino port var numberStringRecieved = ""; var numberRecieved = ""; var timeRecieved = ""; // utility function for ucs2 decode function ucs2Parse(ucs2){ codeArray = ucs2.match(/.{1,4}/g); var returnString = ""; for(i=0;i 1) { if(numberRecieved){ //console.log("emit"); var messageRecieved = ucs2Parse(data); db('messages').push({ numberString: numberStringRecieved,number: numberRecieved, time: timeRecieved, message: messageRecieved }); //add a new message to the board directly socketServer.emit('newMessage', timeRecieved, numberRecieved, messageRecieved); numberStringRecieved = null; numberRecieved = null; timeRecieved = null; } else { //console.log("nothing"); } } else { //debugMessages trigger an alert on the clients //socketServer.emit('debugMessage', data); } }); serialPort.write('AT+CPIN=3797\r'); console.log("Sent Pincode..."); }); } exports.start = startServer;