【发布时间】:2015-08-26 20:58:54
【问题描述】:
我有一个 DHT22 温度和湿度传感器连接到我的 Raspberry Pi。通过 CLI 运行我的 Python 脚本时,该脚本按预期工作并显示温度和湿度读数。我遇到的问题是我无法通过 PHP 检索传感器输出。
作为测试,我在 Python 脚本中包含了一个打印“Hello World”的命令。当我加载我的 PHP 页面时,显示“Hello World”,但不显示传感器的输出。
根据我在解决此问题时阅读的一些讨论,我的 Python 脚本的所有者和组是“www-data”,并且该用户已添加到我的网络服务器上的 sudoers 文件中。
感谢任何指导。我在这方面已经有很长一段时间没有任何突破,我怀疑我对 Python 的不熟悉可能是我问题的根源。
read_dht.py
#!/usr/bin/env python
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
print 'Hello World...'
import sys
import Adafruit_DHT
sensor = Adafruit_DHT.DHT22
pin = 4
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
print '{0:0.1f}|{1:0.1f}'.format(temperature, humidity)
else:
print 'Failed to get a reading. Please try again!'
get_readings.php
<?php
$command = escapeshellcmd('/var/www/cgi-bin/read_dht.py');
$output = shell_exec($command);
echo $output;
?>
【问题讨论】:
-
如果您想在 Internet 或公共网络上公开您的服务,通常将 www-data 添加到 sudoers 是一个(非常)糟糕的主意。拥有 www-data 用户的想法是避免攻击者在 php 漏洞的情况下获得 root 权限。如果给sudoers加上www-data,就没有这个权限分离了! (即,就好像您在 root 中运行 apache/php 一样。)
标签: php python raspberry-pi sensors gpio