簡體   English   中英

使用PHP運行Python腳本

[英]Run Python script with PHP

我有這個python文件:

light.py:

#!/usr/bin/python

import sys
import smbus
import time
from Adafruit_I2C import Adafruit_I2C


class Luxmeter:
i2c = None

def __init__(self, address=0x39, debug=0, pause=0.8):
    self.i2c = Adafruit_I2C(address)
    self.address = address
    self.pause = pause
    self.debug = debug
    self.gain = 0 # no gain preselected
    self.i2c.write8(0x80, 0x03)     # enable the device


def setGain(self,gain=1):
    """ Set the gain """
    if (gain != self.gain):
        if (gain==1):
            self.i2c.write8(0x81, 0x02)     # set gain = 1X and timing = 402 mSec
            if (self.debug):
                print "Setting low gain"
        else:
            self.i2c.write8(0x81, 0x12)     # set gain = 16X and timing = 402 mSec
            if (self.debug):
                print "Setting high gain"
        self.gain=gain;                     # safe gain for calculation
        time.sleep(self.pause)              # pause for integration (self.pause must be bigger than integration time)


def readWord(self, reg):
    """Reads a word from the I2C device"""
    try:
        wordval = self.i2c.readU16(reg)
        newval = self.i2c.reverseByteOrder(wordval)
        if (self.debug):
            print("I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, wordval & 0xFFFF, reg))
        return newval
    except IOError:
        print("Error accessing 0x%02X: Check your I2C address" % self.address)
        return -1


def readFull(self, reg=0x8C):
    """Reads visible+IR diode from the I2C device"""
    return self.readWord(reg);

def readIR(self, reg=0x8E):
    """Reads IR only diode from the I2C device"""
    return self.readWord(reg);

def getLux(self, gain = 0):
    """Grabs a lux reading either with autoranging (gain=0) or with a specified gain (1, 16)"""
    if (gain == 1 or gain == 16):
        self.setGain(gain) # low/highGain
        ambient = self.readFull()
        IR = self.readIR()
    elif (gain==0): # auto gain
        self.setGain(16) # first try highGain
        ambient = self.readFull()
        if (ambient < 65535):
            IR = self.readIR()
        if (ambient >= 65535 or IR >= 65535): # value(s) exeed(s) datarange
            self.setGain(1) # set lowGain
            ambient = self.readFull()
            IR = self.readIR()

    if (self.gain==1):
       ambient *= 16    # scale 1x to 16x
       IR *= 16         # scale 1x to 16x

    if (float(ambient) != 0):
        ratio = (IR / float(ambient)) # changed to make it run under python 2
    else: ratio = 0

    if (self.debug):
        print "IR Result", IR
        print "Ambient Result", ambient

    if ((ratio >= 0) & (ratio <= 0.52)):
        lux = (0.0315 * ambient) - (0.0593 * ambient * (ratio**1.4))
    elif (ratio <= 0.65):
        lux = (0.0229 * ambient) - (0.0291 * IR)
    elif (ratio <= 0.80):
        lux = (0.0157 * ambient) - (0.018 * IR)
    elif (ratio <= 1.3):
        lux = (0.00338 * ambient) - (0.0026 * IR)
    elif (ratio > 1.3):
        lux = 0

    return lux


oLuxmeter=Luxmeter()

i=0
while True:
    light = oLuxmeter.getLux(1)
    if (light != 0):
        print light
        break
    else:
        i+=1
        if (i == 10):
            print light
            break

現在我想用Raspberry Pi在PHP中運行它

echo system("/var/www/light.py")

但是網站的回應卻沒有。 我使用chmod + x授予所有文件權限,但是它沒有任何改變。 如果我輸入

python /var/www/light.py

進入控制台即可。

問題是您正在以沒有特權使用您正在使用的smbus功能的某些用戶身份運行Web服務器。

您可以通過運行su www-data /usr/bin/python /var/www/light.py (當然,詳細信息會根據您的設置而有所不同)。 如果失敗,則說明這是您的問題。 (此外,您將看到追溯,這可能會有所幫助。)

以盡可能少的特權以用戶身份運行Web服務器是一個好主意-但以比可能少的特權運行它顯然不是。 :)

* nix系統上的大多數特權是由用戶/組文件權限控制的,因此答案可能是將Web服務器用戶添加到擁有smbus的組中。 正如您在論壇帖子中暗示的那樣, PHP exec和python-smbus ,該組通常名為i2c ,因此,如果您的Web服務器用戶名為www-data ,則可以運行:

sudo adduser www-data i2c

附帶說明一下,將來不要忽略調用另一個程序的返回值。 如果返回1或除0以外的任何值,則表明程序已失敗,這就是為什么您未獲得任何有用的輸出的原因。 (超出0或不等於0時,盡管2常常意味着錯誤的論點,但實際值並未標准化。)


同時,您還有第二個問題。

system文檔所述, system不會返回已執行命令的輸出。

文檔暗示您想查看passthru ,但這也不是您想要的。 就像system一樣, passthru將輸出轉儲到stdout而不是將其返回; 唯一的區別是,它轉儲了未經換行翻譯的過濾器。

如果要檢索輸出然后回顯它,則可能需要在exec加上output參數:

如果存在output參數,則命令的輸出的每一行都將填充指定的數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM