deploy nodejs modbus-rtu using the modbus-serial library to HEROKU - node.js

Recently, i want to deploy nodejs to heroku using modbus-serial library and socket.io, the idea here is i want to retrieve temperature real time from pt100 using serial RS-485,
locally, the code run well having no problem, but went i deploy to heroku, it seems like heroku didnt recognize the usb port "/dev/ttyUSB0",here what i got,
UnhandledPromiseRejectionWarning: Error: Error: No such file or directory, cannot open /dev/ttyUSB0,
its because heroku doesnt know what it is, or do i have to set the environment variable for the usb port,
here my code,
const ModbusRTU = require("modbus-serial")
const client = new ModbusRTU()
const app = require('http').createServer(server)
app.listen(process.env.PORT || 5000)
const io = require("socket.io")(app)
io.on("connection",function(socket){
client.connectRTUBuffered("/dev/ttyUSB0", { baudRate: 115200 }).then(() => {
setInterval(() => {
client.writeFC3(1,0,2,(err,data) => {
if(err)console.log("error ",err)
console.log("datas ",data)
io.emit("datas_holding_register_rtu",{ datas : data["data"] })
})
},100)
})
socket.on('disconnect', () => console.log('Client disconnected'))
})
any idea what happening here ? thank you
sincerely,
toni

So... Heroku is a cloud system, and serial communication occurs LOCALLY WITH a cable attached to your computer to the USB port. So, Heroku cannot communicate with your device, because it doesn't have an USB port. Solution: Run your code on a computer connected to the usb cable.

Related

Chat with another pc with react chat-app and socket.io

I followed a youtube tutorial on making a chat app with react js and socket.io. It is complete and works perfectly, but only on the pc, the project is running on. What I need is the application to chat with another pc the project is working on, it could be on the same network, for starters. The front-end is separate and is made with react js.
I tried adding a dummy IP to where I specify the port it will run on, but didn't work.
Any ideas? What am I missing here?
Here's the code to the server.js which is in plain node js:
const port = 5000 || '0.0.0.0' <--- Ignore the dummy IP kindly, didn't work, thought it was worth a try
const io = require('socket.io')(port)
io.on('connection', socket => {
const id = socket.handshake.query.id
socket.join(id)
console.log("Listening at " + port)
socket.on('send-message', ({ recipients, text }) => {
recipients.forEach(recipient => {
const newRecipients = recipients.filter(r => r !== recipient)
newRecipients.push(id)
socket.broadcast.to(recipient).emit('receive-message', {
recipients: newRecipients, sender: id, text
})
})
})
})
I'd recommend you look into deploying your chat app on something like Heroku or Firebase, which will provide you a 3rd-party non-localhost link that other PCs and users can go to in order to access your chat app.
Alternatively, you can use something like https://github.com/localtunnel/localtunnel to have other PCs access your localhost

trying to connect to TCP server using Node.js

I'm trying to connect to a server/specific port using Node.js, and I don't even get past var net = require('net');
I'm using Node.js v16.15.0.
Welcome to Node.js v16.15.0.
When I use the command above, I receive UNDEFINED. As far as I know, I've installed everything I need (including socket.io), and I'm working within the Node.js environment in iTerm.
My goal is to connect to a TCP server, receive a list of files, and then download each of them over a persistent socket. But I'm a little stuck as I can't even seem to get into the TCP server in the first place.
This is what I think I'm supposed to run to get in (obviously with my correct port and IP info which is omitted below).
var HOST = 'IP';
var PORT = 'PORT'
var FILEPATH = 'myfilepathhereIwilltweakitwhenIgettothispoint';
Can anyone point me in the right direction?
From what you said, I think you are trying to code NodeJS script within the NodeJS executable start in command line. You get an UNDEFINED because you imported the library into your variable and this assignment does have any value, so it is UNDEFINED. You can read more about this in this subject : link
But what we usually do in NodeJS development is creating a file, let's call it index.js. Inside that file we are writing our code, let's say :
const net = require('net');
const client = net.createConnection({ port: 8124 }, () => {
// 'connect' listener.
console.log('connected to server!');
client.write('world!\r\n');
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
console.log('disconnected from server');
});
Code sample from NodeJS Documentation.
Then we want to run our code by using the command line like this : node path/to/index.js.
Hope it helps !

ERR_CONNECTION_REFUSED error when i try to access the server running on localhost on laptop from my mobile devise connected to same wifi network

I want to access the server that I have build using node.js from my android phone. My server is running on localhost port 80 and my laptop and mobile device is connected to the same wifi network.
I tried turning off the windows defender firewall but it didn't work.
In my mobile device browser, I used the link to make a request to my server as
laptop_ip_address:port_no/home
But it didn't work.
I get the ERR_CONNECTION_REFUSED error in my browser.
I don't want to use ngrok.
what should I do?
this is my server code
const express=require("express")
const app=express()
app.get("/",(req,res)=>{
res.status(200).send("<h1>hiiii</h1><a href='/home'>hii</a>")
})
app.get("/home",(req,res)=>{
res.status(200).send("<h1>yooo</h1>")
})
app.listen(80,()=>{
console.log("App is running");
})
Try after adding cors module. This may help to solve the issue.
const cors=require('cors')
const app=express()
app.use(cors);
app.get("/",(req,res)=>{
res.status(200).send("<h1>hiiii</h1><a href='/home'>hii</a>")
})
app.get("/home",(req,res)=>{
res.status(200).send("<h1>yooo</h1>")
})
app.listen(80,()=>{
console.log("App is running");
})

Can't get hidraw0 device to open in nodeJS app

Good evening!
I'm trying to setup a barcode scanner app using the Node SerialPort package, used on a Raspberry Pi 4, and I'm having a hard time getting the code to either recognise the device
I've identified that the path is /dev/hidraw0 as this is what I was able to ascertain from a dmesg output.
When I run cat /dev/hidraw0 it opens and when I scan a barcode it outputs %""'#''!'&" to the console (I know it's gibberish, but different problem for a different day). BUT, when I reference this path in my nodeJS code, I get the below error:
Serial port error: Error: Error: Invalid argument setting custom baud rate of 9600
I've confirmed on the manufacturer website that the default baud rate is 9600. I have tried removing the baudRate option in the below code, but it still says the same error.
This is the code I'm using right now:
// Use the serialport module to get scanner data
const SerialPort = require('serialport');
const Readline = require('#serialport/parser-readline')
// Open the serial port with some configuration
const port = new SerialPort('/dev/hidraw0', {baudRate: 9600});
const parser = new Readline()
port.pipe(parser)
// When the port is open just say so
port.on('open', function() {
console.log('Serial port open');
});
// Check for errors on the serial interface
port.on('error', function(err) {
console.log(`Serial port error: ${err}`);
});
// Pass scanner data onto web server
port.on('data', function(data) {
console.log('Scan prompted');
});
The USB Scanner I'm using is the Zebra LS2208 and I would love some help or guidance on what may be causing this.
Thanks in advance!
In case anyone else comes around this, I was not able to get the device working with the NPM package I referenced, but as this module used node-hid, I decided to use that one and was able to get my scanner recognised and working properly.

NodeJs text file writing

I am trying to write a text file from NodeJs. I have server running on my laptop. Currently, i am running client on my laptop and it works fine. But if i run same NodeJs client on Linux running on raspberrypi, it doesn't write on file or neither it gives any error.
I have the following code for client
var ioC = require('socket.io-client'),
ioClient = ioC.connect('http://localhost:4000'),
fs = require('fs'),
os = require('os');
ioClient.on('connect', function () { console.log("socket connected"); });
ioClient.on('ChangeState', function(msg){
console.log(msg);
fs.writeFile('server.txt', JSON.stringify(msg), function (err){
if (err) return console.log(err);
});
});
Can anybody please help me what can be the issue with this?
You are connecting to localhost which won't work if the client is on a different machine. You need to change the server to listen to the ip address your server has in your network, and also need to let your client connect to this ip. You can get the ip by running ifconfig in your terminal. Then (depending on wireless or wired connection) look for something like (usually the last paragraph):
and create the server on this ip. E.g.
192.168.178.30:4000
and connect to the same address from your client.
To find your ip on windows, refer to this guide

Resources