hainkiwanki
Posts: 3
Joined: Tue Aug 23, 2016 12:08 pm

bcm2835_init: Unable to open /dev/gpiomem: Permission denied

Tue Aug 23, 2016 12:22 pm

Hi,

Im running a localhost webserver on my raspbian that displays index.php which is located in /var/www/html. index.php:

Code: Select all

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Embedded OS</title>
    </head>
    <body>
		<h2>Raspberry Pi 2: Driver</h2>
		<form method="post" enctype="multipart/form-data" action=""> 
			<input type="radio" name="driver" value="led_on">Leds On
			<input type="radio" name="driver" value="led_off">Leds Off
			<input type="radio" name="driver" value="led_stat">Led Stats
			<input type="radio" name="driver" value="alt">Alt Read
			<input type="radio" name="driver" value="temp">Temperature
			<input type="radio" name="driver" value="light">Lightsensor
			<input type="submit" name="submit" value="Submit">
		</form>
        <?php
			if(isset($_POST['submit']))
			{
				echo "submit pressed\n";
				if($_POST['driver'] == "temp")
				{
					exec('sudo /home/pi/Downloads/bcm2835/temp', $value);
					print_r($value);
				}
				else
				{
					echo "none selected\n";
				}
			}
        ?>
    </body>
</html>
The code behind /home/pi/Downloads/bcm2835/temp is:

Code: Select all

#include <bcm2835.h>
#include <stdio.h>

int main(int argc, char **argv)
{
	if (!bcm2835_init())
	return 1;
	char buffer[4];
	buffer[0] = 50;			
	int i,temp;

	bcm2835_spi_begin();
	bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
	bcm2835_spi_setDataMode(BCM2835_SPI_MODE3);						//rising edge
	bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); 	//clock frequency
	bcm2835_spi_chipSelect(BCM2835_SPI_CS1);                      	//chip select 1
	bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS1, LOW);      	//activate chip select 0 


	buffer[0]=buffer[1]=buffer[2]=buffer[3]=0;

	//read de id
	buffer[0] = 0x58;						
	bcm2835_spi_transfern(buffer,2);
	printf("id:%02X\n",buffer[1]);
	
	//read temperature
	while(1)
	{
		buffer[0] = 0x50;					
		bcm2835_spi_transfern(buffer,3);
		temp = buffer[1]; 
		temp = temp<<8;
                temp = temp + buffer[2];
		temp = temp>>3;
		temp = temp/16;
		printf("temp:%d\n",temp);
		delay(500);
		break;
	}

	bcm2835_spi_end();
	bcm2835_close();
	return 0;
}
This code is compiled like this: gcc -o temp temp.c -l bcm2835.
When I select the temperature radio button and press on the submit button, it returns an empty array instead of some values. So I took a look inside the error.log from apache2 which said:

Code: Select all

bcm2835_init: Unable to open /dev/gpiomem: Permission denied
I changed the permissions from said directory with the following commands:

Code: Select all

sudo chown root.gpio /dev/gpiomem
sudo chmod g+rw /dev/gpiomem
Then I checked the permissions, just to be sure:

Code: Select all

root@raspberrypi:~# ls -la /dev/gpiomem
crwxr-x--- 1 root gpio 244, 0 Aug 23 11:56 /dev/gpiomem
But I still get an empty array returned. Any idea what I have to do ?

This is what the little program should return:
id:C3
temp:35
Thanks for reading.

RAZERZ
Posts: 57
Joined: Mon Jun 13, 2016 4:56 pm

Re: bcm2835_init: Unable to open /dev/gpiomem: Permission de

Tue Aug 23, 2016 12:53 pm

Hello and welcome to the raspberry pi troubleshooting forum.

Nice code you got there, you seem like you know your stuff around linux but, have you tried the "sudo" command?
Raspberry pi 3 set up as desktop with raspbian.
I guess you can say I am not newbie with linux but I am not a master at it either.
I will always try to help you as much as I can because I know how hard it is to run into problems :D

hainkiwanki
Posts: 3
Joined: Tue Aug 23, 2016 12:08 pm

Re: bcm2835_init: Unable to open /dev/gpiomem: Permission de

Tue Aug 23, 2016 1:06 pm

RAZERZ wrote:Hello and welcome to the raspberry pi troubleshooting forum.

Nice code you got there, you seem like you know your stuff around linux but, have you tried the "sudo" command?
Yup, still seem to be getting the error.

hainkiwanki
Posts: 3
Joined: Tue Aug 23, 2016 12:08 pm

Re: bcm2835_init: Unable to open /dev/gpiomem: Permission de

Fri Aug 26, 2016 3:21 pm

Ok so, for some reason I had to redo the command make modules_install again and it seems to be working now. Weird though. Must of missed it.

Killertechno
Posts: 216
Joined: Wed Jan 02, 2013 8:28 am

Re: bcm2835_init: Unable to open /dev/gpiomem: Permission denied

Tue Apr 03, 2018 3:38 pm

So what exactly?
I'm having same trouble, what did you do exactly?

Return to “Troubleshooting”