Node-RED Tutorial: How to use HTTP nodes with the industrial Raspberry PLC

Learn how to HTTP request for industrial and Raspberry Pi automation
September 22, 2021 by
Node-RED Tutorial: How to use HTTP nodes with the industrial Raspberry PLC
Boot & Work Corp. S.L., Fernandez Queralt Martinez

Introduction

HTTP stands for hypertext transfer protocol. It is a protocol that allows communication between different systems. It is most commonly used to transfer data from a web server to a browser to view web pages.

In this post, you will learn how to create an HTTP endpoint to be used with HTTP client and HTTP server in the industrial automation area and with Raspberry Pi-based PLC.

Related links

How to

Connect an open source Raspberry PLC to Wi-Fi

Read > 

Raspberry PLC

Family products for industrial automation

See > 

How to

Program Raspberry PLC Interrupt inputs with Python

Read > 

Basics about

Raspberry Pi industrial PLC Analog Outputs

Read > 

Touchberry Pi

Family products for industrial control

See > 

How to

Find your perfect industrial PLC

Read > 


Requirements

HTTP

According to MDN, HTTP is a protocol that allows the fetching of resources, such as HTML documents. It is the basis of any data exchange on the Web and it is a client-server protocol, which means that requests are initiated by the recipient, usually the Web browser. A complete document is reconstructed from the different sub-documents obtained, for instance, text, layout description, images, videos, scripts, and more.

Clients and servers communicate by exchanging individual messages (instead of a data stream). Messages sent by the client, usually a Web browser, are called requests, and the messages sent by the server in feedback are called responses.

Starting

If this is your first time developing an application with Node-RED, take a look at the website of Node-RED to learn how to create your first flow. >>>

Problem

So, what you want to do is to create an HTTP endpoint that responds to GET requests with some static content, such as an HTML page or CSS stylesheet.

Solution

For this, you are going to use the HTTP In node to listen for requests, a Template node to include the static content, and an HTTP Response node to reply to the request.

HTTP Nodes

As you are going to create a very basic example, you are just going to add three nodes:

1. The first one, an HTTP IN node to create an HTTP endpoint. Set a GET method to your custom URL. In our case: /test

Odoo • Image and Text

2. The second one will be a function template node to set the property: msg.payload based on the provided template, where we will type our HTML entities.

<html>
    <head></head>
    <body>
        <h1>YOUR-MESSAGE-HERE</h1>
    </body>
</html>
Odoo • Image and Text

3. The third one will be the HTTP Response node to send responses back to requests received from the HTTP Input node.

This is how your flow will look like:

[{"id":"f2c95db5.ba59a","type":"tab","label":"Flow 1","disabled":false,"info":"Examples of how to scan for devices"},{"id":"59ff2a1.fa600d4","type":"http in","z":"f2c95db5.ba59a","name":"","url":"/test","method":"get","upload":false,"swaggerDoc":"","x":340,"y":100,"wires":[["54c1e70d.ab3e18"]]},{"id":"54c1e70d.ab3e18","type":"template","z":"f2c95db5.ba59a","name":"page","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n    <head></head>\n    <body>\n        <h1>Hi everyone from Industrial Shields!</h1>\n    </body>\n</html>","x":550,"y":100,"wires":[["266c286f.d993d8"]]},{"id":"266c286f.d993d8","type":"http response","z":"f2c95db5.ba59a","name":"","statusCode":"","headers":{},"x":770,"y":100,"wires":[]}]
Odoo • Image and Text

Finally, deploy your flow, and add the same URL as we set in the first node right after the localhost:1880 in our case /test. So, go to:

localhost:1880/test
and check the HTTP message you just got!

HTTP Recipes

Want to know more? Take a look at the HTTP recipes >>> from Node-RED and learn how to serve JSON content, post raw data to a flow, or get a parsed JSON response, among others!

​Search in our Blog

Node-RED Tutorial: How to use HTTP nodes with the industrial Raspberry PLC
Boot & Work Corp. S.L., Fernandez Queralt Martinez September 22, 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 >>>