0

So I'm trying to get this to work. I have a .py file that turns the servo, and it works, but I'm trying to run it through a browser.

So I installed apache and php, and I made .php file which, when opened in a browser, runs commands in the terminal:

<?php
system("gpio -g mode 4 out");
system("gpio -g write 4 1");
?>

It successfully turns on the led. But I tried making a .php file that would run the .py file which spins the servo:

<?php
system("python /home/pi/Desktop/python/servo.py");
?>

I also tried adding sudo in there but didn't get any results. If anyone could help me I'd really appreciate it!

1

Presumably this is a permissions problem.

Try adding www-data to the gpio group. This may be enough to allow it to manipulate the GPIO. You will probably have to restart the web server for the permission change to take effect.

sudo adduser www-data gpio

I have not considered the security implications of giving www-data GPIO access.


An alternative is to use (my) pigpio daemon.

On the Pi make sure the pigpio daemon is running.

sudo pigpiod # as a one off command

or

systemctl enable pigpiod # to start daemon at boot

You can then use the pigpio Python module or the pigs command to control the servos.

For a Python example see http://abyz.me.uk/rpi/pigpio/examples.html#Python_servo_demo_py

  • In that case wouldn't this command also fail?: system("gpio -g mode 4 out"); – John Hawthorne Apr 5 '18 at 21:43
  • 1
    @JohnHawthorne The wiringPi gpio utility is set uid root. -rwsr-xr-x 1 root root 37068 Oct 31 13:51 /usr/bin/gpio – joan Apr 5 '18 at 22:02
  • I've tried adding a line to the sudoers file, like people suggested, but it corrupted it and I had to reinstall my system. – SteroidCookies Apr 6 '18 at 5:57
  • And that should not be the problem, because the first code works, the python command does not. – SteroidCookies Apr 6 '18 at 6:01
  • @SteroidCookies Why didn't you do what you were asked to do? If you didn't understand what was suggested you should have asked for clarification. I explained why the gpio command would work. – joan Apr 6 '18 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.