Created
January 30, 2021 22:02
-
-
Save lkishfy/a8bd35b0a487b80ff4c622444b262064 to your computer and use it in GitHub Desktop.
talkToArduino.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ///////////////////////////////////////////////////INITIALIZE VARIABLES | |
| var express = require('express') | |
| const url = require('url'); | |
| var app = express() | |
| var SerialPort = require("serialport"); | |
| var path = require('path'); | |
| var server = require('http').createServer(app); | |
| var io = require('socket.io')(server); | |
| var port = process.env.PORT || 3000; | |
| const Readline = require('@serialport/parser-readline'); | |
| const portSerial = new SerialPort('/dev/cu.SLAB_USBtoUART', { baudRate: 9600 }); | |
| ///////////////////////////////////////////////////INITIALIZE VARIABLES | |
| ///////////////////////////////////////////////////PAGE RENDERING | |
| app.use(express.static(__dirname + '/views')); | |
| server.listen(3000, "192.168.1.161",function listening() { | |
| console.log('Listening on %d', server.address().port); | |
| }); | |
| app.get('/', function (req, res) { | |
| res.sendFile('index.html'); | |
| }) | |
| ///////////////////////////////////////////////////PAGE RENDERING | |
| ///////////////////////////////////////////////////BACKEND SERVER LOGIC | |
| const parser = portSerial.pipe(new Readline({ delimiter: '\n' })); | |
| // Read the port data | |
| portSerial.on("open", () => { | |
| console.log('serial port open'); | |
| }); | |
| parser.on('data', data =>{ | |
| console.log('got word from arduino:', data); | |
| }); | |
| io.on('connection', socket => { | |
| socket.on('1', function(){ | |
| console.log("1 caught"); | |
| portSerial.write('Z', (err) => { | |
| if (err) { | |
| return console.log('Error on write: ', err.message); | |
| } | |
| console.log('message written'); | |
| }); | |
| }); | |
| socket.on('2', function(){ | |
| console.log("2 caught"); | |
| portSerial.write('B', (err) => { | |
| if (err) { | |
| return console.log('Error on write: ', err.message); | |
| } | |
| console.log('message written'); | |
| }); | |
| }); | |
| socket.on('3', function(){ | |
| console.log("3 caught"); | |
| portSerial.write('C', (err) => { | |
| if (err) { | |
| return console.log('Error on write: ', err.message); | |
| } | |
| console.log('message written'); | |
| }); | |
| }); | |
| socket.on('4', function(){ | |
| console.log("4 caught"); | |
| portSerial.write('D', (err) => { | |
| if (err) { | |
| return console.log('Error on write: ', err.message); | |
| } | |
| console.log('message written'); | |
| }); | |
| }); | |
| }); | |
| ///////////////////////////////////////////////////BACKEND SERVER LOGIC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment