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

Machine Learning IoT App with Raspberry PLC


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 >>



  • Raspberry Pi industrial PLC 

  • USB camera


  • 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, it 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:

        Node-Red and TensorFlow.js Machine Learning IOT
        Node-Red and TensorFlow.js Machine Learning IOT
        Node-Red and TensorFlow.js Machine Learning IOT
        Node-Red and TensorFlow.js Machine Learning IOT

        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 node, each one next to the machine learning nodes. Your flow should look something like this:

        Node-Red and TensorFlow.js with Raspberry PLC

         Finally, get the output from the Debug nodes and check what the camera detects:

        Node-Red and TensorFlow.js with Raspberry PLC

        [{"id":"9b9cfa72.69ba48","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"a077ac31.78434","type":"debug","z":"9b9cfa72.69ba48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1230,"y":100,"wires":[]},{"id":"117091ec.1f05de","type":"exec","z":"9b9cfa72.69ba48","command":"fswebcam -r 1270x720 --no-banner ","addpay":"payload","append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":600,"y":180,"wires":[["65082784.5c9ce8"],[],[]]},{"id":"c760660d.c40c18","type":"inject","z":"9b9cfa72.69ba48","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":180,"wires":[["7a8d9a35.550bd4"]]},{"id":"7a8d9a35.550bd4","type":"function","z":"9b9cfa72.69ba48","name":"set abs image path","func":"var time = msg.payload;\nmsg.payload = '/home/pi/image-' + time + '.jpg';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":180,"wires":[["117091ec.1f05de","9f18e273.aa112"]]},{"id":"9f18e273.aa112","type":"change","z":"9b9cfa72.69ba48","name":"","rules":[{"t":"set","p":"image","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":120,"wires":[[]]},{"id":"98ec8792.1001d8","type":"cocossd","z":"9b9cfa72.69ba48","name":"","x":1060,"y":100,"wires":[["a077ac31.78434"]]},{"id":"f3b9ab64.91a258","type":"posenet","z":"9b9cfa72.69ba48","name":"","x":1060,"y":180,"wires":[["13988e16.cc41a2"]]},{"id":"13988e16.cc41a2","type":"debug","z":"9b9cfa72.69ba48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1230,"y":180,"wires":[]},{"id":"65082784.5c9ce8","type":"change","z":"9b9cfa72.69ba48","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"image","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":860,"y":180,"wires":[["98ec8792.1001d8","f3b9ab64.91a258","3fc1b816.d4d628"]]},{"id":"3fc1b816.d4d628","type":"handpose","z":"9b9cfa72.69ba48","name":"","x":1060,"y":260,"wires":[["e6c7928f.da4f5"]]},{"id":"e6c7928f.da4f5","type":"debug","z":"9b9cfa72.69ba48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1230,"y":260,"wires":[]}]

          Why Raspberry PLC for machine learning?

          Why Raspberry PLC for machine learning? 

          • Raspberry Pi OS with Debian (Linux).

          • Python: The favourite 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 licence 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.

                              Node-RED tutorial: Develop a Machine Learning IoT App with Raspberry PLC
                              Boot & Work Corp. S.L., Fernandez Queralt Martinez
                              21 May, 2021
                              Share this post

                              Looking for your ideal PLC?

                              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 >>

                              Do you want more information?

                              Just fill the form!

                              Tell me more!