Node-RED: First flow

Zápisník experimentátora

Hierarchy: Node.js

This article will show you how to create flows. It will be a simple example based on an example from the official documentation. The generated flow will have one input, one function that modifies the data and one output on the debug console. In the previous article, we've shown how Node-RED is installed and how it works with the flow editor. Let's create a first flow to help us imagine what the flows are good for.

Nodes

Drag-drop the three nodes into the editor. Connect each other.

  • input/inject - The node is named InjectedTimestamp in the image.
  • function/function - The node is named FuncTime in the image.
  • output/debug - The node is named Debug in the image.

Node inject

This node is used to create a message. It is an ideal node to try out Node-RED because it generates a message that travels across the flow when you press a button on a node. In this case it is set as a timestamp, which is the time in milliseconds since 1.1.1970. But you can choose from many options. For example, you can create a message flow that contains time and is generated every N seconds.

This is just an example of creating one type of input. But anything can be input. For example, in the continuation of this article, we will intensively use two types of input nodes.

  • serial - We will use it to connect Arduino Uno, which will generate any output to the serial port.
  • http - We will connect ESP8266 with this node.

Node function

You can use the function to modify the message on a given node. The illustration shows an example of a function that changes the original message to another. In this case, we create a Date object from the original value, in milliseconds, to create three attributes of the new object that will continue as the message in the flow. Functions are programmed in javascript. Function toString converts the date into a text string in your language. Function toISOString converts the date to a more versatile form, understood by a programmer in any country in the world.

Node debug

This type of node is used for listing variables. When designing flows, you need to make sure that everything works right for you. Therefore, during design, you usually connect several such nodes to important nodes, leaving only the most important ones in the flow when you have debugged the entire flow.

Debug console output

After pressing the button on the inject node, a message is created that contains the time in milliseconds since 1.1.1970. The message is converted to a three-parameter object in the function node. The object is listed on the console in the debug node. The picture shows all three src, asdate and asiso attributes.

Source code

You can paste the source code of example into your project using the function Import.

[
{"id":"be3232a0.de3ee","type":"tab","label":"Flow 1","disabled":false},{"id":"6e0c7bbd.5d5cac","type":"inject","z":"be3232a0.de3ee","name":"InjectedTimestamp","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":"","x":197,"y":100,"wires":[["52d2378d.017e48"]]},
{"id":"2ee8df4.1d535a","type":"debug","z":"be3232a0.de3ee","name":"Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":410,"y":138,"wires":[]},
{"id":"52d2378d.017e48","type":"function","z":"be3232a0.de3ee","name":"FuncTime","func":"var date = new Date(msg.payload);\nmsg.payload = {\n    src: msg.payload,\n    asdate: date.toString(),\n    asiso: date.toISOString()\n};\nreturn msg;","outputs":1,"noerr":0,"x":313,"y":259,"wires":[["2ee8df4.1d535a"]]}
]

28.03.2019


Menu