Page 1 of 1

### php, python, cgi, apache

Posted: Sat Apr 11, 2015 7:16 am
Hello everyone
I followed this tutorial http://www.raspberrypi-spy.co.uk/2013/1 ... n-mcp3008/
I got it working thank god, took me a while but finally came through.
my question now is .I have a server set up on the raspberry pi. How can I make the data that I get from running the python script to be visible on my webserver.
This is the code I am working with

Code: Select all

``````#!/usr/bin/python

import spidev
import time
import os

# 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
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.
def ConvertTemp(data,places):

# (approx)  Temp  Volts
#    0      -50    0.00
#   78      -25    0.25
#  155        0    0.50
#  233       25    0.75
#  310       50    1.00
#  465      100    1.50
#  775      200    2.50
# 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

delay = 5

while True:

# Read the light sensor data
light_volts = ConvertVolts(light_level,2)

# Read the temperature sensor data
temp_volts = ConvertVolts(temp_level,2)
temp       = ConvertTemp(temp_level,2)

# Print out results
print "--------------------------------------------"
print("Light: {} ({}V)".format(light_level,light_volts))
print("Temp : {} ({}V) {} deg C".format(temp_level,temp_volts,temp))

# Wait before repeating loop
time.sleep(delay)``````
Thanks again

### Re: php, python

Posted: Sat Apr 11, 2015 7:26 am
For displaying python data on a webpage, you can use cgi.

Laurens

### Re: php, python

Posted: Tue Apr 21, 2015 12:42 am
I got cgi working on an apache server. But how do I embed the script or what do I need to do??
Thanks!

### Re: php, python

Posted: Wed Apr 22, 2015 10:34 am
Just use the script and use print to display the code.

Laurens

### Re: php, python

Posted: Wed Apr 22, 2015 3:53 pm
You need to save the script in /usr/lib/cgi-bin and make sure it has executable permissions.

Code: Select all

``print "Content-type: text/html\n\n"``
Add HTML formatting to the output:

Code: Select all

``print "<bold>Light:</bold>"``
Here's a crash course in CGI scripting: http://raspberrywebserver.com/cgiscripting/

### Re: php, python, cgi, apache

Posted: Thu Apr 23, 2015 3:20 am
I will give it a try.
Thank you

My next question is how can I make this data go on a google sheet? I want to be able to graph it. and be able to see it on my server.

### Re: php, python, cgi, apache

Posted: Thu Apr 23, 2015 12:30 pm

Alternatively, you could store the data in an SQLite database: http://raspberrywebserver.com/sql-databases/

### Re: php, python, cgi, apache

Posted: Tue Apr 28, 2015 12:28 am
Thank you guys. you are all very helpul
I put the python script mcp3008.py in this folder /usr/lib/cgi-bin . now the file is there but do I go to
http://192.168.2.100/cgi-bin/mcp3008.py ???????
and if so all I get is

Code: Select all

``Light:``
Does the python script mcp3008.py have to be running or somehow cgi calls the script?

### Re: php, python, cgi, apache

Posted: Tue Apr 28, 2015 12:46 am
Apache will call the script, so you don't need to start it in advance. It looks like you're using the correct URL. Make sure the script is executable. Also, double check that you added the AddHandler directive to /etc/apache2/sites-enabled/000-default (see http://raspberrywebserver.com/cgiscript ... ython.html).

### Re: php, python, cgi, apache

Posted: Tue Apr 28, 2015 4:14 pm
hmm
I did all the above, yet still I don't see the data.
All I see is

Light:

### Re: php, python, cgi, apache

Posted: Tue Apr 28, 2015 4:19 pm
Are there any error messages in /var/log/apache2/error.log?

### Re: php, python, cgi, apache

Posted: Thu Apr 30, 2015 2:18 am
WebPi wrote:Are there any error messages in /var/log/apache2/error.log?

This is what I see

Code: Select all

``````[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\$``````
This is my currrent code, so far

Code: Select all

``````#!/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
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):

# (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

delay = 5

while True:

# Read the light sensor data
light_volts = ConvertVolts(light_level,2)
light_volts = ConvertVolts(light_level,2)

# Read the temperature sensor data
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)

``````
Thanks !

### Re: php, python, cgi, apache

Posted: Sat May 02, 2015 4:24 pm
This is a problem with the permission of the SPI interface. I think it's owned by root, but your script is running as Apache's user name, www-data. You need to give www-data access to SPI.

According to this thread, you need to add www-data to the spi group: viewtopic.php?f=28&t=106814

This is the command you need to use:

Code: Select all

``sudo adduser www-data spi``

### Re: php, python, cgi, apache

Posted: Sun May 03, 2015 8:43 pm
WebPi wrote:This is a problem with the permission of the SPI interface. I think it's owned by root, but your script is running as Apache's user name, www-data. You need to give www-data access to SPI.

According to this thread, you need to add www-data to the spi group: viewtopic.php?f=28&t=106814

This is the command you need to use:

Code: Select all

``sudo adduser www-data spi``
awesome.
I am trying this at the moment but the group "spi" does not exist. thats the message I get
Thank you !

### Re: php, python, cgi, apache

Posted: Fri May 08, 2015 5:41 pm
I still cannot figure it out :/
I tried adding the user but nothing.

Code: Select all

``````[email protected] /usr/lib/cgi-bin \$ sudo adduser www-data spi
adduser: The group `spi' does not exist.
[email protected] /usr/lib/cgi-bin \$ sudo addgroup spi
Adding group `spi' (GID 1002) ...
Done.
[email protected] /usr/lib/cgi-bin \$ sudo adduser www-data spi
Adding user `www-data' to group `spi' ...
Adding user www-data to group spi
Done.
``````
any ideas?

### Re: php, python, cgi, apache

Posted: Fri May 08, 2015 6:03 pm
There is no point in adding a spi group as it won't help, if it's relevant the group would already exist.

You need to look at the spi device and see the access permissions.

What is reported by

Code: Select all

``````ls -l /dev/spi*
``````
My Pi2 shows

Code: Select all

``````crw-rw---- 1 root spi 153, 0 May  7 22:57 /dev/spidev0.0
crw-rw---- 1 root spi 153, 1 May  7 22:57 /dev/spidev0.1
``````

### Re: php, python, cgi, apache

Posted: Fri May 08, 2015 6:12 pm
joan wrote:There is no point in adding a spi group as it won't help, if it's relevant the group would already exist.

You need to look at the spi device and see the access permissions.

What is reported by

Code: Select all

``````ls -l /dev/spi*
``````
My Pi2 shows

Code: Select all

``````crw-rw---- 1 root spi 153, 0 May  7 22:57 /dev/spidev0.0
crw-rw---- 1 root spi 153, 1 May  7 22:57 /dev/spidev0.1
``````

Code: Select all

``````crw------- 1 root root 153, 0 Dec 31  1969 /dev/spidev0.0
crw------- 1 root root 153, 1 Dec 31  1969 /dev/spidev0.1``````
date seems weird.

### Re: php, python, cgi, apache

Posted: Fri May 08, 2015 6:19 pm
sudo apt-get update && sudo apt-get dist-upgrade
will get your system updated (to the new Raspbian stuff as at yesterday) and one of the new packages will add the spi group and will add the pi user to the spi group.

### Re: php, python, cgi, apache

Posted: Fri May 08, 2015 6:50 pm
DougieLawson wrote:sudo apt-get update && sudo apt-get dist-upgrade
will get your system updated (to the new Raspbian stuff as at yesterday) and one of the new packages will add the spi group and will add the pi user to the spi group.
...but make sure you delete the spi group you created before doing this.

### Re: php, python, cgi, apache

Posted: Sat May 09, 2015 4:39 pm
Did all the above.
Still having the same problem :/

Code: Select all

``````<type 'exceptions.IOError'>	Python 2.7.3: /usr/bin/python
Sat May 9 11:38:25 2015
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.

/usr/lib/cgi-bin/mcp3008.py in ()
23 # Open SPI bus
24 spi = spidev.SpiDev()
=>   25 spi.open(0,0)
26
27 # Function to read SPI data from MCP3008 chip
spi = <SpiDev object>, spi.open = <built-in method open of SpiDev object>
<type 'exceptions.IOError'>: [Errno 13] Permission denied
args = (13, 'Permission denied')
errno = 13
filename = None
message = ''
strerror = 'Permission denied``````