-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
44 lines (34 loc) · 1.07 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var localConfig = require('./config/config.js');
var SerialPort = require("serialport").SerialPort
var sp = new SerialPort(localConfig.serialPort, { baudrate: 9600 });
//var socket = require('socket.io-client')('http://ledserver-dev.eu-west-1.elasticbeanstalk.com');
var socket = require('socket.io-client')('http://localhost:3000');
var arduinoAck = '';
socket.on('connect', socketConnected)
socket.on('connect_error', socketError)
socket.on('Light Control', buttonClicked)
sp.on('open', serialPortOpen)
sp.on('close', serialPortClosing)
sp.on('data', arduinoAckFunc)
function socketConnected(){
console.log('Socket Connected');
}
function socketError(error){
console.log('Socket: ' + error);
}
function serialPortOpen(){
console.log('Serial Port Open');
}
function buttonClicked(data) {
sp.write(data);
}
function serialPortClosing(){
console.log('Serial Port Closing');
}
function arduinoAckFunc(buffer){
arduinoAck += buffer.toString();
if (arduinoAck.indexOf('\r') >= 0) {
socket.emit('arduinoAck', arduinoAck.trim());
arduinoAck = '';
}
}