python - 使用 PHP 和 SSH 在 Web 上使用 Python 的 Pi RGB 控制器。这是个好主意?
问题描述
我制作了可以控制我的 BLE RGB 条的 Python 脚本,我还用 php 制作了网页,通过 ssh 连接到 Pi 并运行 Python 脚本。我的问题是,有没有更好的方法?
几天后我会标记我最喜欢的答案。我希望有人会看到这一点并得到和我一样的灵感。
感谢所有评论。
我的 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
转换为Flask
直接在 PI 上运行(没有 SSH)
我使用了端口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的东西,它也使用 Python代码
推荐阅读
- javascript - Tensorflow:替代 DenseFeatures 输入层以移植到 tfjs
- r - 如何使 combn 在 dplyr::mutate 中工作?
- php - php环境中Curl没有响应
- c# - 我的验证器不符合要求,没有返回正确的值
- excel - 如何仅将宏应用于选定行
- ios - Swift 使用estimatedItemSize 单元格在滚动期间打破单元格宽度
- java - javax.el.PropertyNotFoundException:在类型 [java.util.Optional] 上找不到属性 [operatingAirlines] - 已在 SO 上给出的答案不起作用
- php - 如何根据函数php的值自动生成number_format?
- javascript - 停止在移动设备上运行的 jQuery 图像缩放
- angular - Angular 7:更新全局变量的问题