使用 PHP 和 SSH 通过 Web 使用 Python 的 Pi RGB 控制器。这是个好主意?
Pi RGB controler with Python over the web using PHP and SSH. It is good idea?
我制作了可以控制我的 BLE RGB 灯带的 Python 脚本,我还制作了带有 php 的网页,通过 ssh 和 运行 Python 脚本连接到 Pi .我的问题是,有没有更好的方法?
我会在几天内标记我最喜欢的答案。我希望有人会看到这个并得到像我一样的灵感。
感谢所有评论。
我的 PHP 代码。
?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);
我会用 Python
/Flask
来 运行 这个网页。我会 运行 它直接在 Pi 上,所以我不需要使用 login
/password
这是(或多或少)PHP
直接在 PI 上(没有 SSH)转换为 Flask
到 运行
我使用端口 8080
因为标准端口 80
需要 root 权限。
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)
最终我会为每个命令创建单独的 URL
http://ip_of_raspberry:8080/turn/on
http://ip_of_raspberry:8080/turn/off
http://ip_of_raspberry:8080/color/red
后来我会使用Python构建桌面GUI(tkinter
、PyQt
、PyGTK
、wxPython
)或移动应用程序(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)
我使用 subprocess
到 运行 你的 on.py
、off.py
、color.py
但我宁愿使用 import on
、import off
、import color
并从导入的模块执行函数,但它需要将代码放入 on.py
、off.py
、color.py
中(你可能没有)
顺便说一句:这样你就可以构建类似于 Home Assistant which also use Python code
的东西
我制作了可以控制我的 BLE RGB 灯带的 Python 脚本,我还制作了带有 php 的网页,通过 ssh 和 运行 Python 脚本连接到 Pi .我的问题是,有没有更好的方法?
我会在几天内标记我最喜欢的答案。我希望有人会看到这个并得到像我一样的灵感。
感谢所有评论。
我的 PHP 代码。
?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);
我会用 Python
/Flask
来 运行 这个网页。我会 运行 它直接在 Pi 上,所以我不需要使用 login
/password
这是(或多或少)PHP
直接在 PI 上(没有 SSH)转换为 Flask
到 运行
我使用端口 8080
因为标准端口 80
需要 root 权限。
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)
最终我会为每个命令创建单独的 URL
http://ip_of_raspberry:8080/turn/on
http://ip_of_raspberry:8080/turn/off
http://ip_of_raspberry:8080/color/red
后来我会使用Python构建桌面GUI(tkinter
、PyQt
、PyGTK
、wxPython
)或移动应用程序(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)
我使用 subprocess
到 运行 你的 on.py
、off.py
、color.py
但我宁愿使用 import on
、import off
、import color
并从导入的模块执行函数,但它需要将代码放入 on.py
、off.py
、color.py
中(你可能没有)
顺便说一句:这样你就可以构建类似于 Home Assistant which also use Python code
的东西