Node-RED tutorial: Develop a Machine Learning IoT App with Raspberry PLC

Learn how to use TensorFlow.js with industrial Raspberry Pi PLC based family
May 21, 2021 by
Node-RED tutorial: Develop a Machine Learning IoT App with Raspberry PLC
Boot & Work Corp. S.L., Fernandez Queralt Martinez


Machine learning is an application of artificial intelligence (AI) that provides systems the ability to learn and improve automatically from experience without being explicitly programmed. 

TensorFlow is an end-to-end open-source platform for machine learning. It has a comprehensive, flexible ecosystem of tools, libraries, and community resources that let developers easily build and deploy ML-powered applications.

In this post, you are going to learn how to develop open-source projects using open source platforms, such as Node-RED and TensorFlow! Let's get started!

Related links

How to connect 
Raspberry PLC to Wi-Fi

Read >>

Basics about Raspberry Pi PLC Analog outputs

Read >>

How to find your
perfect industrial PLC

Read >>

How to program Raspberry PLC Interrupt inputs with Python

Read >>

Raspberry PLC
family products

Read >>

Touchberry Pi
family products

Read >>




  • Node-RED
  • fswebcam package in the Raspberry


              Learn how to build and deploy machine learning apps that can run offline and directly on any Raspberry Pi PLC controller. Using Node-RED with TensorFlow.js you will be able to incorporate machine learning into your device in a very easy and low-code way!

              Machine Learning: An application of Artificial Intelligence (AI) that focuses on the development of computer programs that can access data and use it to learn for themselves.

              Node-RED: A flow-based programming tool for connecting hardware devices, APIs and online services. Really useful for open source projects.

              TensorFlow.js: A JavaScript library for training and developing machine learning models in the browser and on Node.js.

              The combination of Node-RED and TensorFlow.js is a perfect match for creating and developing Industrial and IoT applications, adding machine learning functionality to your devices!


              Nodes installation

              By default, in the Raspberry Pi based family of products, Node-RED is already installed. You can connect to the industrial Raspberry PLC remotely by putting the IP address in the URL section of your browser. Connect an Ethernet cable to the Raspberry Ethernet port, and type the following:


              The Node-RED flow library has several TensorFlow.js-enabled nodes. One of these if node-red-contrib-tensorflow, which contains the trained models.

              To install the node, go to the top-right menu of the flow editor > click "Manage Palette" > select the Install tab > look for: node-red-contrib-tensorflow. > Click on install.

              Node-Red and TensorFlow.js

              Once the installation is complete, go to the Analysis category, and you will see four node-red-contrib-tensorflow nodes. These are all image recognition nodes, but they can also generate image data with annotation and perform other functions like image recognition, or offline, which is necessary for edge analytics.

              • Cocossd: A node that returns the name of the object in the image.
              • Handpose: A node that estimates the positions of fingers and joints from a hand image.
              • Mobilenet: The node that classifies images with MobileNet
              • Posenet: A node that estimated the position of arms, head, and legs from the image of a person.

              Creating a flow

              In this post, you are going to use the cocossd, handpose and posenet nodes, because what our application will do, is to take a picture, to detect fingers in a hand and to estimate human pose. So, you will take a picture with our USB camera using the fswebcam command, our TensorFlow nodes will process the information, and you will get the data through our debug nodes.

              Then, you are going to add an exec node to run the fswebcam command so that it takes a picture:

              fswebcam -r 1270x720 --no-banner {absolute-path}

              You can just run the command like this: fswebcam myimage.jpg

              As you, we are going to name the pictures: image-{timestamp}.jpg, you are going to append the timestamp injecting it from an inject node as shown in the pictures below, and you are going to set that name to a flow variable called flow.image:

              In the inject node, you can inject once every X seconds, and then repeat every X seconds so that the application works in a specified time interval.

              Once the picture is taken, let's get the flow.image and set it to the msg.payload, so that you can send it to our TensorFlow nodes. Just connect a change node to the stdout of the exec node, set the msg.payload to flow.image, and connect the cocossd, posenet and handpose after the change node. Wire three debug nodes, each one next to the machine learning nodes. Your flow should look something like this:

                Why Raspberry PLC for Machine Learning?

                • Raspberry Pi OS with Debian (Linux).
                • Python: The favorite programming language for machine learning.
                • It has different Analog/Digital/Relay I/Os to receive and send the data from.
                • Open solution: Open Source. No license fees. 
                • Equipment designed and manufactured for industrial use at a lower price than competitive products.
                • Modular solution: Product specifications can be scalable in the future.

                Want to learn how to capture data from a sensor with a Raspberry PLC and Node-Read?

                Read the following link.

                                    ​Search in our Blog

                                    Node-RED tutorial: Develop a Machine Learning IoT App with Raspberry PLC
                                    Boot & Work Corp. S.L., Fernandez Queralt Martinez May 21, 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 >>>