Skip to content

Update to latest dependencies versions and ES6 modules #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 25 additions & 35 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,33 @@
var http = require('http');
var fs = require('fs');
var index = fs.readFileSync( 'index.html');

var SerialPort = require('serialport');
const parsers = SerialPort.parsers;

const parser = new parsers.Readline({
delimiter: '\r\n'
import { createServer } from "http";
import { readFileSync } from "fs";
import { SerialPort } from "serialport";
import { ReadlineParser } from "@serialport/parser-readline";
import { Server as SocketIOServer } from "socket.io";

const index = readFileSync("index.html");
const port = new SerialPort({ path: "/dev/cu.usbmodem101", baudRate: 9600 });
const parser = port.pipe(new ReadlineParser({ delimiter: "\r\n" }));

const app = createServer(function (_req, res) {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(index);
});

var port = new SerialPort('/dev/tty.wchusbserialfa1410',{
baudRate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false
// Specify the CORS options
const io = new SocketIOServer(app, {
cors: {
origin: "*", // Replace with the domain you want to make requests from
methods: ["GET", "POST"], // Allowed request methods
// allowedHeaders: ["my-custom-header"], // Custom headers here
credentials: true, // Allow credentials (cookies, session IDs)
},
});

port.pipe(parser);

var app = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(index);
});

var io = require('socket.io').listen(app);

io.on('connection', function(socket) {

console.log('Node is listening to port');

});
io.on("connection", () => console.log("Node is listening to port"));

parser.on('data', function(data) {

console.log('Received data from port: ' + data);

io.emit('data', data);

parser.on("data", (data) => {
// console.log(data);
io.emit("data", data);
});

app.listen(3000);
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>

<script src='https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js'></script>
<script src='https://cdn.socket.io/4.0.0/socket.io.min.js'></script>

<style>

Expand All @@ -23,7 +23,7 @@ <h1>Communicating from an Arduino to Node.js</h1>

<script>

var socket = io();
const socket = io("http://127.0.0.1:3000");

socket.on('data', function(data) {

Expand Down
23 changes: 16 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
{
"name": "basic-socket",
"version": "0.0.1",
"description": "Communication between Arduino and HTML/JavaScript",
"dependencies": {
"socket.io": "^2.0.4",
"serialport": "^9.0.1"
}
"name": "basic-socket",
"version": "0.0.2",
"description": "Communication between Arduino and HTML/JavaScript",
"main": "app.js",
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js"
},
"dependencies": {
"serialport": "^12.0.0",
"socket.io": "^4.7.4"
},
"devDependencies": {
"nodemon": "^3.1.0"
},
"type": "module"
}