III. Temperature sensor & Raspberry PLC: How to parse the temperature using Node-RED

DS18B20 Sensor & Raspberry PLC & Node-RED Usage
June 14, 2021 by
III. Temperature sensor & Raspberry PLC: How to parse the temperature using Node-RED
Boot & Work Corp. S.L., Fernandez Queralt Martinez

Introduction

Previously, we learned how to connect our Dallas sensor to a Raspberry PLC using the 1-Wire protocol.

See how (Part I) 

Then, we learned how to get the temperature from our Dallas sensor from Linux.

See how (Part II) 

In this blog, we are going to learn how to parse the temperature and how to get it from Node-RED.

Related Links

Raspberry PLC

Family Products

See 

How to

Program Raspberry PLC Interrupt inputs with Pyhton

Read 

Touchberry Pi

Family Products

See 

Explanation

Opening Node-RED

The Raspberry PLC is a very useful PLC as it incorporates the Raspbian OS and you can easily program it using as much open-source software as you like. One of our favorites is Node-RED, really useful for open-source hardware development. 

Once your Dallas sensor is connected like so >>> and you can get your temperature as shown >>> let's parse the value!

If you are connected to the Raspberry PLC by ssh to the default IP address 10.10.10.20, go to your favorite browser and type the following to get into Node-RED.

http://10.10.10.20:1880/

Otherwise, connect your Raspberry PLC to the Wi-Fi and install Node-RED by typing:

sudo apt install build-essential git curl

bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

sudo systemctl enable nodered.service

Finally, go to your browser and type:

http://localhost:1880/

Node-RED nodes

Once you are already in the Node-RED editor, let's add some nodes! 

1. First of all, we are going to add an exec node, as it a node that runs a system command and returns its output. Drag and drop the node to the editor, and add the following command that we already used previously to get the temperature:

sudo cat /sys/bus/w1/devices/28-0000072b7724/w1_slave | sed -e 's/.*t=//' | sed '1d'

Remember to replace the 28-0000072b7724 for the directory name starting by 28 that appears in your /sys/bus/w1/devices/ directory

    Step 1 - Node-RED Nodes - III. Temperature sensor & Raspberry PLC: How to parse the temperature using Node-RED

2.  If we add an inject node with the default parameter before the exec node, and a debug node right after the exec node, connected to the stdout output. When we click on the inject button, in the debug messages tab we will get the output as we get in Linux, just like this:

3. To parse the string and return a floating point number, we are going to add a function node between the exec node and the debug node. This is the function we are going to add:

msg.payload = parseFloat((msg.payload/1000).toFixed(3));
return msg;
You can change the number in the toFixed function, to get how many decimals as you want right after the dot. 

4. Deploy the changes and click on the inject node to get the parsed temperature!
Step 4 - Node-RED Nodes - III. Temperature sensor & Raspberry PLC: How to parse the temperature using Node-RED
Finally, you will be able to see the temperature from your Dallas sensor connected to your Raspberry PLC in a very simple way with Node-RED! 

Final Step - Node-RED Nodes - III. Temperature sensor & Raspberry PLC: How to parse the temperature using Node-RED

And with that information, you can create graphs or graphical interfaces to see the information in a much friendlier way.

See more


​Search in our Blog

III. Temperature sensor & Raspberry PLC: How to parse the temperature using Node-RED
Boot & Work Corp. S.L., Fernandez Queralt Martinez June 14, 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 >>>