How to start actions from a script

Revision as of 14:32, 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 http actions implementation

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

Flow used to managed actions from http requests
[{"id":"b6723204.12023","type":"http in","z":"60ec2a4b.5ad324","name":"","url":"/cli","method":"get","upload":false,"swaggerDoc":"","x":270,"y":320,"wires":[["f0aa7f90.176ca"]]},{"id":"b7bfe1ac.9ea71","type":"http response","z":"60ec2a4b.5ad324","name":"","statusCode":"","headers":{},"x":1070,"y":360,"wires":[]},{"id":"f0aa7f90.176ca","type":"switch","z":"60ec2a4b.5ad324","name":"","property":"payload.cmd","propertyType":"msg","rules":[{"t":"eq","v":"test1","vt":"str"},{"t":"eq","v":"test2","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":3,"x":410,"y":320,"wires":[["90147b04.5474d8"],["dd4df7c0.d02e78","d1ae2912.4a8b28"],["5ed52e43.7efaa"]]},{"id":"5ed52e43.7efaa","type":"template","z":"60ec2a4b.5ad324","name":"unknown command","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Invalid command","x":630,"y":500,"wires":[["b7bfe1ac.9ea71"]]},{"id":"568331f2.a1eae","type":"comment","z":"60ec2a4b.5ad324","name":"receive HTTP request","info":"curl --silent http://localhost:1880/cli?cmd=test1\ncurl --silent \"http://localhost:1880/cli?cmd=test1&param=hello\"","x":340,"y":260,"wires":[]},{"id":"e5a43097.a3995","type":"join","z":"60ec2a4b.5ad324","name":"join new payload","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":850,"y":360,"wires":[["b7bfe1ac.9ea71"]]},{"id":"dd4df7c0.d02e78","type":"change","z":"60ec2a4b.5ad324","name":"remove payload","rules":[{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":340,"wires":[["e5a43097.a3995"]]},{"id":"4b1cf72.3c23a08","type":"comment","z":"60ec2a4b.5ad324","name":"command to send ","info":"1) command executed immediately\n curl \"http://localhost:1880/cli?cmd=test1&param=hello%20world\"\n\n2) command with delayed answer\n curl \"http://localhost:1880/cli?cmd=test2&param=test\"\n \n 3) Invalid command \n  curl \"http://localhost:1880/cli?cmd=test3&param=867\"\n\n","x":330,"y":420,"wires":[]},{"id":"333e6ce4.35b9c4","type":"inject","z":"60ec2a4b.5ad324","name":"Send the result","props":[{"p":"payload"},{"p":"complete","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"This is the result","payloadType":"str","x":640,"y":380,"wires":[["e5a43097.a3995"]]},{"id":"d1ae2912.4a8b28","type":"debug","z":"60ec2a4b.5ad324","name":"log received request","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":640,"y":300,"wires":[]},{"id":"90147b04.5474d8","type":"template","z":"60ec2a4b.5ad324","name":"Immediate answer ","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"The command is : {{payload.cmd}}\nThe parameter is : {{payload.param}}\n","output":"str","x":630,"y":180,"wires":[["b7bfe1ac.9ea71"]]},{"id":"c224d87c.d2e008","type":"comment","z":"60ec2a4b.5ad324","name":"sendHTTP resp","info":"","x":1040,"y":280,"wires":[]},{"id":"10e65355.bea00d","type":"comment","z":"60ec2a4b.5ad324","name":"Immediate processing and answer","info":"","x":640,"y":140,"wires":[]},{"id":"91c65281.dde4e","type":"comment","z":"60ec2a4b.5ad324","name":"Answer after processing","info":"The request is logged in the debug node.\nUse the Inject node to send the result.","x":640,"y":260,"wires":[]},{"id":"1c77828c.c367fd","type":"comment","z":"60ec2a4b.5ad324","name":"Invalid command","info":"This answer is used when command is not valid.\nHTTP status is OK (200) but could be forced to error code.","x":640,"y":460,"wires":[]}]

In this flow, afirst check is done on the cmd parameters. The "switch" node will send the message to the correct part of the flow :

  • The top part is similar to previous exemple : the answer is prepared and sent immediately to requester. It can be useful some information are already known in the flow and should be returned (note that flow is always running, and data could be memorized in flow "context")
  • The second part is for a command which requires some processing time. In this case, the request is added to the log and the user must click on the "inject"node to send the result. The http request in console is waiting until user click to send the answer. When user clicks, the answer is joined to the http request message, and sent back to the http response node. The "http response" node needs the original "req" part of the request to be able to answer. This is why there is a need to join the original message from "http in" without the payload and the new payload coming from the "inject" node.
  • The third part is for a wrong command. In this case, and error message is sent immediately to unblock the console.

This example can be adapted to trig any actions on the flow. The http request can be sent by console, scripts or any software.


1.5. Example : Toggling Nucleo LED from a command line

Put the LED example here and explain how it is working

No categories assignedEdit