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


INTRODUCTION

HTTP stands for hypertext transfer protocol. It's a protocol that allows communication between different systems. Most commonly, it is used for transferring data from a web server to a browser to view web pages.

In this blog, we are going to show you how to create an HTTP endpoint to be able to use it with HTTP client and HTTP server in the industrial automation area.

 RELATED LINKS

How to

Connect Raspberry PLC to Wi-Fi

Read 

Raspberry PLC

Family products

See 

How to

Program Raspberry PLC Interrupt inputs with Python

Read 

Basics about

Raspberry Pi PLC Analog Outputs

Read 

Touchberry Pi

Family products

See 

How to

Find your perfect industrial PLC

Read 


 REQUIREMENTS

  • Power Supply >>>

  • Either Ethernet cable or external monitor and keyboard to access to the Raspberry PLC

HTTP

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

Clients and servers communicate by exchanging individual messages (as opposed to a stream of data). The messages sent by the client, usually a Web browser, are called requests and the messages sent by the server as an answer 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 we 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, we 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 we are going to create a very basic example, we are just going to add three nodes:

1. The first one, a HTTP IN node to create an HTTP end-point. 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 in order to send responses back to requests received from the HTTP Input node.

This is how our 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!

Node-RED Tutorial: How to use HTTP nodes with the Raspberry PLC
Boot & Work Corp. S.L., Fernandez Queralt Martinez
22 September, 2021
Share this post
Archive

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!