II. Node-RED & Raspberry Tutorial: How to create an Automatic Counter App

Raspberry PLC & Node-RED along with industrial automation
October 15, 2021 by
II. Node-RED & Raspberry Tutorial: How to create an Automatic Counter App
Boot & Work Corp. S.L., Fernandez Queralt Martinez

INTRODUCTION

In the last blog: Read >  we learnt how to connect the Bernstein 6088185022 limit switch with the Raspberry PLC, powered by a power supply.

In this blog, we will learn how to automate a counter by using a limit switch, by getting the result in a Node-RED Dashboard.

Your Dynamic Snippet will be displayed here... This message is displayed because you did not provided both a filter and a template to use.

NODE-RED 

First of all, make sure that Node-RED is already installed in your Raspberry PLC, and that you can get access to the Raspberry Pi based PLC.

Also, make sure that your Raspberry PLC has the Dashboard nodes > already installed in Node-RED.

AUTOMATIC COUNTER APP

The intention of our automatic counter application will be to create a Node-RED dashboard to be able to monitor the counter of the pieces that touch the limit switch, by storing the value in a file as a database. It will be also possible to change the numeric value in the dashboard itself, if it has to be modified for any reason.

In our flow, there will be four lines:

1. In the first one, we will read the I0.0 input, and we will add up one on the downhill side to the flow.count variable and to the counter.txt file used as our database.

2. In the second one, we will read the value from the file, and we will show it in a gauge dashboard node and in a dashboard text node. After reading the counter.txt file, we will input the counter value in a numeric dashboard node, that will store the value in the flow.count variable, and in the counter.txt file.

3. In the last one, we will inject the value of the counter.txt file to the flow.count once after 1 second when we start the flow.

STEPS

In order to create our Node-RED flow, follow these steps:


  • FIRST LINE

1. Add an inject node with the msg.payload as default (timestamp) and configure: 

Inject once after 0.1 seconds, then

Interval every 0.2 seconds

2. Wire it to an Industrial Shields digital read node >  with your Raspberry PLC model and the input that you want to read. In our case:

RPIPLC_21

Input: I0.0

3. As the intention is to add up a counter on the downhill flag, we will add a RBE node with the mode: block unless value changes in order to only pass the msg.payload in the uphill flag.

4. After that, we will add a switch node with the property: msg.payload == (number) 0. To make sure that the interrupt has been up and down again to the initial state.

Odoo • Image and Text

5. In that moment, we will have to add up 1 to the counter. We will add a change node and we will set the flow.count to the $flowContext("count") + 1

6. Then, add another change node and set the msg.payload to flow.count.

7. Finally, we will add a file node and we will type the absolute path of the filename. In our case: /home/pi/counter.txt. And in the action: we will overwrite file with the inputs.

Note: If you already created the file in the Raspberry PLC, do not mark the check button: Create directory if it doesn't exist. Otherwise, mark it.

Odoo • Image and Text
  • SECOND LINE

1. From the same inject node of the first line, we will wire a file in node with the default parameters and our filename: /home/pi/counter.txt

2. Now, we will add a change node in order to set msg.payload to the $number(payload).

3. Now, we are going to add a dashboard gauge node. We will configure the group and tab names, along with the units label, and the minimum and maximum range. 

Odoo • Text and Image
Odoo • Image and Text

4. We will also add a dashboard text after the change noed in order to show the value of the counter.

5. Finally, we will add a numeric node, to be able to modify the value of the counter. We will also connect it to a file node and to a change node  to store the modified value.

In the change node, we will set the flow.count to $number(payload)

Odoo • Text and Image
Odoo • Image and Text
  • THIRD LINE

Finally, we are going to add an inject node and we are going to connect it to a file in node with the same configuration as before. The inject node will be injected once after 1 second.

Then, we will add a change node and we will set flow.count to $number(payload)

Odoo • Image and Text

NODE-RED DASHBOARD

Now that the flow is done, let's display the widgets in the dashboard as we like.

Go to the Dashboard menu > Layout > Hover in your Group (In our case: Home) > Click on layout

And display the Node-RED dashboard widgets as you like!

Odoo • Image and Text
Odoo • Image and Text

And now, start using your limit switch connected to the Raspberry PLC, and be sure to count everything that happens ahead! 

Odoo • Image and Text

GET THE FLOW

[{"id":"d757dbb1.993928","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"beab2351.33bf7","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false},{"id":"46cc0e82.6b4ce","type":"ui_base","theme":{"name":"theme-dark","lightTheme":{"default":"#0094CE","baseColor":"#0094CE","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":true,"reset":false},"darkTheme":{"default":"#097479","baseColor":"#097479","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":true,"reset":false},"customTheme":{"name":"Untitled Theme 1","default":"#4B7930","baseColor":"#4B7930","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"},"themeState":{"base-color":{"default":"#097479","value":"#097479","edited":false},"page-titlebar-backgroundColor":{"value":"#097479","edited":false},"page-backgroundColor":{"value":"#111111","edited":false},"page-sidebar-backgroundColor":{"value":"#333333","edited":false},"group-textColor":{"value":"#0eb8c0","edited":false},"group-borderColor":{"value":"#555555","edited":false},"group-backgroundColor":{"value":"#333333","edited":false},"widget-textColor":{"value":"#eeeeee","edited":false},"widget-backgroundColor":{"value":"#097479","edited":false},"widget-borderColor":{"value":"#333333","edited":false},"base-font":{"value":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"}},"angularTheme":{"primary":"indigo","accents":"blue","warn":"red","background":"grey","palette":"light"}},"site":{"name":"Node-RED Dashboard","hideToolbar":"false","allowSwipe":"false","lockMenu":"false","allowTempTheme":"true","dateFormat":"DD/MM/YYYY","sizes":{"sx":48,"sy":48,"gx":6,"gy":6,"cx":6,"cy":6,"px":0,"py":0}}},{"id":"52b8bacf.7a0fc4","type":"ui_group","name":"Counter","tab":"beab2351.33bf7","order":1,"disp":true,"width":"19","collapse":false},{"id":"f3999e54.3e4c1","type":"rpiplc-config","model":"RPIPLC_21","name":""},{"id":"8922413.f537fc","type":"ui_spacer","name":"spacer","group":"52b8bacf.7a0fc4","order":1,"width":9,"height":1},{"id":"9e46710.d01e89","type":"ui_spacer","name":"spacer","group":"52b8bacf.7a0fc4","order":3,"width":1,"height":1},{"id":"1a2dc237.9cd55e","type":"ui_spacer","name":"spacer","group":"52b8bacf.7a0fc4","order":4,"width":1,"height":1},{"id":"4c8d22bf.3c8f9c","type":"ui_spacer","name":"spacer","group":"52b8bacf.7a0fc4","order":6,"width":3,"height":1},{"id":"c5ad2b6.e7d81d8","type":"ui_spacer","name":"spacer","group":"52b8bacf.7a0fc4","order":7,"width":1,"height":1},{"id":"a7a6c5cf.3a8488","type":"ui_spacer","name":"spacer","group":"52b8bacf.7a0fc4","order":8,"width":1,"height":1},{"id":"53547df1.fd2a34","type":"ui_spacer","name":"spacer","group":"52b8bacf.7a0fc4","order":9,"width":3,"height":1},{"id":"3a5f339e.21845c","type":"ui_spacer","name":"spacer","group":"52b8bacf.7a0fc4","order":10,"width":1,"height":1},{"id":"8e03abce.839ea8","type":"ui_spacer","name":"spacer","group":"52b8bacf.7a0fc4","order":11,"width":4,"height":1},{"id":"81ae006c.63d66","type":"ui_spacer","name":"spacer","group":"52b8bacf.7a0fc4","order":13,"width":2,"height":1},{"id":"c7b9ab90.6055d8","type":"ui_spacer","name":"spacer","group":"52b8bacf.7a0fc4","order":14,"width":1,"height":1},{"id":"5801083c.d500e8","type":"ui_spacer","name":"spacer","group":"52b8bacf.7a0fc4","order":15,"width":4,"height":1},{"id":"7e79d3c4.04056c","type":"ui_spacer","name":"spacer","group":"52b8bacf.7a0fc4","order":16,"width":2,"height":1},{"id":"52db3ae9.f50924","type":"ui_spacer","name":"spacer","group":"52b8bacf.7a0fc4","order":17,"width":1,"height":1},{"id":"1c5d4e62.e304c2","type":"ui_spacer","name":"spacer","group":"52b8bacf.7a0fc4","order":18,"width":9,"height":1},{"id":"43ec4a1b.904754","type":"ui_spacer","name":"spacer","group":"52b8bacf.7a0fc4","order":19,"width":1,"height":1},{"id":"28c7f816.30b308","type":"rpiplc-digital-read","z":"d757dbb1.993928","rpiplc":"f3999e54.3e4c1","pin":"I0.0","name":"","x":340,"y":160,"wires":[["30384994.5e6b96"]]},{"id":"30384994.5e6b96","type":"rbe","z":"d757dbb1.993928","name":"block unless value changes","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","x":640,"y":160,"wires":[["20a72e84.c5d322"]]},{"id":"20a72e84.c5d322","type":"switch","z":"d757dbb1.993928","name":"msg.payload == 0","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":930,"y":160,"wires":[["56cdef99.f1f0a"]]},{"id":"905e9c43.e8eca","type":"inject","z":"d757dbb1.993928","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"1","topic":"","payload":"","payloadType":"date","x":150,"y":480,"wires":[["27375f15.1f67e"]]},{"id":"34fe523a.d90a6e","type":"change","z":"d757dbb1.993928","name":"","rules":[{"t":"set","p":"count","pt":"flow","to":"$number(payload)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":480,"wires":[[]]},{"id":"56cdef99.f1f0a","type":"change","z":"d757dbb1.993928","name":"flow.count += 1","rules":[{"t":"set","p":"count","pt":"flow","to":"$flowContext(\"count\") + 1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":80,"wires":[["568add37.90a6a4"]]},{"id":"86fe9b36.541848","type":"ui_text","z":"d757dbb1.993928","group":"52b8bacf.7a0fc4","order":5,"width":5,"height":2,"name":"","label":"People","format":"{{msg.payload}}","layout":"row-spread","x":900,"y":320,"wires":[]},{"id":"48cbabe0.4ad3a4","type":"file in","z":"d757dbb1.993928","name":"","filename":"/home/pi/counter.txt","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":370,"y":240,"wires":[["14892d62.3e51f3"]]},{"id":"91fe28ea.c5c328","type":"inject","z":"d757dbb1.993928","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"0.2","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":240,"wires":[["28c7f816.30b308","48cbabe0.4ad3a4"]]},{"id":"ca6ba5e6.c35e98","type":"file","z":"d757dbb1.993928","name":"","filename":"/home/pi/counter.txt","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"none","x":930,"y":80,"wires":[[]]},{"id":"14892d62.3e51f3","type":"change","z":"d757dbb1.993928","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number(payload)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":240,"wires":[["86fe9b36.541848","a053ff48.1f95b","9fdc2836.02ac78"]]},{"id":"87dfcef5.d357","type":"file","z":"d757dbb1.993928","name":"","filename":"/home/pi/counter.txt","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"none","x":930,"y":400,"wires":[[]]},{"id":"27375f15.1f67e","type":"file in","z":"d757dbb1.993928","name":"","filename":"/home/pi/counter.txt","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":370,"y":480,"wires":[["34fe523a.d90a6e"]]},{"id":"9fdc2836.02ac78","type":"ui_numeric","z":"d757dbb1.993928","name":"","label":"","tooltip":"","group":"52b8bacf.7a0fc4","order":12,"width":3,"height":2,"wrap":false,"passthru":true,"topic":"topic","topicType":"msg","format":"{{msg.payload}}","min":0,"max":"10000","step":1,"x":580,"y":400,"wires":[["34fe523a.d90a6e","87dfcef5.d357"]]},{"id":"a053ff48.1f95b","type":"ui_gauge","z":"d757dbb1.993928","name":"","group":"52b8bacf.7a0fc4","order":2,"width":9,"height":6,"gtype":"gage","title":"","label":"pax","format":"{{value}}","min":0,"max":"10000","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":890,"y":240,"wires":[]},{"id":"568add37.90a6a4","type":"change","z":"d757dbb1.993928","name":"set flow.count to msg.payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"count","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":80,"wires":[["ca6ba5e6.c35e98"]]}]

​Search in our Blog

II. Node-RED & Raspberry Tutorial: How to create an Automatic Counter App
Boot & Work Corp. S.L., Fernandez Queralt Martinez October 15, 2021
Share this post

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>