const { SerialPort } = require('serialport') const express = require('express') const bodyParser = require('body-parser') const app = express() const movement = 1000; // Create a port const port = new SerialPort({ path: '/dev/ttyUSB0', baudRate: 9600, }) // app.use(bodyParser.json()); const toPlotter = (message) => { console.log("i will write to plotter ", message); port.write(message, function (err) { if (err) { return console.log('Error on write: ', err.message) } console.log('message written: ', message) }) } // Open errors will be emitted as an error event port.on('error', function (err) { console.log('Error: ', err.message) }) port.on("open", function () { console.log('open'); port.on('data', function (data) { console.log(data); }); }); app.post('/init', (req, res) => { toPlotter(`IN;SC0,0,100,0,100;SP1;PA60,60;PU`) res.send('Initialise the plotter') }) app.post('/pen-up', (req, res) => { toPlotter(`PU;`); res.send("Sending a message to the plotter about PU;",) }); app.post('/pen-down', (req, res) => { toPlotter(`PD;`); res.send("Sending a message to the plotter about PD;",) }); app.post('/move-left', (req, res) => { toPlotter(`PR-${movement},0;`); res.send("Sending a message to the plotter about PR-10,0;",) }); app.post('/move-right', (req, res) => { toPlotter(`PR${movement},0;`); res.send("Sending a message to the plotter about PR10,0;",) }); app.post('/move-up', (req, res) => { toPlotter(`PR0,${movement},0;`); res.send("Sending a message to the plotter about PR0,10;",) }); app.post('/move-down', (req, res) => { toPlotter(`PR0,-${movement};`); res.send("Sending a message to the plotter about PR0,-10;",) }); // GET /text?message={message} app.post('/text', (req, res) => { toPlotter(`SI0.5,0.8;LB${req.query.message};`); res.send(`SI0.5,0.8;LB${req.query.message};`) }); // GET /direct?message={message} app.post('/direct', (req, res) => { toPlotter(`${req.query.message}`); res.send(`${req.query.message}`) }); app.use(express.static('public')) app.listen(3000, () => { console.log(`Example app listening on port`) }) const tmi = require('tmi.js'); const client = new tmi.Client({ channels: [ 'twitchplaysplotter' ] }); client.connect(); client.on('message', (channel, tags, message, self) => { console.log(`${tags['display-name']}: ${message}`); switch ( message.toLowerCase() ) { case 'pu': console.log("Performing a PU"); break; case 'pd': console.log("Performing a PD"); break; case 'up': console.log("Performing a move UP"); break; case 'down': console.log("Performing a move DOWN"); break; case 'left': console.log("Performing a move LEFT"); break; case 'right': console.log("Performing a move RIGHT"); break; default: console.log("Message not recognized"); toPlotter(`IN;SC0,0,100,0,100;SP1;PA60,60;PU`) break; } });