Add socketio example (#1081)

* Add socketio example
* fix json in .eslintrc file
This commit is contained in:
Jessica Quynh Tran 2017-02-19 19:39:54 -05:00 • committed by Francis Gulotta
parent b51991f8ca
commit b8b77e725f
5 changed files with 192 additions and 1 deletions

View file

@ -7,7 +7,7 @@
],
"env": {
"node": true,
"mocha": true,
"mocha": true
},
"rules": {
"arrow-parens": [2, "as-needed", {"requireForBlockBody": true }],

View file

@ -0,0 +1,58 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<ul id="messages"></ul>
<div id="actions">
<button id="turnOnLed" type="button">Turn On LED</button>
<button id="turnOffLed" type="button">Turn Off LED</button>
</div>
</body>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
$(function() {
// Initialize Variables
const socket = io();
const $messages = $('#messages');
const $turnOffLed = $('#turnOffLed');
const $turnOnLed = $('#turnOnLed');
// Socket.io listeners
socket.on('new message', (msg) => {
displayMessage(msg);
});
socket.on('close', () => {
displayMessage('Lost connection to device.');
});
// Browser Event Listeners
$turnOffLed.click(() => {
sendData('off');
});
$turnOnLed.click(() => {
sendData('on');
});
// Functions
function sendData(data) {
socket.send(data);
}
function displayMessage(msg) {
$messages.append($('<li>').text(msg));
}
});
</script>
</html>

102
examples/socketio/index.js Normal file
View file

@ -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');
});

View file

@ -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);
}
}

View file

@ -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"
}