-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
36 lines (32 loc) · 1.04 KB
/
app.js
File metadata and controls
36 lines (32 loc) · 1.04 KB
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
const express = require('express')
const app = express()
const http = require('http').createServer(app)
const io = require('socket.io')(http)
const SerialPort = require('serialport')
const readLine = require('readline')
const usbPort = '/dev/cu.usbmodem14201' // Reset the value of this variable to the name of the port your arduino is connected to
const port = new SerialPort(usbPort)
const { processPulse } = require('./serial.js')
app.use(express.static('public'))
// listening for connection to front end
io.on('connection', (socket) => {
console.log('A user connected')
const lineReader = connectUSB()
// this is the input from port, from createInterface
lineReader.on('line', line => {
const lineArr = line.split(',')
const beat = processPulse(line)
io.sockets.emit('signal', beat)
console.log(beat)
})
})
http.listen(8081, () => {
console.log('listening on *:8081')
})
// establishes connection to port
const connectUSB = () => {
let lineReader = readLine.createInterface({
input: port
})
return lineReader
}