janpihlgren
Posts: 61
Joined: Thu Jul 26, 2012 4:00 am

[Solved] I get Unable to open SPI device: Permission denied

Wed Apr 08, 2015 7:49 am

I'm trying to run a C-program via html.
I'm using Raspberry Pi B and PiFace Difgital I/O (1).
I use a html.index, piface-blink.php and the C-program piface-blink.c
When I run the c-progam it access OK and blinc the LED at PiFace.
When I try to run i through a web I get this error message in /var/log/apache2/error.log:
Unable to open SPI device: Permission denied

This is what I have done so far:
[email protected] ~ $ uname -a
Linux Cisero 3.18.7+ #755 PREEMPT Thu Feb 12 17:14:31 GMT 2015 armv6l GNU/Linux

Access through web, http://cisero_wifi/check/

I have opened SPI by raspi-config.

[email protected] ~ $ tail /var/log/apache2/error.log
Unable to open SPI device: Permission denied
[Wed Apr 08 07:48:12 2015] [notice] caught SIGTERM, shutting down
[Wed Apr 08 07:48:14 2015] [notice] Apache/2.2.22 (Debian) PHP/5.4.39-0+deb7u2 configured -- resuming normal operations
[Wed Apr 08 07:50:34 2015] [notice] caught SIGTERM, shutting down
[Wed Apr 08 07:50:35 2015] [notice] Apache/2.2.22 (Debian) PHP/5.4.39-0+deb7u2 configured -- resuming normal operations
Unable to open SPI device: Permission denied
[Wed Apr 08 08:02:25 2015] [notice] caught SIGTERM, shutting down
[Wed Apr 08 08:02:45 2015] [notice] Apache/2.2.22 (Debian) PHP/5.4.39-0+deb7u2 configured -- resuming normal operations
Unable to open SPI device: Permission denied
Unable to open SPI device: Permission denied

[email protected] /var/www/check $ ls -l
totalt 24
-rwxr-xr-x 1 pi pi 903 apr 8 06:23 index.html
-rwxr-xr-x 1 pi pi 7079 apr 8 06:28 piface-blink-2
-rw-r--r-- 1 pi pi 1452 apr 8 06:28 piface-blink-2.c
-rwxrwxrwx 1 pi pi 1340 apr 8 07:28 piface-blink.php

[email protected] /var/www/check $ ./piface-blink-2 1
argc=2
buf=1

LED nbr 7 blinking

[email protected] /etc $ sudo more sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root ALL=(ALL:ALL) ALL

# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
pi ALL=(ALL) NOPASSWD: ALL
www-data ALL=(ALL) NOPASSWD: ALL

[email protected] /etc/sudoers.d $ ls -l
totalt 8
-r--r----- 1 root root 958 mar 3 2013 README
-r--r----- 1 root root 155 apr 8 07:21 www-data
[email protected] /etc/sudoers.d $ sudo more www-data
#/etc/sudoers.d/www-data
#
# sudoers file for the www-data user - very limited permissions only

www-data ALL=NOPASSWD: /var/www/piface/piface-blink
here are the programs:
index.html

Code: Select all

<!DOCTYPE html>
<html lang="sv">
	<head>
		<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8">
		<title>Kör Piface-blink</title>
		<meta name="generator" content="Bluefish 2.2.4" >
		<meta name="CREATED" content="20140611;5273800">
		<meta name="CHANGED" content="2015-04-08">
		<meta name="DESCRIPTION" content="Testsida för att prova ,testa funktioner">
		<meta name="KEYWORDS" content="Testsida">
	</head>
	<body>

		<h1>Test av köra piface-blink</h1>
		<p>Programnamn = piface-blink.php.</p>
		<form method="POST" action="piface-blink.php"> 
		<form action="piface" metod="POST">
			<input type="checkbox" name="val" value="7">Blinka 7 LEDS
			<br />
			<input type="checkbox" name="val" value="1">Blinka 1 LED
			<br />
			<input type="submit" value="SKICKA">
		</form>
		<form metod="POST" action="index.html">
			<input type="submit" value="RESET">
		</form>
	</body>
</html> 
piface-blink.php

Code: Select all

<?php
/*
 * piface-blink.php
 * 
 * Copyright 2015  Jan Pihlgren
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * 
 */
 if($_SERVER['REQUEST_METHOD'] == "POST") {
	 echo "Testmeddelande";
	 echo "<br />";
	 echo $_SERVER['REQUEST_METHOD'];
	 echo "<br />";
	 echo $_POST['val'];
	 echo "<br />";
	 $val = $_POST['val'];
	 echo "val = ", $val;
	 echo "<br />";
 }
 $command="./piface-blink-2 {$val} &";
 echo "command = ", $command;
 echo "<br />";
exec($command);
exec('exit');
echo "<br>";
echo "Programmet klart!\n";
echo "<br>";
?>
<html>
<body>
<form method="POST" action="index.html">
<input type="submit" value="TILLBAKA"> 
</form>
</body>
</html>
piface-blink-2.c

Code: Select all

/* programnamn: piface-blink-2.c 
* Jan Pihlgren 2015-04-02
* Kompilera med:
* gcc -L/usr/local/lib -lwiringPi -lwiringPiDev -o piface-blink-2 piface-blink-2.c
*/
#include <stdio.h>
#include <string.h>

#include <wiringPi.h>
#include <piFace.h>

// Use 200 as the pin-base for the PiFace board, and pick a pin
//      for the LED that's not connected to a relay

#define PIFACE  200
#define LED     (PIFACE+2)

int main(int argc, char *argv[])
{
// Always initialise wiringPi. Use wiringPiSys() if you don't need
//      (or want) to run as root
	wiringPiSetupSys () ;

// Setup the PiFace board
	piFaceSetup (PIFACE) ;
  
	char buf[10]="";
	int val=0;
	int i;
  
	if(argc > 1) {
		strcpy(buf,argv[1]);	// Kopiera argv[] till buf
	}	
    printf("argc=%d\n",argc);
    printf("buf=%s\n",buf);  
	val=atoi(buf);				// Omvandla char to int
	switch(val){
		case 0:
			break;
		case 1:
			blink1();
			break;
		case 2:
			blink7();
			break;
		case 7:
			blink7();
			break;		
		default:
			return 0;
	}
     return 0 ;
}

blink1()
{
	int i;
//		printf("buf=%s\n",buf);
		for(i=0;i<7;i++){
			digitalWrite(LED+5, 1);
			delay(100);
			digitalWrite(LED+5, 0);
			delay(100);
		}
	
}

blink7()
{
	int i;
//	 printf("Blinka\n");
     for (i=0;i<=7;i++){
         digitalWrite(i+LED, 1);
         delay(100);
         digitalWrite(i+LED, 0);
         digitalWrite(i+1+LED, 1);
         delay(100);
         digitalWrite(i+1+LED, 0);
         }
     return;	
}
My question:
What else do I have to do or what does I had done wrong?
Edit:
For some reason I don't understand the program begun running with success. :)
Last edited by janpihlgren on Wed Apr 08, 2015 10:37 am, edited 2 times in total.

User avatar
joan
Posts: 14478
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I get Unable to open SPI device: Permission denied

Wed Apr 08, 2015 8:30 am

Who has permission to use SPI on your Pi?

Code: Select all

ls -l /dev/spi*
crw-rw---- 1 root spi 153, 0 Apr  7 22:07 /dev/spidev0.0
crw-rw---- 1 root spi 153, 1 Apr  7 22:07 /dev/spidev0.1
Add the user you are using (www-data?) to the spi group.

sudo adduser www-data spi

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

Re: I get Unable to open SPI device: Permission denied

Wed Apr 08, 2015 8:37 am

DO NOT add www-data to the sudoers file if you ever intend to open your machine to the public internet.

sudo adduser www-data spi
allows the webserver to access the SPI bus.
sudo adduser www-data i2c
does the same for the i2c bus.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

akaodin
Posts: 78
Joined: Wed Mar 18, 2015 3:13 pm

Re: [Solved] I get Unable to open SPI device: Permission denied

Wed Apr 11, 2018 5:52 pm

I ran

adduser www-data spi
( which acknowledged that it indeed added the www-data user )

then

ls -l /dev/spi*

and got:
crw-rw---- 1 root spi 153, 1 Apr 9 17:29 /dev/spidev0.1
crw-rw---- 1 root spi 153, 0 Apr 9 17:29 /dev/spidev0.0

what happened? shouldn't www-data also be there?

User avatar
joan
Posts: 14478
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: [Solved] I get Unable to open SPI device: Permission denied

Wed Apr 11, 2018 5:55 pm

No. The spi group has rw access. You have made www-data a member of the spi group.

akaodin
Posts: 78
Joined: Wed Mar 18, 2015 3:13 pm

Re: [Solved] I get Unable to open SPI device: Permission denied

Wed Apr 11, 2018 6:05 pm

I needed to reboot to make it work. Thanks.

Jutzi
Posts: 2
Joined: Wed Jul 01, 2015 4:39 pm

Re: [Solved] I get Unable to open SPI device: Permission denied

Fri Jan 18, 2019 8:41 am

I have the same problem. The following commands solved it, as described above:

sudo chgrp spi /sys/class/gpio/export
sudo chmod 660 /sys/class/gpio/export
sudo chgrp spi /sys/class/gpio/unexport
sudo chmod 660 /sys/class/gpio/unexport
sudo chgrp spi /dev/spidev0.0
sudo chmod 660 /dev/spidev0.0

However, the settings disappear after reboot. Shouldn't they be permanent?

I have old Raspberry B and Piface Digital 1. Problems started after upgrading from Wheezy to Jessie.

Return to “Troubleshooting”