I wanted to blink or just lit up an LED on my Pi GPIO pin via a server/website.

I've the resources like Pi, LED, php server etc.

I just don't know how to code to connect my Pi to server and control GPIO pin using server commands, help me please.

1 Answer 1


You will need to use the php command shell_exec() to execute a script or bash command which allows you to access the GPIO Pins. I would suggest writing a python script which takes input parameters and manages the turning on/off accordingly.

Then, from php, you would call this script using (for example) shell_exec("sudo /usr/bin/python /path/to/script.py on 50");

You will need root access to use the GPIO (that's why there's the sudo there), one idea to do this is is allowing this one file to be executed without password for the user www-data: Edit /etc/sudoers as root (using the command visudo) and add

pi raspberry = (root) NOPASSWD: /usr/bin/python /path/to/script

at the end for user pi on host raspberry.

On how to access the GPIO via python, take a look here. You can read the command line parameters using

import sys
parameterone = sys.argv[1] #sys.argv[0] will always be the filename

If you want continous blinking and turning it off again via the php webinterface, it's probably easiest to have a configuration file somewhere, and have the python file (with an infinite loop) run at startup and check the file every second. From php, alter the contents of the file to control what the python script does.

I hope this helps, have fun experimenting!

Your Answer

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

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