Hi I am trying to run this python code in my /usr/lib/cgi-bin/mcp3008.py I am working with a raspberry PI and an Apache server. I am pretty new to this, Thank you
#!/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)
The problem is that I get an error. This is what my var/log/apache2/error.log looks like
[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$
To allow your web programs running on the Apache web server to talk to the SPI devices, you'll need to give the user account that runs the Apache web server permissions for the SPI devices. Enter this command:
sudo usermod -a -G spi www-data
You need to reboot the PI before changes take effect
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.