Executing a scrip on my Raspberry Pi


#1

I have a robot vacuum that doesn’t have ST or IFTTT integration. It does, however, have a program run through node.js that I can execute commands from my Raspberry Pi on to send commands to the vaccum. I just execute the program with a few command line arguments and off it goes. How would I accomplish doing this with WebCoRE? My first thought was to have WebCoRE do an rsh, but that would involve me opening up ports and doing some forwarding and I’m not really sure I want to do it. Then I started thinking, that I probably don’t need to handle anything from the outside, ST and WebCoRE should be able to handle it all internally using my LAN’s ip addresses.

So what would be the best way to go about doing this?


#2

@Core_PHX helped someone with a similar question a while back. Not sure where the thread is, ST or in this forum. Maybe he will remember LOL.


#3

Here’s a relatively simple way to execute a command on your RP using webcore.
It involves python and Flask (http://flask.pocoo.org/)

For this example, it’ll be a simple GET request to start my printer daemon (cupsd), you can adapt it for pretty much anything

Install flask:
python -m pip install flask

Contents of “cups.py” file:

from flask import Flask
import subprocess
app = Flask(__name__)

@app.route('/')

def my_command():
    cmd = ["/etc/init.d/cups", "start"]
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
    out,err = p.communicate()
    return out

if __name__ == "__main__" :
    app.run()

Set env variable and start flask:

FLASK_APP=cups.py
flask run --host=<your IP address>

It’ll default to listening on port 5000

It webcore, just create a simple GET request:

Now when the piston gets executed, it makes a GET request to FLASK, which then executes your command.
You could easily define other URL endpoints to execute different commands as well.

Another thing to note is that command line args need to be separated out in the python list. (note how “/etc/init.d/cups” and “start” are separated)

Is this what you’re looking for?


Schedule a command from Webcore to a raspberry pi after x hours of inactivity
#4

I do believe so… I’ll try it out this evening and report back if I have problems. Thank you very much!


#5

How do I do that? Just start another instance of flask on a different port?


#6

Nope, just simply add other endpoints to the file… For example:

from flask import Flask
import subprocess
app = Flask(__name__)

@app.route('/')
def my_command():
    cmd = ["/etc/init.d/cups", "start"]
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
    out,err = p.communicate()
    return out

@app.route('/uptime')
def my_uptime():
    cmd = ["uptime"]
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
    out,err = p.communicate()
    return out

if __name__ == "__main__" :
    app.run()

Notice the “uptime” endpoint. Now when I access http://<IP_address>:5000/uptime, it executes the “uptime” command.


#8

Ok, this is great thank you. I’ve got it all working. A couple things to note if anyone stumbles on this post in the future. There must be a newer version of Flask out there now as “flask start” does not work, its now “flask run”

Also the command line args SHOULD NOT be separated out, maybe this is a Python2 v Python3 thing? Dunno, I’m not very familiar with Python, but a little Googleing went a long way.

Nonetheless, I’ve got this working as I’d like. Just need to test it a little more. Thanks again.


#9

That was a typo on my part. I’ll correct the original post.
According to the python docs for the subprocess module, ‘cmd’ is a list of arguments (unless passing “shell=True” to subprocess.popen.

anyhow, glad it’s working for you! This is actually something I’ve been wanting to figure out for quite awhile, and seeing your post reminded me to find a solution :slight_smile:

Also, if you get a chance, please mark my original post in this thread as “solution”, thanks!

one more edit to this post… If you want to automate the process of having flask start on boot and “daemonize” it, check out the “pm2” utility. http://pm2.keymetrics.io/docs/usage/quick-start/


#10

Not sure if anyone else is picking up on this thread but here goes…after lots of trial and error I’ve now got a python script triggering within Flask so when I got to the IP:port on a browser my script starts (in my case it triggers a motor to turn).

Problem is, the GET request isn’t working in WebCore to do the same and I would like to automate the process using it. I get the following error code whenever I run the piston;

Error executing external web request: java.lang.reflect.UndeclaredThrowableException

Any ideas of why this is happening? My WebCore script couldn’t be simpler;

Thanks in advance.


#11

Maybe it’s related to this?


#12

Can you post a screenshot of the actual GET request in the piston? Maybe I can help.


#13

I am looking to do something similar but I would like to run a node.js file that in turn changes my eufy security mode. Using this eufy node (https://github.com/JanLoebel/eufy-node-client-examples), I am able to send a node message to trigger the action in the command line. However, I want to trigger the node.js file from webcore to integrate setting eufy security with the rest of my home security.
Is this possible?