Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions examples/socketio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 3000;
const tcpPort = process.env.PORT || 3000;

const serialport = require('serialport');
const SerialPort = serialport.SerialPort;
const SerialPort = require('serialport');

const serial = new SerialPort('/dev/cu.usbmodem1411', {
baudRate: 9600,
parser: SerialPort.parsers.byteLength(1)
const port = new SerialPort('/dev/cu.usbmodem1411', {
baudRate: 9600
});

const byteParser = new SerialPort.parsers.ByteLength({ length: 1 });
port.pipe(byteParser);

// Values to send over to Arduino.
const HIGH = Buffer.from([1]);
const LOW = Buffer.from([0]);
Expand All @@ -36,8 +37,8 @@ app.get('/', (req, res) => {
res.sendfile('index.html');
});

http.listen(port, () => {
console.log(`listening on *:${port}`);
http.listen(tcpPort, () => {
console.log(`listening on http://localhost:${tcpPort}`);
});

/* ===========================================
Expand All @@ -57,10 +58,10 @@ io.on('connection', (socket) => {
console.log('Message received: ', msg);
switch (msg) {
case 'on':
serial.write(HIGH);
port.write(HIGH);
break;
case 'off':
serial.write(LOW);
port.write(LOW);
break;
default:
break;
Expand All @@ -74,15 +75,14 @@ io.on('connection', (socket) => {
*
=========================================== */

serial.on('open', () => {
port.on('open', () => {
console.log('Port is open!');
});

/**
* EventListener to receive data from .ino script uploaded to Arduino.
*
* listen to the bytes as they are parsed from the parser.
*/
serial.on('data', (data) => {
byteParser.on('data', (data) => {
let message;

if (HIGH.compare(data) === 0) {
Expand All @@ -96,7 +96,7 @@ serial.on('data', (data) => {
io.sockets.emit('new message', message);
});

serial.on('close', () => {
port.on('close', () => {
console.log('Serial port disconnected.');
io.sockets.emit('close');
});
4 changes: 2 additions & 2 deletions examples/socketio/ledWrite.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ void loop() {
while (Serial.available()) {
int byte = Serial.read();
digitalWrite(LED_BUILTIN, byte);
Serial.print(byte);
Serial.write(byte);
}
}
}
9 changes: 4 additions & 5 deletions examples/socketio/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
{
"name": "socketio-example",
"version": "1.0.0",
"name": "serialport-socketio-example",
"version": "1.0.1",
"description": "LED switch example written for node-serialport.",
"main": "index.js",
"dependencies": {
"express": "^4.14.1",
"serialport": "^4.0.7",
"serialport": "^6.0.0",
"socket.io": "^1.7.2"
},
"devDependencies": {},
"scripts": {
"start": "node index.js"
},
"author": "GitHub",
"license": "CC0-1.0"
"license": "MIT"
}