Hallo,
ich möchte mit PHP eine Pythondatei ausführen und die Konsolenausgabe als Variable speichern und ausgeben.
So sieht die PHP Datei aus:
PHP
<?php
$cmd = escapeshellcmd("python /var/www/html/combined.py");
$out = shell_exec($cmd);
echo $out;
So sieht die Python Datei aus:
Python
#!/usr/bin/env python3
import time
import ST7735
from subprocess import Popen, PIPE
try:
# Transitional fix for breaking change in LTR559
from ltr559 import LTR559
ltr559 = LTR559()
except ImportError:
import ltr559
from bme280 import BME280
from pms5003 import PMS5003, ReadTimeoutError as pmsReadTimeoutError, SerialTimeoutError
from enviroplus import gas
# BME280 temperature/pressure/humidity sensor
bme280 = BME280()
# PMS5003 particulate sensor
pms5003 = PMS5003()
time.sleep(1.0)
# Create a values dict to store the data
variables = ["temperature",
"pressure",
"humidity",
"light",
"oxidised",
"reduced",
"nh3",
"pm1",
"pm25",
"pm10"]
units = ["C",
"hPa",
"%",
"Lux",
"kO",
"kO",
"kO",
"ug/m3",
"ug/m3",
"ug/m3"]
values = {}
# Get the temperature of the CPU for compensation
def get_cpu_temperature():
process = Popen(['vcgencmd', 'measure_temp'],
stdout=PIPE, universal_newlines=True)
output, _error = process.communicate()
return float(output[output.index('=') + 1:output.rindex("'")])
# Tuning factor for compensation. Decrease this number to adjust the
# temperature down, and increase to adjust up
factor = 0.75
cpu_temps = [get_cpu_temperature()] * 5
mode = 10 # The starting mode
# The main loop
if mode == 10:
cpu_temp = get_cpu_temperature()
# Smooth out with some averaging to decrease jitter
cpu_temps = cpu_temps[1:] + [cpu_temp]
avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps))
raw_temp = bme280.get_temperature()
raw_data = raw_temp - ((avg_cpu_temp - raw_temp) / factor)
print("Temp: " + str(raw_data))
raw_data = bme280.get_pressure()
print("Pressure: " + str(raw_data))
raw_data = bme280.get_humidity()
print("Humidity: " + str(raw_data * 10))
gas_data = gas.read_all()
print("Oxidising: " + str(gas_data.oxidising / 1000))
print("Reducing: " + str(gas_data.reducing / 1000))
print("NH3: " + str(gas_data.nh3 / 1000))
lux = ltr559.get_lux()
print("Lux: " + str(lux))
prox = ltr559.get_proximity()
print("Prox: " + str(prox))
pms_data = None
try:
pms_data = pms5003.read()
except (SerialTimeoutError, pmsReadTimeoutError):
print("Failed to read PMS5003")
else:
print("PMS 1.0" + str(float(pms_data.pm_ug_per_m3(1.0))))
print("PMS 2.5" + str(float(pms_data.pm_ug_per_m3(2.5))))
print("PMS 10" + str(float(pms_data.pm_ug_per_m3(10))))
print("=============================")
Alles anzeigen
Und das ist die Fehlermeldung von Apache:
Code
Traceback (most recent call last):
File "/var/www/html/combined.py", line 9, in <module>
ltr559 = LTR559()
File "/usr/local/lib/python2.7/dist-packages/ltr559-0.1.1-py2.7.egg/ltr559/__init__.py", line 212, in __init__
BitField('ALS', 0x0F)
File "/usr/local/lib/python2.7/dist-packages/i2cdevice-0.0.7-py2.7.egg/i2cdevice/__init__.py", line 166, in __init__
self._i2c = smbus.SMBus(1)
IOError: [Errno 13] Permission denied
Die Pythondatei funktioniert einwandfrei und kann sowohl normal (über python combined.py) gestartet werden, als auch über die PHP CLI (php -a).