Using Node-RED Part 1 (exec node, Execute Python programs)

Introduction

 In this article, I used exec node of Node-RED. This is a node that can execute commands.

 I started using Node-RED MCU first, so I have not used Node-RED itself much. It is a very useful software and I would like to learn how to use it.

 Note that the environment is running Node-RED on a Raspberry Pi.

▼I use Raspberry Pi 400.

▼I recently wrote about creating a node in the Node-RED Advent Calendar 2023. This is a node for servo motors.

https://qiita.com/background/items/9b820251aa9dda5a3167

▼I use the repository as an example.

https://github.com/404background/node-red-mcu-servo

Using exec node

Creating Folders

 First, I created a folder to check the operation. Basically, it is the same as when running with LX Terminal, but you need to be careful about the current directory.

▼As you will see when you run it, the following two flows work differently.

 In the above flow, the folder is created after moving to Desktop. The exec folder was properly created in the Desktop.

 However, in the below flow, the exec folder is created in the /home/pi directory. It seems that the behavior is such that each exec node is executed in its home directory.

 So be careful with the file paths.

git commands

 I tried git clone and git pull.

▼The exec node can specify an argument, passing the URL of the clone destination.

▼When executed, the repository was cloned.

 I could also execute git pull.

▼Here is the flow.

▼Already up to date is displayed.

npm commands

 まずはnpm initコマンドを実行して、package.jsonを作成してみました。

▼npmフォルダを作成してから、npm initを実行します。

▼デバッグノードに結果が表示されています。

▼package.jsonが作成されていました。

 先程git cloneでインストールしたフォルダを、npmでインストールしてみました。

▼フローはこちら。こちらも引数にフォルダのパスを指定することで、injectノードからペイロードを送れば実行できます。

▼node_modulesフォルダと、package-lock.jsonが生成されています。npm installを実行すると用意されるものです。

Restart Node-RED

 In my environment, I am running Node-RED as a service. I often use this command when I update a node.

▼You can restart with the following flow.

Execute Python programs

 In this case, I prepared a hello.py file, which just displays hello.

▼I created a Python folder in the /home/pi directory.

▼Only print('hello').

▼If you run the command, you will see hello.

 I run hello.py after moving to the Python directory. I passed the filename as an argument to make it easier to execute even if there are multiple files.

▼Here is the flow. I pass the filename as an argument.

▼When I ran it, it also displayed hello in the debug field.

Finally

 I could run the commands I usually use, so it seems convenient to use.

 I always opened LX Terminal for each directory, so my taskbar was crowded. if I prepare commands that I often use in exec node, it will save me a lot of typing.

 I think this would also allow me to create a flow to prepare the Node-RED MCU environment.

▼I know that building the environment is a lot of work. I think I'm getting used to the commands because of that, though.

Node-RED MCUの環境を構築する(ESP-IDF v5.1.1対応版、Raspberry Pi)

はじめに  Moddable SDKがv4.2にアップデートされたことにより、Node-RED MCUの環境が大きく変化しています。特にESP-IDF v5に対応したことで、ESP-IDF v4のままだとエラ…

Leave a Reply

Your email address will not be published. Required fields are marked *