diff --git a/.eslintrc b/.eslintrc index 720bced..8078aa7 100644 --- a/.eslintrc +++ b/.eslintrc @@ -7,7 +7,7 @@ ], "env": { "node": true, - "mocha": true, + "mocha": true }, "rules": { "arrow-parens": [2, "as-needed", {"requireForBlockBody": true }], diff --git a/examples/socketio/index.html b/examples/socketio/index.html new file mode 100644 index 0000000..6779fd2 --- /dev/null +++ b/examples/socketio/index.html @@ -0,0 +1,58 @@ + + + + + Hello World! + + +

Hello World!

+ + + +
+ + +
+ + + + + + + diff --git a/examples/socketio/index.js b/examples/socketio/index.js new file mode 100644 index 0000000..436873c --- /dev/null +++ b/examples/socketio/index.js @@ -0,0 +1,102 @@ +/* eslint require-path-exists/exists: 0 */ + +/** + * This is a small example app to turn off and on + * the built-in LED of an arduino by data sent + * from the browser with socket.io. + */ + +'use strict'; + +// Initialize application constants +const app = require('express')(); +const http = require('http').Server(app); +const io = require('socket.io')(http); +const port = process.env.PORT || 3000; + +const serialport = require('serialport'); +const SerialPort = serialport.SerialPort; + +const serial = new SerialPort('/dev/cu.usbmodem1411', { + baudrate: 9600, + parser: SerialPort.parsers.byteLength(1) +}); + +// Values to send over to Arduino. +const HIGH = Buffer.from([1]); +const LOW = Buffer.from([0]); + +/* =========================================== +* +* Setup a simple server. +* +=========================================== */ + +app.get('/', (req, res) => { + res.sendfile('index.html'); +}); + +http.listen(port, () => { + console.log(`listening on *:${port}`); +}); + +/* =========================================== +* +* Socket.io stuff +* +=========================================== */ + +io.on('connection', (socket) => { + console.log('a user connected'); + + /** + * Socket listener to determine whether or not to send HIGH / LOW + * values to Arduino. + */ + socket.on('message', (msg) => { + console.log('Message received: ', msg); + switch (msg) { + case 'on': + serial.write(HIGH); + break; + case 'off': + serial.write(LOW); + break; + default: + break; + } + }); +}); + +/* =========================================== +* +* Serialport stuff +* +=========================================== */ + +serial.on('open', () => { + console.log('Port is open!'); +}); + +/** + * EventListener to receive data from .ino script uploaded to Arduino. + * + */ +serial.on('data', (data) => { + let message; + + if (HIGH.compare(data) === 0) { + message = 'LED successfully turned on.'; + } else if (LOW.compare(data) === 0) { + message = 'LED successfully turned off.'; + } else { + message = 'LED did not behave as expected.'; + } + + io.sockets.emit('new message', message); +}); + +serial.on('close', () => { + console.log('Serial port disconnected.'); + io.sockets.emit('close'); +}); diff --git a/examples/socketio/ledWrite.ino b/examples/socketio/ledWrite.ino new file mode 100644 index 0000000..5159ddb --- /dev/null +++ b/examples/socketio/ledWrite.ino @@ -0,0 +1,14 @@ +void setup() { + // put your setup code here, to run once: + pinMode(LED_BUILTIN, OUTPUT); + Serial.begin(9600); +} + +void loop() { + // put your main code here, to run repeatedly: + while (Serial.available()) { + int byte = Serial.read(); + digitalWrite(LED_BUILTIN, byte); + Serial.print(byte); + } +} \ No newline at end of file diff --git a/examples/socketio/package.json b/examples/socketio/package.json new file mode 100644 index 0000000..8e5af38 --- /dev/null +++ b/examples/socketio/package.json @@ -0,0 +1,17 @@ +{ + "name": "socketio-example", + "version": "1.0.0", + "description": "LED switch example written for node-serialport.", + "main": "index.js", + "dependencies": { + "express": "^4.14.1", + "serialport": "^4.0.7", + "socket.io": "^1.7.2" + }, + "devDependencies": {}, + "scripts": { + "start": "node index.js" + }, + "author": "GitHub", + "license": "CC0-1.0" +}