├── .gitignore ├── serial-sketch └── serial-sketch.ino ├── serial-counter └── serial-counter.ino ├── README.md ├── package.json └── server ├── index.js └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /serial-sketch/serial-sketch.ino: -------------------------------------------------------------------------------- 1 | void setup() { 2 | Serial.begin(9600); 3 | 4 | } 5 | 6 | void loop() { 7 | Serial.print(2); 8 | delay(2000); 9 | } 10 | -------------------------------------------------------------------------------- /serial-counter/serial-counter.ino: -------------------------------------------------------------------------------- 1 | int counter = 0; 2 | void setup() { 3 | Serial.begin(9600); 4 | 5 | } 6 | 7 | void loop() { 8 | Serial.print(++counter, DEC); 9 | delay(2000); 10 | } 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # serial port with nodejs and arduino 2 | 3 | # Steps 4 | 1. load serial-sketch to arduino 5 | - you can see serial: `cat /dev/ttyACM0` 6 | 7 | # Issues 8 | - for permissiones: `sudo chmod a+rw /dev/ttyACM0` 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "arduino-node-serialport", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "NODE_ENV=production node server/index.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "express": "^4.16.2", 15 | "serialport": "^6.0.4", 16 | "socket.io": "^2.0.4" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /server/index.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const express = require('express'); 3 | const http = require('http'); 4 | const SocketIo = require('socket.io'); 5 | 6 | const app = express(); 7 | const server = http.createServer(app); 8 | const io = SocketIo.listen(server); 9 | 10 | // settings 11 | 12 | // routes 13 | app.get('/', (req, res) => { 14 | res.sendFile(__dirname +'/index.html'); 15 | }); 16 | 17 | // static files 18 | app.use(express.static(path.join(__dirname, 'public'))); 19 | 20 | const SerialPort = require('serialport'); 21 | const Readline = SerialPort.parsers.Readline; 22 | const parser = new Readline(); 23 | 24 | const mySerial = new SerialPort('/dev/ttyACM0', { 25 | baudRate: 9600 26 | }); 27 | 28 | mySerial.pipe(parser); 29 | 30 | mySerial.on('open', function () { 31 | console.log('Opened Port.'); 32 | }); 33 | 34 | mySerial.on('data', function (data) { 35 | console.log(parseInt(data)); 36 | console.log(data.toString()); 37 | io.emit('arduino:data', { 38 | value: data.toString() 39 | }); 40 | }); 41 | 42 | mySerial.on('err', function (data) { 43 | console.log(err.message); 44 | }); 45 | 46 | server.listen(3000, () => { 47 | console.log('Server on port 3000'); 48 | }); 49 | -------------------------------------------------------------------------------- /server/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |