Node-red freezes when i read from PLC

im working on a server that reads values froma PLC and dumps them in a simple database. It was working fine but from a day to another it suddenly stops working, node-red freezes when i connect to the PLC and reads no values. I don´t know why as it was working fine. i get this new messages when i start node-red. Can anyone help me please.

21 Mar 10:33:46 - [info]

Welcome to Node-RED
===================

21 Mar 10:33:46 - [info] Node-RED version: v3.1.7
21 Mar 10:33:46 - [info] Node.js  version: v20.11.1
21 Mar 10:33:46 - [info] Windows_NT 10.0.19045 x64 LE
21 Mar 10:33:47 - [info] Loading palette nodes
21 Mar 10:33:47 - [info] Dashboard version 3.6.5 started at /ui
21 Mar 10:33:47 - [info] Settings file  : C:\Users\felipe.sobrino\.node-red\settings.js
21 Mar 10:33:47 - [info] Context store  : 'default' [module=memory]
21 Mar 10:33:47 - [info] User directory : \Users\felipe.sobrino\.node-red
21 Mar 10:33:47 - [warn] Projects disabled : editorTheme.projects.enabled=false
21 Mar 10:33:47 - [info] Flows file     : \Users\felipe.sobrino\.node-red\flows.json
21 Mar 10:33:47 - [warn]

---------------------------------------------------------------------
Your flow credentials file is encrypted using a system-generated key.

If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.

You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.
---------------------------------------------------------------------

21 Mar 10:33:47 - [info] Server now running at http://127.0.0.1:1880/
21 Mar 10:33:47 - [info] Starting flows
21 Mar 10:33:47 - [info] Started flows
21 Mar 10:33:47 - [info] [sqlitedb:ed53b3331ecaacca] opened Documents\servidor_raspberry\db_web.db ok
1 Like

Have you recently update NodeJS or updated Node-RED?

What kind of PLC are you accessing and what contrib nodes are you using?

How often are/were you writing to the database?

How big is your database? SQLite is great for smaller scale stuff but not ideally suited to very long time series data.

1 Like

i had to downgrade the version of nodejs from 21.5 to 20.x because i got a punycode error " [DEP0040] The punycode module is deprecated" that is now solved with the current version i have. Im connecting to a PLC 1214C using library "plcindustry". My database is composed of 3 non related tables which i write to every 5 seconds at least. Thank you so much for you answer.

Could it be because i use too many nodes for the database? It´s weird because it was working fine with these nodes and i have no problem reading form the database.

I think SQLite has some binary bindings so will need to be rebuilt following a nodejs version change.

Even if it doesnt there may be other nodes or libraries under the hood that do.

doing the appropriate npm rebuild in your node-red user directory would be my first suggestion.

I opened the terminal in the ".node-red" folder and executed this command but it still crashed when it connected to the PLC. Do you have any other suggestion? I get no error message in the terminar other than the shown in the first post, it just freezes and won´t respond. Thank you again for your answer.

It isn't crashing, there would be a message.
Check in system monitor whether it is hogging the processor.

Not sure if it is hogging the system, the CPU reached a peak of 50% although the nodejs usage was only 16% at most. The dashboard is frozen even after some time of not doing anything. I moved a node and deployed and it´s pretty much stucked loading, don´t think it will finish any time soon.
image

Do you mean you have multiple database nodes all referencing the same database? I certainly would not recommend that. Sqlite is not good for multiple parallel access.

Yes, exactly that. I've been using this many nodes for the same data base for the whole project, i try not to ask to the data base from different nodes at the same time. This configuration has worked till two days ago but maybe that could be the problem. How could i use only 1 sqlite node if it only reads the msg.topic and this is used in all my functions either to write or read? Maybe another db program? Although I don't want to use mysql cus it requires a bigger processing and this project is meant to be in a raspberry pi.

Sorry, I have no idea what you mean by that.

For what it's worth, I run Mariadb (== MySQL) on a Raspberry Pi 4 along with Node-red and several other services. Several tables have tens of thousands of records.
htop suggests that the Pi is not breaking a sweat.

1 Like

i will try that, what library are you using in node-red to connect to the mariadb database?

node-red-node-mysql

1 Like

I found my error, it was in a function node. I don´t really know why it collapses the server. I´d be so thankful if anyone could help me. This is the code that is making node-red crash.

// get global variables
var formatoDeseado = global.get("formatoDeseado");
var hora_con_segundos = global.get("hora_con_segundos");

//set local variables to the msg read from the PLC
var TIEMPODOSIS = msg.payload["Tiempo_dosis"]
var PVPRESION = msg.payload["PV_Presion"]
var PRESIONDOSIS = msg.payload["Presion_dosis"]
var SPRESION = msg.payload["SP_Presion"]
var PRESIONTOQUE = msg.payload["Presion_toque"]

var x =1 ;

while (x<2)
    msg.topic = "INSERT INTO Tabla_dosis (Fecha,Hora,'T_dosis','PV_Presion','SP_Presion','Presion_dosis','Presion_toque') VALUES ('"+formatoDeseado+"','"+hora_con_segundos+"','"+TIEMPODOSIS+"','"+PVPRESION+"','"+SPRESION+"','"+PRESIONDOSIS+"','"+PRESIONTOQUE+"') "
    x = (x + 1);
    node.send(msg);//send msg once with this loop

//when loop ends, change msg.topic to read from DB
msg.topic = "SELECT * FROM Tabla_dosis WHERE Fecha = '"+formatoDeseado+"');"
return msg;

This is not doing what you think.

Your loop will run for ever. As there are no braces following the while, only the next statement is looped. In short, you never increment x

This is how it should be.

while (x<2) {
    msg.topic = "INSERT INTO Tabla_dosis (Fecha,Hora,'T_dosis','PV_Presion','SP_Presion','Presion_dosis','Presion_toque') VALUES ('"+formatoDeseado+"','"+hora_con_segundos+"','"+TIEMPODOSIS+"','"+PVPRESION+"','"+SPRESION+"','"+PRESIONDOSIS+"','"+PRESIONTOQUE+"') "
    x = (x + 1);
    node.send(msg);//send msg once with this loop
}
1 Like

that was the solution. it runs perfectly now, sorry for all the trouble.As you can tell, I´ve never programmed in java script and im using node-red to learn. This thread was very helpful to me.
Thanks so much to everyone involved. See you guys soon!!!

1 Like