Node-RED usage tutorial one

Vue d'ensemble

This tutorial introduces the Node-RED editor and creates a flow that demonstrates Inject, Debug, and Function nodes.

1. Access the editor

With Node-RED running, open the editor in a web browser.

If you are using a browser on the same computer where Node-RED is running, you can access it using the url: http://localhost:1880.

If you are using the browser on another computer, you will need to use the IP address of the computer running Node-RED: http:// :1880.

2. Add an Inject node

The Inject node allows you to inject messages into the stream by clicking a button on the node or by setting a time interval between injections.

Drag one from the palette to the workspace.

Select the newly added Inject node to view information about its properties and a description of its role in the Information sidebar pane.

3. Add a Debug node

The Debug node causes any messages to be displayed in the Debug sidebar. By default it only displays the payload of the message, but the entire message object can be displayed.

4. Connect the two together

Connect the Inject and Debug nodes together by dragging between the output port of one and the input port of the other.

5. Deployment

At this point the node only exists in the editor and must be deployed to the server.

Click the deploy button.

6.Injection

With the Debug sidebar tab selected, click the Inject button (the small square button next to the Inject node). You should see the number appear in the sidebar. By default, the Inject node uses the number of milliseconds since January 1, 1970 as its payload.

7. Add function nodes

The Function node allows you to pass each message through a JavaScript function.

Delete the existing wire (select it and press Delete on your keyboard).

Connect a Function node between the Inject and Debug nodes.

Double-click the function node to open the edit dialog box. Copy the following code into the function field:

// Create a Date object from the payload var date = new Date(msg.payload);

// Change the payload to be a formatted Date string msg.payload = date.toString();

// Return the message so it can be sent on return msg;

Click Finish to close the edit dialog box and click the Deploy button.

Messages in the sidebar will now be formatted with a readable timestamp when you click the “Inject” button.

generalize

This process demonstrates the basic concepts of creating a process. It shows how to use the Inject node to trigger the process manually, and how the Debug node displays messages in the sidebar. It also shows how to use Function nodes to write custom JavaScript to run against messages.

resource

The process created in this tutorial is represented by the following json. To import it into the editor, copy it to the clipboard and paste it into the Import dialog box.

[{“id”:”58ffae9d.a7005″,”type”:”debug”,”name”:””,”active”:true,”complete”:false,”x”:640,”y”:200 ,”wires”:[]},{“id”:”17626462.e89d9c”,”type”:”inject”,”name”:””,”topic”:””,”payload”:””,” repeat”:””,”once”:false,”x”:240,”y”:200,”wires”:[[“2921667d.d6de9a”]]},{“id”:”2921667d.d6de9a”, “type”:”function”,”name”:”Format timestamp”,”func”:”// Create a Date object from the payloadnvar date = new Date(msg.payload);n// Change the payload to be a formatted Date stringnmsg.payload = date.toString();n// Return the message so it can be sent onnreturn msg;”,”outputs”:1,”x”:440,”y “:200,”wires”:[[“58ffae9d.a7005”]]}]

Nous contacter