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
Requirements
- Industrial Raspberry Pi PLC controller >>>
- Power Supply >>>
- Either Ethernet cable or external monitor and keyboard to access the open source PLC Raspberry Pi.
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
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>
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":[]}]
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/testand 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 industrial Raspberry PLC