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
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.
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.
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.
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:
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
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;
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!