# Node-red freezes when i read from PLC

**URL:** <https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630>\
**Category:** General\
**Created:** [21 March 2024 09:34 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630 "2024-03-21T09:34:31Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felipesr](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@Felipesr](https://discourse.nodered.org/u/Felipesr)\
**Post date:** [21 March 2024 09:34 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/1 "2024-03-21T09:34:31Z")

</div>

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.

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 March 2024 09:39 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/2 "2024-03-21T09:39:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Felipesr](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@Felipesr](https://discourse.nodered.org/u/Felipesr)\
**Post date:** [21 March 2024 10:10 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/3 "2024-03-21T10:10:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Felipesr](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@Felipesr](https://discourse.nodered.org/u/Felipesr)\
**Post date:** [21 March 2024 10:12 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/4 "2024-03-21T10:12:42Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/8/28b389a6311c4f6ac9990587495aca84be58186e.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 March 2024 10:37 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/5 "2024-03-21T10:37:32Z")

</div>

> [@Felipesr](#):
>
> i had to downgrade the version of nodejs from 21.5 to 20.x

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.

---

<div class="post-metadata">

**Author:** ![Felipesr](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@Felipesr](https://discourse.nodered.org/u/Felipesr)\
**Post date:** [21 March 2024 10:45 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/6 "2024-03-21T10:45:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 March 2024 11:43 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/7 "2024-03-21T11:43:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Felipesr](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@Felipesr](https://discourse.nodered.org/u/Felipesr)\
**Post date:** [21 March 2024 15:32 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/8 "2024-03-21T15:32:38Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/e/9ee9f40e1f39b85381c373c6634a18925f305cf8.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 March 2024 17:02 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/9 "2024-03-21T17:02:49Z")

</div>

> [@Felipesr](#):
>
> Could it be because i use too many nodes for the database?

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.

---

<div class="post-metadata">

**Author:** ![Felipesr](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@Felipesr](https://discourse.nodered.org/u/Felipesr)\
**Post date:** [21 March 2024 19:00 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/10 "2024-03-21T19:00:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 March 2024 16:00 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/11 "2024-03-22T16:00:25Z")

</div>

> [@Felipesr](#):
>
> 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?

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [22 March 2024 16:12 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/12 "2024-03-22T16:12:10Z")

</div>

> [@Felipesr](#):
>
> 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.

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7f1cdb88aa374b38039e6b08b16866e6491007cd.png)

---

<div class="post-metadata">

**Author:** ![Felipesr](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@Felipesr](https://discourse.nodered.org/u/Felipesr)\
**Post date:** [1 April 2024 07:51 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/13 "2024-04-01T07:51:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 April 2024 07:53 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/14 "2024-04-01T07:53:05Z")

</div>

[node-red-node-mysql](https://flows.nodered.org/node/node-red-node-mysql)

---

<div class="post-metadata">

**Author:** ![Felipesr](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@Felipesr](https://discourse.nodered.org/u/Felipesr)\
**Post date:** [2 April 2024 12:45 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/15 "2024-04-02T12:45:05Z")

</div>

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.

```auto
// 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;

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 April 2024 13:00 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/16 "2024-04-02T13:00:08Z")

</div>

> [@Felipesr](#):
>
> ```auto
> 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
> 
> ```

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.

```auto
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
}

```

---

<div class="post-metadata">

**Author:** ![Felipesr](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@Felipesr](https://discourse.nodered.org/u/Felipesr)\
**Post date:** [2 April 2024 14:13 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/17 "2024-04-02T14:13:43Z")

</div>

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!!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [16 April 2024 14:13 UTC](https://discourse.nodered.org/t/node-red-freezes-when-i-read-from-plc/86630/18 "2024-04-16T14:13:52Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
