Skip to content

Commit

Permalink
feat: upgrade socket-io example to latest serialport and fix bug (#1505)
Browse files Browse the repository at this point in the history
  • Loading branch information
reconbot committed Mar 8, 2018
1 parent bac0237 commit 86e5ab0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
30 changes: 15 additions & 15 deletions examples/socketio/index.js
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
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
@@ -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"
}

0 comments on commit 86e5ab0

Please sign in to comment.