0

I want to be able to send commands to my air purifier via Flask, so I can use its functions with my home automation in webcore.

Here is the python snippet

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

@app.route('/status')

def my_command():
        cmd = 'airctrl --ipaddr 192.168.99.55 --protocol coap --mode S'
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
        out,err = p.communicate()
        return out

if __name__ == "__main__" :
    app.run(host="192.168.99.45", port=5000, debug=True)

What will always return

FileNotFoundError: [Errno 2] No such file or directory: 'airctrl --ipaddr 192.168.99.55 --protocol coap --mode S': 'airctrl --ipaddr 192.168.99.55 --protocol coap --mode S'

How can I tell flask, wehre to find "airctrl"?

I have tried

 cmd = '/usr/local/bin/airctrl', 'airctrl --ipaddr 192.168.99.55 --protocol coap --mode S'

which will return nothing but also do nothing.

Reallay appreciate your help, as you can see I am a total beginner and have googled for solutions for hours now.

Thank you,

Thomas

davidism
  • 121,510
  • 29
  • 395
  • 339
oglo
  • 19
  • 2

2 Answers2

1

As mentioned in a comment above one solution is to specify the absolute path. Adapted to your example it would be this:

cmd = '/usr/local/bin/airctrl --ipaddr 192.168.99.55 --protocol coap --mode S'

Also read the Python documentation regarding Popen and e.g. this SO question, which presents an alternative solution using shell=True.

flyingdutchman
  • 1,197
  • 11
  • 17
0

Adding shell=True did the trick for me. It seems I didn't even know what exactly to look up. So thanks for pointing me at the right answer. Here is how the endpoints look now.

@app.route('/sleep')

def sleepmode():
        cmd = ["airctrl --ipaddr 192.168.99.55 --protocol coap --mode S"]
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
        out,err = p.communicate()
        return out

Adding just the full path, I ran into a authentication error

oglo
  • 19
  • 2