In compliance with high industry standards

Browse our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

Node-RED & Raspberry tutorial: How to get temperature from Dallas sensor

DS18B20 Sensor & Raspberry PLC & Node-RED Usage

INTRODUCTION


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

Then, we learnt how to get the temperature from our Dallas sensor from Linux. Go to the post >>

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

 

Odoo text and image block

RELATED LINKS

How to program Raspberry PLC

interrupt inputs with Python



Read >>

Raspberry PLC

family products



Read >>

TouchBerry Pi

family products



Read >>

How to program Raspberry PLC

interrupt inputs with Python



Read >>

Raspberry PLC

family products



Read >>

TouchBerry Pi

family products



Read >>

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 many open-source software as you like. One of our favourites 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 favourite browser and type the following to get into Node-RED.

http://10.10.10.20:1880/


Otherwise, connect your Raspberry PLC to the WiFi 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

    Odoo image and text block

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:


    Odoo image and text block

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!








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! 

Odoo image and text block

Looking for your ideal PLC?

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

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


Industrial PLC controller comparison >>

Do you want more information?

Just fill the form!

Tell me more!