您好我正在尝试在我的/usr/lib/cgi-bin/mcp3008.py中运行此python代码 我正在使用raspberry PI和Apache服务器。我很陌生, 谢谢
#!/usr/bin/env python
#--------------------------------------
# This script reads data from a
# MCP3008 ADC device using the SPI bus.
#
# Author : Matt Hawkins
# Date : 13/10/2013
#
# http://www.raspberrypi-spy.co.uk/
#
#--------------------------------------
import spidev
import time
import os
print "Content-type: text/html\n\n"
print "<bold>Light:</bold>"
# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)
# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data
# Function to convert data to voltage level,
# rounded to specified number of decimal places.
def ConvertVolts(data,places):
volts = (data * 3.3) / float(1023)
volts = round(volts,places)
return volts
# Function to calculate temperature from
# TMP36 data, rounded to specified
# number of decimal places.
# TMP36 data, rounded to specified
# number of decimal places.
def ConvertTemp(data,places):
# ADC Value
# (approx) Temp Volts
# 0 -50 0.00
# 78 -25 0.25
# 155 0 0.50
# 233 25 0.75
# 310 50 1.00
# 388 75 1.25
# 465 100 1.50
# 543 125 1.75
# 620 150 2.00
# 698 175 2.25
# 775 200 2.50
# 853 225 2.75
# 930 250 3.00
# 1008 275 3.25
# 1023 280 3.30
temp = ((data * 330)/float(1023))-50
temp = round(temp,places)
return temp
# Define sensor channels
light_channel = 0
temp_channel = 1
# Define delay between readings
delay = 5
while True:
# Read the light sensor data
light_level = ReadChannel(light_channel)
light_volts = ConvertVolts(light_level,2)
light_volts = ConvertVolts(light_level,2)
# Read the temperature sensor data
temp_level = ReadChannel(temp_channel)
temp_volts = ConvertVolts(temp_level,2)
temp = ConvertTemp(temp_level,2)
# Print out results
print "------The 'Ponics Project--------------------------------------"
print("Light : {} ({}V)".format(light_level,light_volts))
print("Temperature in the room is: {} ({}V) {} deg F".format(temp_level,temp_volts,(temp * 9/5)+ 32))
# Wait before repeating loop
time.sleep(delay)
问题是我收到了错误。 这就是我的var / log / apache2 / error.log看起来像
[Wed Apr 29 21:15:37 2015] [error] [client 192.168.2.1] Traceback (most recent call last):
[Wed Apr 29 21:15:37 2015] [error] [client 192.168.2.1] File "/usr/lib/cgi-bin/mcp3008.py", line 21, in <module>
[Wed Apr 29 21:15:37 2015] [error] [client 192.168.2.1]
[Wed Apr 29 21:15:37 2015] [error] [client 192.168.2.1] spi.open(0,0)
[Wed Apr 29 21:15:37 2015] [error] [client 192.168.2.1] IOError: [Errno 13] Permission denied
[Wed Apr 29 21:15:38 2015] [error] [client 192.168.2.1] File does not exist: /var/www/favicon.ico, referer: http://192.168.2$
答案 0 :(得分:0)
要允许在Apache Web服务器上运行的Web程序与SPI设备通信,您需要为运行Apache Web服务器的用户帐户提供SPI设备的权限。输入以下命令:
sudo usermod -a -G spi www-data
您需要在更改生效之前重新启动PI