Try everything๐Ÿ˜Ž

Node-RED/Windows ๋ณธ๋ฌธ

์•„๋‘์ด๋…ธ

Node-RED/Windows

d021_n 2020. 8. 2. 19:09

_9.Piano.ino.ino
0.00MB
_4.HC_SR04.ino.ino
0.00MB
_3.PIR_Sensor.ino.ino
0.00MB

์„ค๋ช…

 

User Guide : Node-RED

User Guide

nodered.org

์„ค์น˜ํ•ด์•ผ ํ•  ๊ฒƒ ๋งํฌ

 

Running on Windows : Node-RED

Running on Windows This page gives specific instructions on setting up Node-RED in a Microsoft Windows environment. The instructions are specific to Windows 10 but should also work for Windows 7 and Windows Server from 2008R2. It is not advisable to use ve

nodered.org

Running on Windows

This page gives specific instructions on setting up Node-RED in a Microsoft Windows environment. The instructions are specific to Windows 10 but should also work for Windows 7 and Windows Server from 2008R2. It is not advisable to use versions prior to Windows 7 or Windows Server 2008R2 due to lack of current support.

Note : Some of the following instructions mention the "command prompt". Where this is used, it refers to either the Windows cmd or PowerShell terminal shells. It is recommended to use PowerShell on all newer versions of Windows as this gives you access to commands and folder names that are closer to those of Linux/Mac.

Quick Start

1. Install Node.js

๋‚œ ์˜ค๋ฅธ์ชฝ์„ ํƒํ•˜๊ฒ ์–ด!!

Download the latest 12.x LTS version of Node.js from the official Node.js home page. It will offer you the best version for your system.

Run the downloaded MSI file. Installing Node.js requires local administrator rights; if you are not a local administrator(๊ด€๋ฆฌ์ž, ํ–‰์ •์ธ), you will be prompted((์–ด๋–ป๊ฒŒ ํ•˜๋„๋ก·๋˜๋„๋ก) ํ•˜๋‹ค, ์ด‰๋ฐœํ•˜๋‹ค, ์ž๊ทนํ•˜๋‹ค, ๊ณ ๋ฌดํ•˜๋‹ค, ๋ถ€์ถ”๊ธฐ๋‹ค(incite, instigate)) for an administrator password on install. Accept the defaults when installing. After installation completes, close any open command prompts and re-open to ensure new environm

ent variables are picked up.

Once installed, open a command prompt and run the following command to ensure Node.js and npm are installed correctly.

Using Powershell: node --version; npm --version

์œˆ๋„์šฐ ํ‚ค+r => cmd

Using cmd: node --version && npm --version

You should receive back output that looks similar to:

v12.15.0 6.14.5

2. Install Node-RED

Installing Node-RED as a global module adds the command node-red to your system path. Execute the following at the command prompt:

npm install -g --unsafe-perm node-red

deprecated : ์ค‘์š”๋„๊ฐ€ ๋–จ์–ด์ ธ ๋” ์ด์ƒ ์‚ฌ์šฉ๋˜์ง€ ์•Š๊ณ  ์•ž์œผ๋กœ๋Š” ์‚ฌ๋ผ์ง€๊ฒŒ ๋  (์ปดํ“จํ„ฐ ์‹œ์Šคํ…œ ๊ธฐ๋Šฅ ๋“ฑ)

๋œ๊ฑด๊ฐ€.......

3. Run Node-RED

Once installed, you are ready to run Node-RED.

Running on Windows 

Once installed, the simple way to run Node-RED is to use the node-red command in a command prompt: If you have installed Node-RED as a global npm package, you can use the node-red command:

C:>node-red

Server now running at ~~~๋งํฌ๋ฅผ ๊ฒ€์ƒ‰ํ•ด์„œ ๋“ค์–ด๊ฐ€๋‹ˆ 

์ด๋Ÿฐ ํ™”๋ฉด์ด ๋–ด๋‹ค! (์•„๋ž˜ ๋งํฌ)

 

 

Workspace : Node-RED

Workspace The main workspace is where flows are developed by dragging nodes from the palette and wiring them together. The workspace has a row of tabs along the top; one for each flow and any subflows that have been opened. Reference Actioncore:zoom-in Key

nodered.org

This will output the Node-RED log to the terminal. You must keep the terminal open in order to keep Node-RED running.

Note that running Node-RED will create a new folder in your %HOMEPATH% folder called .node-red. This is your userDir folder, think of it as the home folder for Node-RED configuration(1. ๋ฐฐ์—ด, ๋ฐฐ์น˜; ๋ฐฐ์—ด[๋ฐฐ์น˜] ํ˜•ํƒœ 2. ํ™˜๊ฒฝ ์„ค์ •) for the current user. You will often see this referred to as ~/.node-red in documentation. ~ is shorthand for the user home folder on Unix-like systems. You can use the same reference if using PowerShell as your command line as recommended. If you are using the older cmd shell, that won’t work.

You can now create your first flow.

 

Creating your first flow

Overview

This tutorial introduces the Node-RED editor and creates a flow the demonstrates the 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 that is running Node-RED, you can access it with the url: http://localhost:1880. <- cmd์— node-red ์ž…๋ ฅํ•œ ํ›„์—์•ผ ๋“ค์–ด๊ฐ€์ง

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

2. Add an Inject node

 

 

Workspace : Node-RED

Workspace The main workspace is where flows are developed by dragging nodes from the palette and wiring them together. The workspace has a row of tabs along the top; one for each flow and any subflows that have been opened. Reference Actioncore:zoom-in Key

nodered.org

 

The Inject node allows you to inject(1. ์ฃผ์‚ฌํ•˜๋‹ค 2. (์•ก์ฒด๋ฅผ) ์ฃผ์ž…ํ•˜๋‹ค) messages into a flow, either by clicking the button on the node, or setting a time interval(1. (๋‘ ์‚ฌ๊ฑด ์‚ฌ์ด์˜) ๊ฐ„๊ฒฉ 2. (์—ฐ๊ทน·์˜ํ™”·์ฝ˜์„œํŠธ์˜) ์ค‘๊ฐ„ ํœด์‹ ์‹œ๊ฐ„) between injects.

Drag one onto the workspace from the palette.

Select the newly added Inject node to see information about its properties and a description of what it does in the Information sidebar pane.

3. Add a Debug node

The Debug node causes any message to be displayed in the Debug sidebar. By default, it just displays the payload of the message, but it is possible to display the entire message object.

4. Wire the two together

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

5. Deploy

At this point, the nodes only exist in the editor and must be deployed to the server.

Click the Deploy button.

With the Debug sidebar tab selected, click the Inject button. You should see numbers appear in the sidebar. By default, the Inject node uses the number of milliseconds since January 1st, 1970 as its payload.

6. Add a Function node

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

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

Wire a Function node in between the Inject and Debug nodes.

Double-click on the Function node to bring up the edit dialog. 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 Done to close the edit dialog and then click the deploy button.

Now when you click the Inject button, the messages in the sidebar will now be formatted is readable timestamps.


Summary

This flow demonstrates the basic concept of creating a flow. It shows how the Inject node can be used to manually trigger a flow, and how the Debug node displays messages in the sidebar. It also shows how the Function node can be used to write custom JavaScript to run against messages.

Source

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

[{"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 payload\nvar date = new Date(msg.payload);\n// Change the payload to be a formatted Date string\nmsg.payload = date.toString();\n// Return the message so it can be sent on\nreturn msg;","outputs":1,"x":440,"y":200,"wires":[["58ffae9d.a7005"]]}]

Next Steps

 

Writing Functions : Node-RED

Writing Functions The Function node allows JavaScript code to be run against the messages that are passed through it. The message is passed in as an object called msg. By convention it will have a msg.payload property containing the body of the message. Ot

nodered.org

Using PM2

If you are using Windows to develop Node-RED flows or nodes, you may find it helpful to use PM2 to run Node-RED. This can be configured to automatically restart when files change, always keep Node-RED running and manage log output.

Run Node-RED on Startup

If you want to use Windows as a production platform for Node-RED, you will want to have a Windows Task Scheduler job set up. To do so:

  1. Go to the start menu and type “task scheduler” and click on the result.
  2. Click on “Create Task…” in the right-hand menu. Follow the steps to create a new task.

Make sure that you use the user login that you’ve used to set up and do the initial run of Node-RED. You can use an “At startup” trigger to always run Node-RED at system startup. Use the Action “Start a program” with details set to C:\Users\<user>\AppData\Roaming\npm\node-red.cmd (replacing <user> with your actual user name).

You may wish to make sure that it only starts if the network is available. You may also wish to restart if the job fails. Perhaps restarting every minute but only 3 times - if it won’t start by then, the error is fatal and will need some other intervention. You can check for failures by looking in the event log. If you want to access to the logs when running this way, you should amend the node-red.cmd file to redirect std and error outputs to a file (creating an alternative startup file would be better so that it isn’t overwritten on updates).

 

Interacting with Arduino : Node-RED

Interacting with Arduino There are several ways to interact with an Arduino using Node-RED. They all assume the Arduino is connected to the host computer via a USB serial connection. Note: you can’t use both the Arduino IDE and the Arduino nodes at the s

nodered.org

Interacting with Arduino

There are several ways to interact with an Arduino using Node-RED. They all assume the Arduino is connected to the host computer via a USB serial connection.

Note: you can’t use both the Arduino IDE and the Arduino nodes at the same time as they will conflict. You will need to stop Node-RED running if you wish re-program the Arduino from the IDE.

Serial

As the Arduino appears as a Serial device, the Serial in/out nodes can be used to communicate with it.

This is normally the case if you program the Arduino with the IDE, as you can then send and receive input over the serial port to interact with your creation. Just make sure you set the serial port speed (baud rate) to be the same at both ends.

Firmata

Firmata is a protocol for communicating between an Arduino (as well as other microcontrollers) and the host computer, providing direct access to the IO pins.

 

Installation

First you need to load the default Firmata sketch onto the Arduino using the standard Arduino software download tools. This is usually found in the Arduino IDE under the menu:

Files - Examples - Firmata - Standard Firmata

You then need to install the Node-RED Arduino nodes into the palette.

๋ญ”์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์œผ๋‚˜ ์„ค๋ช…์„ ๋”ฐ๋ผ ๋“ค์–ด๊ฐ€๋‹ˆ ์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋“ค์ด ์จ์žˆ์—ˆ๋‹ค. ๋งํฌ๋ฅผ ๋”ฐ๋ผ ๋“ค์–ด๊ฐ€ ๋ณด์•˜๋‹ค (์•„๋ž˜)

 

firmata/arduino

Firmata firmware for Arduino. Contribute to firmata/arduino development by creating an account on GitHub.

github.com

์˜ค๋ฅธ์ชฝ releases๋“ค์–ด๊ฐ€ ์••์ถ•ํด๋” 2๊ฐœ๋ฅผ ๋‹ค์šด๋ฐ›์•˜๋‹ค

Please check that `npm -v` is at least version 2.x - if not - update it using sudo npm i -g npm@latest hash -r

Change directory to your Node-RED user directory, this is normally ~/.node-red

cd ~/.node-red

Then install the Arduino nodes

npm install node-red-node-arduino

Finally restart Node-RED, and reload the editor in the browser. There should now be two new Arduino nodes in the palette.

Blink

To run a “blink” flow that uses LED 13, copy the following flow and paste it into the Import Nodes dialog (Import From - Clipboard in the dropdown menu, or Ctrl-i, Ctrl-v). After clicking okay, click in the workspace to place the new nodes.

[{"id":"d7663aaf.47194","type":"arduino-board","device":""},
{"id":"dae8234f.2517e","type":"inject","name":"0.5s tick",
"topic":"","payload":"","payloadType":"date","repeat":"0.5","crontab":"",
"once":false,"x":150,"y":100,"z":"359a4b52.ca65b4","wires":[["56a6f8f2.a95908"]]},
{"id":"2db61802.d249e8","type":"arduino out","name":"","pin":"13","state":"OUTPUT",
"arduino":"d7663aaf.47194","x":570.5,"y":100,"z":"359a4b52.ca65b4","wires":[]},
{"id":"56a6f8f2.a95908","type":"function","name":"Toggle output on input","func":
"\n// If it does exist make it the inverse of what it was or else initialise it to false\n//
(context variables persist between calls to the function)\ncontext.level
= !context.level || false;\n\n// set the payload to the level and return\nmsg.payload 
= context.level;\nreturn msg;","outputs":1,"noerr":0,"x":358,"y":100,"z":"359a4b52.ca65b4",
"wires":[["2db61802.d249e8"]]}]

This flow is set to automatically try to detect the board on a a serial port. If you need to change that, double click the node labelled Pin 13 - the Arduino node. Click the pencil icon and change the port definition as needed.

Click the deploy button and the flow should start running. LED 13 should start toggling on and off once a second.

Capabilities

The Arduino output node currently supports three modes of operation:

  • Digital - 0 or 1
  • Analogue - 0 to 255
  • Servo - 0 to 180

The Arduino input node, available in the palette but not used in this example, can support both Digital and Analog pins. The input will send a message whenever it detects a change. This may be okay for digital inputs as they tend to be fairly stable, but analog readings often end up being at the full sample rate (default: 40 times a second…). This can be changed in the configuration of the serial port to reduce it to a more manageable rate.

Details of the Node.js arduino-firmata library can be found here.


Johnny-Five

You may also use the popular Johnny-Five library as this adds capabilities like I2C.

One way to use it is via Luis Montes’ node-red-contrib-gpio node, which also adds support for a number of other boards, such as Raspberry Pi, BeagleBone Black, Galileo/Edison, Blend Micro, LightBlue Bean, Electric Imp and Spark Core, in a consistent manner.

Another way is to make it available within functions. This can be achieved by editing the globalContextSettings sections of settings.js to be

functionGlobalContext: { jfive:require("johnny-five"), // this is the reference to the library j5board:require("johnny-five").Board({repl:false}) // this actually starts the board link... },

We start the board link here so that multiple functions within the workspace can use it, though you should be careful to only access each pin once.

Finally install the npm from within your Node-RED home directory

cd ~/.node-red npm install johnny-five

and then you may access all the richness of Johnny-Five from within functions…

var five = context.global.jfive; // create a shorter alias var led = new five.Led(13); // instatiate the led led.blink(500); // blink it every 500 ms

Note: this is a simple, but poor example as the led pin is created each time the function is called… so only ok if you only deploy it and call it once.

Blink 2

The flow below shows a more advanced example that turns on and off a flashing led, and shows the use of context to hold the state and a single instance of the led pin.

It can be imported to the workspace by using ctrl-c (copy) / ctrl-i (import) / ctrl-v (paste).

[{"id":"62f58834.9d0a78","type":"inject","name":"",
"topic":"","payload":"1","payloadType":"string","repeat":"",
"crontab":"","once":false,"x":226,"y":326,"z":"359a4b52.ca65b4",
"wires":[["ae84ad08.517b5"]]},{"id":"ae84ad08.517b5","type":"function",
"name":"1 = start flash, 0 = stop","func":"var five = context.global.jfive;
\ncontext.led = context.led || new five.Led(13);\ncontext.switch = context.switch 
|| 0;\ncontext.switch = msg.payload;\nconsole.log(typeof(context.switch));\nif
(context.switch == 1) {\n context.led.blink(500);\n}\nif (context.switch == 0) 
{\n context.led.stop().off();\n}\nreturn msg;","outputs":1,"noerr":0,"x":447,"y":
349,"z":"359a4b52.ca65b4","wires":[["df638a80.209c78"]]},{"id":"df638a80.209c78","type":
"debug","name":"","active":true,"console":"false","complete":"false","x":645,"y":349,"z":
"359a4b52.ca65b4","wires":[]},{"id":"d79bc51d.286438","type":"inject","name":"","topic":"",
"payload":"0","payloadType":"string","repeat":"","crontab":"","once":false,"x":
224.4000244140625,"y":364.60003662109375,"z":"359a4b52.ca65b4","wires":[["ae84ad08.517b5"]]}]