## php, python, cgi, apache

frafrichile
Posts: 42
Joined: Wed Mar 05, 2014 3:11 pm

### php, python, cgi, apache

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

# Define delay between readings
delay = 5

while True:

# Read the light sensor data
light_level = ReadChannel(light_channel)
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 "--------------------------------------------"
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
Last edited by frafrichile on Thu Apr 23, 2015 2:53 am, edited 1 time in total.

Laurens-wuyts
Posts: 716
Joined: Wed Aug 21, 2013 7:35 pm
Location: Belgium
Contact: Website

### Re: php, python

For displaying python data on a webpage, you can use cgi.

Laurens

frafrichile
Posts: 42
Joined: Wed Mar 05, 2014 3:11 pm

### Re: php, python

I got cgi working on an apache server. But how do I embed the script or what do I need to do??
Thanks!

Laurens-wuyts
Posts: 716
Joined: Wed Aug 21, 2013 7:35 pm
Location: Belgium
Contact: Website

### Re: php, python

Just use the script and use print to display the code.

Laurens

WebPi
Posts: 262
Joined: Wed Apr 10, 2013 6:47 pm
Location: Birmingham, UK
Contact: Website

### Re: php, python

You need to save the script in /usr/lib/cgi-bin and make sure it has executable permissions.

Next, add some code near the start of your script to print HTTP headers:

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/
raspberrywebserver.com - Raspberry Pi tutorials
LinuxWebServers.net - Linux Web Server tutorials and examples
pyplate.com - Python web publishing tool

frafrichile
Posts: 42
Joined: Wed Mar 05, 2014 3:11 pm

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

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.

WebPi
Posts: 262
Joined: Wed Apr 10, 2013 6:47 pm
Location: Birmingham, UK
Contact: Website

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

You could try following this tutorial about Google Docs: https://learn.adafruit.com/dht-humidity ... cs-updated

Alternatively, you could store the data in an SQLite database: http://raspberrywebserver.com/sql-databases/
raspberrywebserver.com - Raspberry Pi tutorials
LinuxWebServers.net - Linux Web Server tutorials and examples
pyplate.com - Python web publishing tool

frafrichile
Posts: 42
Joined: Wed Mar 05, 2014 3:11 pm

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

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?
Last edited by frafrichile on Tue Apr 28, 2015 4:17 pm, edited 1 time in total.

WebPi
Posts: 262
Joined: Wed Apr 10, 2013 6:47 pm
Location: Birmingham, UK
Contact: Website

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

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).
raspberrywebserver.com - Raspberry Pi tutorials
LinuxWebServers.net - Linux Web Server tutorials and examples
pyplate.com - Python web publishing tool

frafrichile
Posts: 42
Joined: Wed Mar 05, 2014 3:11 pm

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

hmm
I did all the above, yet still I don't see the data.
All I see is

Light:

WebPi
Posts: 262
Joined: Wed Apr 10, 2013 6:47 pm
Location: Birmingham, UK
Contact: Website

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

Are there any error messages in /var/log/apache2/error.log?
raspberrywebserver.com - Raspberry Pi tutorials
LinuxWebServers.net - Linux Web Server tutorials and examples
pyplate.com - Python web publishing tool

frafrichile
Posts: 42
Joined: Wed Mar 05, 2014 3:11 pm

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

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
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)

``````
Thanks !

WebPi
Posts: 262
Joined: Wed Apr 10, 2013 6:47 pm
Location: Birmingham, UK
Contact: Website

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

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``
raspberrywebserver.com - Raspberry Pi tutorials
LinuxWebServers.net - Linux Web Server tutorials and examples
pyplate.com - Python web publishing tool

frafrichile
Posts: 42
Joined: Wed Mar 05, 2014 3:11 pm

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

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 !

frafrichile
Posts: 42
Joined: Wed Mar 05, 2014 3:11 pm

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

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?

joan
Posts: 14074
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

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

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
``````

frafrichile
Posts: 42
Joined: Wed Mar 05, 2014 3:11 pm

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

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
``````
this is what mine reads

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.

DougieLawson
Posts: 35520
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

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

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.
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

joan
Posts: 14074
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

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

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.

frafrichile
Posts: 42
Joined: Wed Mar 05, 2014 3:11 pm

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

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``````

Return to “Automation, sensing and robotics”