gupi
Posts: 6
Joined: Mon Jul 29, 2013 7:51 pm

WiringPi-PHP for version 2 of WiringPi

Mon Jul 29, 2013 8:33 pm

Hello,
I tried to use the WiringPi-PHP from GitHub but this seemes to be outdated.

I've created a PHP extension using Gordon's WiringPi Version 2 in the following manner:
- Creation of a wiringpi.i file based on the file from WiringPi2-Python
- Creation of the wrapper files with swig (-> wiringpi.php, wiring_wrap.c … )
- Compiling and linking the wiring_wrap.c with the source files from Gordons WiringPi to wiringpi2.so
- Instalation of the wiringpi2.so as a PHP Extension
- checked the installation with phpinfo()
- Included the wiringpi.php within my php script

Code: Select all

<?php
include('wiringpi2.php); // file with the abstract wiringpi2 class created by swig
class wp2 extends wiringpi2 {}
wp2::wiringPiSetup();
wp2::pinMode(1,2); // pwm mode
wp2::pwmWrite(1,1024); 100%
…
?>
but when I start my script it ends up with an entry in the error.log file of PHP : "WiringPiSetup: Must be root. (Di you forget sudo?)"

what's wrong with my procedure? What else need to be done to run this wiringPiSetup() function with the required credentials?

regards
Gunter

User avatar
gordon@drogon.net
Posts: 2023
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: WiringPi-PHP for version 2 of WiringPi

Sat Aug 03, 2013 10:02 pm

Always best to get wiringPi directly from https://git.drogon.net/ (The one on github is the one maintained by Gadgetoid for the Python wrappers)

As for the error - it is what it says - to use wiringPi, your program needs to run as root.

The alternative is to use wiringPiSetupSys () as the initialiser - but before you run your program in that mode, you need to first use the gpio command to export the GPIO pins you need to use. (You can use system() if you need to).

-Gordon
--
Gordons projects: https://projects.drogon.net/

gupi
Posts: 6
Joined: Mon Jul 29, 2013 7:51 pm

Re: WiringPi-PHP for version 2 of WiringPi

Sat Aug 03, 2013 11:17 pm

Hi Gordon,
thanks for your response.
Always best to get wiringPi directly from https://git.drogon.net/
this is exactly where I got it from. I used wiring.i from Gadgetoid as a template only.
Is there any example for using wiringPiSetupSys () somewhere in the web or can you provide one?

Best regards
Gunter

User avatar
gordon@drogon.net
Posts: 2023
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: WiringPi-PHP for version 2 of WiringPi

Sat Aug 03, 2013 11:24 pm

gupi wrote:Hi Gordon,
thanks for your response.
Always best to get wiringPi directly from https://git.drogon.net/
this is exactly where I got it from. I used wiring.i from Gadgetoid as a template only.
Is there any example for using wiringPiSetupSys () somewhere in the web or can you provide one?

Best regards
Gunter
You use it the same way you use the others - you pick one of:

wiringPiSetup (); # WiringPi pin numbers
wiringPiSetupGpio (); # bcm pin numbers
wiringPiSetupPhys(); # Physical P1 pin numbers

These need root access, or:

wiringPiSetupSys();

This uses the /sys/class/gpio interface which would normally need root access to, but if you use the gpio program to export the pins you need, it changes ownership.

So if you need to blink an LED on GPIO0/BCM 17, then:

gpio export 17 output

and your program has:

Code: Select all

wiringPiSetupSys() ;
for (;;)
{
  digitalWrite (17, 1) ; delay (500) ;
  digitalWrite (17,0) ; delay (500) ;
}
You need to do the exports before running the program as wiringPiSetupSys() reads in all the exported pins just once.

-Gordon
--
Gordons projects: https://projects.drogon.net/

gupi
Posts: 6
Joined: Mon Jul 29, 2013 7:51 pm

Re: WiringPi-PHP for version 2 of WiringPi

Mon Aug 05, 2013 8:01 am

wiringPiSetupSys();

This uses the /sys/class/gpio interface which would normally need root access to, but if you use the gpio program to export the pins you need, it changes ownership.

So if you need to blink an LED on GPIO0/BCM 17, then:

gpio export 17 output
Does that mean I have to use BMC numbering system instead of the wiringPi numbers?
Do I have to change from:

Code: Select all

 205   wiringPiSetup () ;
 206 
 207   if (bits == 4)
 208     lcdHandle = lcdInit (rows, cols, 4, 11,10, 4,5,6,7,0,0,0,0) ;
 209   else
... 
to:

Code: Select all

 205   wiringPiSetupSys () ;
 206 
 207   if (bits == 4)
 208     lcdHandle = lcdInit (rows, cols, 4, 7,8, 23,24,25,4,0,0,0,0) ;
 209   else
 ... 
with exporting this previously:
gpio export 8 output
gpio export 7 output
gpio export 23 output
gpio export 24 output
gpio export 25 output
gpio export 4 output

I've no idea if I understood your advice right.

Gunter

User avatar
gordon@drogon.net
Posts: 2023
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: WiringPi-PHP for version 2 of WiringPi

Wed Aug 07, 2013 11:54 am

gupi wrote:
wiringPiSetupSys();

This uses the /sys/class/gpio interface which would normally need root access to, but if you use the gpio program to export the pins you need, it changes ownership.

So if you need to blink an LED on GPIO0/BCM 17, then:

gpio export 17 output
Does that mean I have to use BMC numbering system instead of the wiringPi numbers?
Do I have to change from:

Code: Select all

 205   wiringPiSetup () ;
 206 
 207   if (bits == 4)
 208     lcdHandle = lcdInit (rows, cols, 4, 11,10, 4,5,6,7,0,0,0,0) ;
 209   else
... 
to:

Code: Select all

 205   wiringPiSetupSys () ;
 206 
 207   if (bits == 4)
 208     lcdHandle = lcdInit (rows, cols, 4, 7,8, 23,24,25,4,0,0,0,0) ;
 209   else
 ... 
with exporting this previously:
gpio export 8 output
gpio export 7 output
gpio export 23 output
gpio export 24 output
gpio export 25 output
gpio export 4 output

I've no idea if I understood your advice right.

Gunter
Yes. I'm afraid so. Sorry.

The whole pin numbering thing is something I could wax lyrically, or rant and rave about for hours on end.

When I published my original pin numbering, it was based on the pin numbers in the original GPIO diagram published on the eLinux wiki site. I recieved a lot of criticism and abuse/sneering over it, so I then included the means to use the (non sequential) native pin numbering scheme that apparently is the way it's been done in Linux since forever. (Now try to run your program on a Rev 1 and a Rev 2 ... Is it pin 22 or 27, 0 or 2, etc. )-:

My intention was to help Arduino people understand the Pi without needing to know what's underneath. Pin 13 on an Arduino is port B, bit 5 - do you really want/need to know that? Similarly pin 0 on the Pi is BCM_GPIO pin 17 - try writing a script to light up 8 LEDs on the first 8 "normal GPIO pins one after the other - for i = 0 to 7 vs. for i in 17,18, 21 (or is it 27), etc.

The "sys" mode was added afterwards too - to allow for people who didn't want to run their program as root.

The alternative for you is you write your program using wiringPi mode (as it is now), test it with sudo, then when happy and you need to call it from PHP or some other script, make it set-uid root. (Which is how the gpio program works) Running anything as root always has potential security issues, but if it's just a little personal server, then the risk should be fairly low. Backup your data elsewhere, etc.

-Gordon
--
Gordons projects: https://projects.drogon.net/

StarkJohan
Posts: 17
Joined: Sun Nov 10, 2013 5:26 pm

Re: WiringPi-PHP for version 2 of WiringPi

Sat Mar 08, 2014 2:05 pm

I'm struggling with permissions with the php extension.

I used the latest wiringpi to build the wiringpi.so extension and it works like a charm from command line as user pi. However, I cannot seem to do the exports/permissions correctly for the www-data so that my lighttpd server can run the same php script.

Code: Select all

<?php
wiringPiSetupSys();
echo digitalRead(17);
digitalWrite(17,1);
delay(500);
echo digitalRead(17);
digitalWrite(17,0);
echo "\r\n";
?>
I used these instructions to keep the pins i need available: http://dissectionbydavid.wordpress.com/ ... do-access/

I'm clearly missing something…

User avatar
gordon@drogon.net
Posts: 2023
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: WiringPi-PHP for version 2 of WiringPi

Sat Mar 08, 2014 7:07 pm

StarkJohan wrote:I'm struggling with permissions with the php extension.

I used the latest wiringpi to build the wiringpi.so extension and it works like a charm from command line as user pi. However, I cannot seem to do the exports/permissions correctly for the www-data so that my lighttpd server can run the same php script.

Code: Select all

<?php
wiringPiSetupSys();
echo digitalRead(17);
digitalWrite(17,1);
delay(500);
echo digitalRead(17);
digitalWrite(17,0);
echo "\r\n";
?>
I used these instructions to keep the pins i need available: http://dissectionbydavid.wordpress.com/ ... do-access/

I'm clearly missing something…
I'm presuming you do the exports at boot time, so in you put them in e.g. /etc/rc.local then it's run as root, then you can

sudo -u www-data /usr/local/bin/gpio export 17 in

and so on to export the pins you're using and the direction. The gpio command will change ownership to www-data for you.

-Gordon
--
Gordons projects: https://projects.drogon.net/

StarkJohan
Posts: 17
Joined: Sun Nov 10, 2013 5:26 pm

Re: WiringPi-PHP for version 2 of WiringPi

Sat Mar 08, 2014 7:24 pm

I've tried that without any luck. It doesn't matter if I export the pins for the www-data user, the user cannot access them using php. It works fine for the pi user using php from cli.

At the moment I've worked around the issue by allowing www-data to sudo the php script (which calls it self) and that works quite well even if it makes the solution far from pretty. Security isn't an issue as this is a pi dedicated for this single task.

Code: Select all

if (php_sapi_name() == 'cli') {
	echo "This is cli\n";

	if (isset($argv[3])) {
		$pin = [
	    	17 => $argv[1],  // Eller 17 / 0
	    	27 => $argv[2],  // Eller 27 / 2
	    	22 => $argv[3],  // Eller 22 / 3
		];

		foreach ($pin as $key => $val) {
			echo 'Set '.$key.' pin to: ' . $val . "\n";	
			digitalWrite($key,$val);
		}

	} else {
		die("Three arguments must be passed to this script. They must be 0 or 1.");
	}

} else {
	$r = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING); 
	/*$pin = [ // wiringpin numbers
    	0 => $r['r1'],   
    	2 => $r['r2'],   
    	3 => $r['r3'],   
    ];*/
    if (isset($r['r1']) AND isset($r['r2']) AND isset($r['r3'])) {
    	shell_exec('sudo /usr/bin/php /var/www/index.php '.$r['r1'].' '.$r['r2'].' '.$r['r3']);
    	echo "Relay 1: ".$r['r1'].", Relay 2: ".$r['r2'].", Relay 3: ".$r['r3'];
    } else {
    	die("Three arguments must be passed to this script. They must be 0 or 1.");
    }
}

User avatar
gordon@drogon.net
Posts: 2023
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: WiringPi-PHP for version 2 of WiringPi

Sat Mar 08, 2014 7:34 pm

StarkJohan wrote:I've tried that without any luck. It doesn't matter if I export the pins for the www-data user, the user cannot access them using php. It works fine for the pi user using php from cli.
Hm. It worked for me (tm)

As root (which you would be in a boot script)

Code: Select all

root@fuzepi:~# sudo -u www-data /usr/local/bin/gpio export 17 in
root@fuzepi:~# ls -l /sys/class/gpio/gpio17/value 
-rw-r--r-- 1 www-data www-data 4096 Mar  8 19:32 /sys/class/gpio/gpio17/value
Ah, make sure the path to sudo is in there too - might need /usr/bin/sudo in there...

-Gordon
--
Gordons projects: https://projects.drogon.net/

StarkJohan
Posts: 17
Joined: Sun Nov 10, 2013 5:26 pm

Re: WiringPi-PHP for version 2 of WiringPi

Sat Mar 08, 2014 7:47 pm

The problem isn't after boot. I'm issuing the commands straight up. Permissions are correct I think (see below) but the wiringpi.so php module can't reach the pins if the user www-data calls the script.

Code: Select all

pi@pisms ~ $ sudo -u www-data /usr/local/bin/gpio export 17 out
pi@pisms ~ $ ls -l /sys/class/gpio/gpio17/value 
-rwxrwx--- 1 www-data www-data 4096 Mar  8 20:16 /sys/class/gpio/gpio17/value
pi@pisms ~ $ 

StarkJohan
Posts: 17
Joined: Sun Nov 10, 2013 5:26 pm

Re: WiringPi-PHP for version 2 of WiringPi

Sat Oct 18, 2014 2:00 pm

I forgot about this old post but what eventually solved my problem was this excellent information:

http://dissectionbydavid.wordpress.com/ ... do-access/

Mir Sayeed Mohammad
Posts: 5
Joined: Sat Jun 06, 2015 1:10 pm

Re: WiringPi-PHP for version 2 of WiringPi

Mon Jun 15, 2015 11:49 am

Hello mr

i have copied a program into codeblocks from the web to run a sonar sensor with the gpio headers.

#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>

#define TRUE 1

#define TRIG 5
#define ECHO 6

void setup() {
wiringPiSetup();
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);

//TRIG pin must start LOW
digitalWrite(TRIG, LOW);
delay(30);
}

int getCM() {
//Send trig pulse
digitalWrite(TRIG, HIGH);
delayMicroseconds(20);
digitalWrite(TRIG, LOW);

//Wait for echo start
while(digitalRead(ECHO) == LOW);

//Wait for echo end
long startTime = micros();
while(digitalRead(ECHO) == HIGH);
long travelTime = micros() - startTime;

//Get distance in cm
int distance = travelTime / 58;

return distance;
}

int main(void) {
setup();

printf("Distance: %dcm\n", getCM());

return 0;
}


at first it showed permission denied. no file as wiringPi.h was found. So i did the following:

sudo apt-get install git-core
git clone git://git.drogon.net/wiringPi
cd wiringPi
git pull origin
./build

now after compiling, the program was running. now it shows, WiringPiSetup: must be root (did you forget sudo?)
i ran codeblocks from the start menu>prgramming>codeblocks
what should i do now?
note that i havent connected anything to the gpio yet.

Heater
Posts: 18759
Joined: Tue Jul 17, 2012 3:02 pm

Re: WiringPi-PHP for version 2 of WiringPi

Mon Jun 15, 2015 12:40 pm

"WiringPiSetup: must be root (did you forget sudo?)"

Presumably as you are posting to this thread you have read some of it. The answer to your question is a couple of posts back.
Memory in C++ is a leaky abstraction .

Azazel84
Posts: 1
Joined: Wed Mar 23, 2016 8:27 am

Re: WiringPi-PHP for version 2 of WiringPi

Wed Mar 23, 2016 8:43 am

Hi,
I have the same probleme.
When i use wiringpi in tomcat service I have this in my log :

wiringPiSetup: Must be root. (Did you forget sudo?)

This log match with this "final GpioController gpio = GpioFactory.getInstance();"

But i dont know how i can fix it.

Regards.

User avatar
DougieLawson
Posts: 41892
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: WiringPi-PHP for version 2 of WiringPi

Thu Mar 24, 2016 6:44 am

http://wiringpi.com/wiringpi-update-to-2-29/
Set an envvar

Code: Select all

export WIRINGPI_GPIOMEM=1
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on Twitter/LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

Return to “Other programming languages”