How to start actions from a script

Revision as of 13:30, 24 May 2024 by Registered User

1. How to start some actions from an external script

1.1. Introduction

STM32CubeMonitor provides the environment to create flows to build dashboard or manage some action. Thanks to Finite State Machine provided in node-RED ecosystem, it is possible to build complex automatized process for your dashboards or to drive some action on STM32 devices. It is also possible to execute some "command line" action with the exec node. So most of the time, STM32CubeMonitor will manage your measurements automatically.

Some users need to drive the actions from an external script ( .bat, .sh, ....) or external tools. In this case, interacting with the graphical UI of STM32CubeMonitor is not efficient. This page will explain how it is possible to use the http node to allow a script or external application to trig actions in the STM32CubeMonitor flow.

1.2. The concept

As soon as STM32CubeMonitor is started, the flow are running. Even if the dashboard page is not open, flows are working in background. The solution is to add in the flow some http nodes to catch requests from user script and perform the actions requested. The command curl can be used easily in command line or script to send the request to STM32CubeMonitor. When the action is finished, the result is sent back in the http response.

Interaction between script and STM32CubeMonitor


1.3. The HTTP node

The HTTP node is used to send or receive HTTP request in Node-RED. For the action from script case, the "http in" and "http response" nodes are used.

Simple HTTP example

The "http in" node will receive a http request on url "demo" (http://localhost:1880/demo). The message is passed to a template node to prepare the answer, and then answer is sent back through "http response node".

The request can be sent from a command line with curl or from a web browser.

Test flow :

[{"id":"1fb8a74d55ae9749","type":"http in","z":"4eae8e47b3603ea6","name":"","url":"/demo","method":"get","upload":false,"swaggerDoc":"","x":300,"y":320,"wires":[["4e47e385987aad47"]]},{"id":"d92d3f76d1186408","type":"http response","z":"4eae8e47b3603ea6","name":"","statusCode":"","headers":{},"x":670,"y":320,"wires":[]},{"id":"4e47e385987aad47","type":"template","z":"4eae8e47b3603ea6","name":"Build answer ","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"The command is : {{payload.cmd}}\nThe parameter is : {{payload.param}}\n","output":"str","x":490,"y":320,"wires":[["d92d3f76d1186408"]]}]

To test the flow, go to "Menu > Import > Clipboard" and Copy/Paste the .json flow in the area, and then click on Import.

Then on a command line use : curl "http://localhost:1880/demo?cmd=test1&param=hello" or put "http://localhost:1880/demo?cmd=test1&param=hello" in a web browser. The result should be

>curl "http://localhost:1880/demo?cmd=test1&param=hello"
The command is : test1
The parameter is : hello

The element after the ? are parameters. In this example there is a parameter "cmd" and another parameter "param. The "template" node use it to build the answer. The name are free and the parameters can be used for any purpose.


1.4. The basic implementation

The previous example was just illustrating the http nodes, but not really usefull for the flow. Here is a more interesting flow, showing how to manage multiple commands, with immediate or delayed responses.

Show a very simple interaction between a script and a flow . Put the basic example

1.5. Example : Toggling Nucleo LED from a command line

Put the LED example here and explain how it is working

No categories assignedEdit