Node-RED: Debug HTTP endpoint

Zápisník experimentátora

Hierarchy: Node.js

I'm working on continuing the article on Node-RED. In the following article, I will deal with ESP8266 and the DS18B20 temperature sensor. But before we program it all, I'll write this article about HTTP server debugging, which is basically the flow I'm preparing for ESP8266. ESP8266 acts as an HTTP client and Node-RED as an HTTP server.

Debugging both programs at once is unnecessarily complicated and slow. If you have a clear idea of the interface that both programs will use, you can greatly simplify development. And you can also prepare a set of tests to test this interface.

Postman

You can find the Postman program at https://www.getpostman.com/. This program allows you to design a set of HTTP requests and analyze responses that the HTTP server sends back. You can create HTTP requests for all commonly used HTTP communication methods. We will only deal with POST requests that send and receive content as JSON data. But nothing prevents you from communicating in other ways.

I use Postman because it makes it easy to make a few requests for the same URLs that send different parameters. This allows you to conveniently test the behavior of your interface for various errors. In the article on ESP8266 we will deal with the treatment of various errors, so I designed a Node-RED interface that will be fault-tolerant.

The resulting HTTP request must look the same regardless of whether it is created by Postman or ESP8266. As a result, I do not need to record all the tests in ESP8266, but I can prepare them conveniently. When I am satisfied with the answers from the server, I will program the final code for ESP8266.

Sample data

Sample data includes array of sensor and temperature information. This is how the array with data is created correctly.

[
    {
        "id": "0011223344556677",
        "value": 25
    },
    {
        "id": "0011223344556688",
        "value": 26
    }
]

I suggested three checks to test the submitted data on the server. Only the correct data is further processed in the flow and any errors are sent in response to the HTTP client.

  • The POST request must have a JSON array as the body.
  • Array item must be an object with id and value parameters.
  • The id parameter must have 16 hexadecimal characters, which is derived from the DS18B20 temperature sensor identifier.

HTTP request

The following three images show how the HTTP requests look and what response came back from the HTTP server.

Completely poorly prepared body of request

Request body as array but with incorrect parameters

Correctly prepared HTTP request body

Error debugging in Node-RED

In Node-RED, you can type debugging information on the console in Function nodes, but you must use the node.log command instead of console.log.


23.04.2019


Menu