Press button in NR will run a python script on a remote win pc

I would like to make a button in Node Red on my RPI that sends a command to a remote windows pc to run a *.bat file or python script directly. So if I press this button in Node Red it will execute a python script on a remote win pc.
I guess I'll need a "exec" node an some ssh trickery, however I never managed to get it to work.
Does anybody know how to do this?

You will need some kind of communication protocol. for example "processing" + mqtt. If you need to be as simple as possible and there are no security issues, then you can use a telnet server on a computer with windows installed.

There are many ways to do this.

The easiest way is probably to run Node-RED on the PC as well as the Pi. Then you can set up an http input on the PC as an API endpoint. Then you can interact with it from the Pi. You will need to probably change a PC firewall to allow the incoming connection though.

An alternate - that really only helps if the PC is always on the same network as the Pi - would be to get Node-RED on the PC to connect to an MQTT broker on the Pi. You can then simply send messages via the broker.

You could also set up a listening HTTP API using Python as well if you wanted to, another alternative but then you'd have to keep the Python server script running on the PC. Keeping Node-RED running would be more flexible.

All are on the same network. So for the second option. You mean to install only NR on the remote PC ( because Mosquitto is already running on the RPI ) and then on the Remote PC side do something like the node flow below.

1 Like

If you do not have the MQTT broker installed and running, you need to do that as well. What you need to to do on the remote pc should work as you showed

Another alternative, depending on your Python skills, is to add MQTT support to your script and then let the script wait for commands directly from your RPi

I had the same idea in the beginning since the python script also runs Paho. But I'm using the MQTT system to see if the app is still running. If NR on the RPI doesn't receive a MQTT msg from the remote PC within a given time I know the app has crashed. Somehow I didn't like the idea to use that same technique to start to trigger the script for running my main program. I like the idea running NR on the remote PC and use that to start or stop a script on any given time.

I think I said it was another alternative. Installing NR on both computers and use MQTT is a very good choice

You need to communicate between the Pi and the PC, and MQTT is a good choice.

But if you are running Node-Red on the PC, you can view it's dashboard from a browser on the Pi (HTTP) and it can run the script locally when the button is pressed. No need for MQTT in that case.

I'm using the NR dashboard from the RPI to control the remote PC. For example to Power on the remote PC or to reboot. Measure the temps on the GPU's etc.. I understand your point but in that case I need to switch between the dashboard running on the RPI to power the PC on. And to run the python script I would need to switch to the dashboard running on the PC. I would like to use one one dashboard to control all those things. I assume in that case I need to use MQTT.
The only issue I have now is I can't seem to run my script. I would like to accomplish the next. When I press the button on the NR dashboard. It will open the python script the same way as if I would be on the remote PC and double clicked ( run ) the python script locally. That doesn't seem to work. It looks like it's doing something in the task manager but it doesn't work correctly. I assumed I just needed to add the next in the function node:

msg.payload="python E:/PATH/Python Program.py"
return msg;

I see a bunch of cmd.exe, pi.py and python.exe pop up in the task manager. It looks like it wants to sort of starts up the script and it tries this over and over again by starting a new script...

Yes, you would have two browser tabs open. I can see that's not desirable.

I am interested in the idea of turning a PC on by a command from the Pi. How does that work?
I know it's not straightforward to turn a Pi on by a command from a PC.

No idea about running scripts on the PC, sorry. I looked at Microsoft's instructions for enabling Python and decided not to go there.

I've build a ESP8266 inside my PC running on the internal USB power.
The ESP has 3 temp sensors attached that are measuring my GPU's temp. And for the power and reset I have connected optocouplers to the Power and Reset button of the remote pc. In my test setup the optocouplers are represented as normal red leds. The ESP is controlled by MQTT. So I can read temps, start or reset the remote pc ( so no WOL ). And I also have an optocoupler on the PS of the PC. So the ESP can detect if the PC is powered on.


Optocoupler

1 Like

I never succeeded to run a remote python script ( python script running on a Win System ) directly from the exec node in NR ( NR running on a RPI ). Or to run a python script from the exec node on a local NR for that matter ( both python script and NR on the same Win system ).

So I finally ended up using a python script that runs on a start up of the Win system, lets call it "app_a.py". This script looks at a specific topic that reads the START and STOP buttons from NR on the RPI. So whenever the START button is pressed in NR on the RPI the python "app_a.py" script will open a new command window and start a specific script "app_b.py". And when the STOP button is pressed the "app_a.py" script will close down the "app_b.py" script.

I'm still interested to know how to create a new console that runs a specific python script by using the exec node in NR.

What errors are you getting? Remember that if running node-red as a service, it won't be getting the same environment variables as running from a terminal. Typically you need, for example, to use absolute file names in your exec node settings.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.