Iam made Python script that can control my BLE RGB strip, also I made web page with php that connect to Pi via ssh and run Python script. My question is, is there a better way ?
I will mark my favorite answer in few days. I hope someone will see this and get same inspiration like me.
Thanks for all comments.
My PHP code.
?php $mode = htmlspecialchars($_GET["mode"]); $color = htmlspecialchars($_GET["color"]); function color_name_to_hex($color_name) { // standard 147 HTML color names $colors = array( 'aliceblue'=>'F0F8FF', 'antiquewhite'=>'FAEBD7' ...; $color_name = strtolower($color_name); if (isset($colors[$color_name])) { return ('#' . $colors[$color_name]); } else { return ($color_name); } } $connection = ssh2_connect('*ip*', 22); ssh2_auth_password($connection, '*login*', '*passsword*'); switch ($mode) { case 'on': $stream = ssh2_exec($connection, 'python on.py'); break; case 'off': $stream = ssh2_exec($connection, 'python off.py'); break; case 'color': $stream = ssh2_exec($connection, 'python color.py ''.color_name_to_hex($color).'''); break; default: ssh2_disconnect ($connection); break; } ssh2_disconnect ($connection);
Advertisement
Answer
I would use Python
/Flask
to run this web page. And I would run it directly on Pi so I wouldn’t need to use login
/password
This is (more or less) PHP
converted to Flask
to run directly on PI (without SSH)
I used port 8080
because standard port 80
would need root privileges.
from flask import Flask, request import subprocess app = Flask(__name__) def color_name_to_hex(color_name): # standard 147 HTML color names colors = { 'aliceblue': '#F0F8FF', 'antiquewhite': '#FAEBD7', # ... } color_name = color_name.lower() # get `colors[color_name]` (first `color_name` in get()) or use `color_name` (second `color_name` in get()) color_hex = colors.get(color_name, color_name) return color_hex @app.route('/') def index(): mode = request.args.get('mode') color = request.args.get('color') if mode == 'on': subprocess.run('python on.py') elif mode == 'off': subprocess.run('python off.py') elif mode == 'color': color_hex = color_name_to_hex(color) subprocess.run(f"python color.py '{color_hex}'") return 'OK' if __name__ == '__main__': app.run('0.0.0.0', port=8080, debug=True)
Eventually I would create separated URLs for every command
http://ip_of_raspberry:8080/turn/on http://ip_of_raspberry:8080/turn/off http://ip_of_raspberry:8080/color/red
And later I would use Python to build desktop GUI (tkinter
, PyQt
, PyGTK
, wxPython
) or mobile App (Kivy).
from flask import Flask, request import subprocess app = Flask(__name__) def color_name_to_hex(color_name): # standard 147 HTML color names colors = { 'aliceblue': '#F0F8FF', 'antiquewhite': '#FAEBD7', # ... } color_name = color_name.lower() # get `colors[color_name]` (first `color_name` in get()) or use `color_name` (second `color_name` in get()) color_hex = colors.get(color_name, color_name) return color_hex @app.route('/') def index(): return 'HELLO WORLD' @app.route('/turn/on') def turn_on(): subprocess.run('python on.py') return 'OK turn on' @app.route('/turn/off') def turn_off(): subprocess.run('python off.py') return 'OK turn off' @app.route('/color/<color_name>') def color(color_name): color_hex = color_name_to_hex(color_name) subprocess.run(f"python color.py '{color_hex}'") return f'OK color {color_name}' if __name__ == '__main__': app.run('0.0.0.0', port=8080, debug=True)
I use subprocess
to run your on.py
, off.py
, color.py
but I would rather use import on
, import off
, import color
and execute function from imported modules but it would need to put code in function in on.py
, off.py
, color.py
(which probably you didn’t)
BTW: this way you would build something similar to Home Assistant which also use Python code