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!

asked Apr 5, 2018 at 20:49

1 Answer 1

2

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

answered Apr 5, 2018 at 21:23
11
  • In that case wouldn't this command also fail?: system("gpio -g mode 4 out"); Commented Apr 5, 2018 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 Commented Apr 5, 2018 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. Commented Apr 6, 2018 at 5:57
  • And that should not be the problem, because the first code works, the python command does not. Commented Apr 6, 2018 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. Commented Apr 6, 2018 at 7:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.