I'm a student in electronics department doing a project on home automation using Raspberry Pi (for bulbs, fan, temperature and camera).

I want to access the GPIO pins from a web interface to execute a Python script to control the devices on the server (RPi).

I installed WiringPi but I can't move forward to control them.

See the example below of a webpage to control a simple led (on & off): https://i.sstatic.net/SLNyx.jpg

I have installed apache2 server.
I have paced the code in cgi-bin and pointed the button on to that link It says "permission denied" in error log file, it shows to try as root.

led.py in cgi-bin folder:


import RPi.GPIO as GPIO 
GPIO.setup(7, GPIO.OUT) 

3 Answers 3


WebIOPi may be the best solution for you. https://code.google.com/p/webiopi/ You can modify the software to your needs, but that will control all GPIO pins through a web interface. Try it out and see if it works for you.


Check out this project built for this exact reason: https://github.com/newbrute/pi-ui

The UI is not pre-configured and offers more flexibility than other options out there

Edit: To be clear, this is an alternative solution and not a fix for WiringPi.

Relevant code

<!-- Setup a pin. Since no direction is specified, it will be an output -->
<div ng-init="run('gpio.setup').with({channel: 18})" />
<!-- Write to the pin when buttons are clicked -->
<button ng-click="run('gpio.write').with({channel: 18, value: true})">
    Red On

NodeJS's child_process.exec function will allow you to run a python script in conjunction with this option

  • 1
    Welcome to Raspberry Pi! Please take the tour and visit the helpcenter to see how things work here. Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference only... and while it is not a product you're trying to sell here I would rather like to ask to give full disclosure that this is your own project (actually there's no need to hide that fact or to be ashamed ;)
    – Ghanima
    Commented Jul 19, 2017 at 19:51
  • Edited my answer. Yes it's my project. I'm not ashamed and I think it can eliminate a lot of headaches. Just a force of habit not to say "I" too much because people don't care, so I just said it "was built"
    – Kiran Kota
    Commented Jul 20, 2017 at 21:21
  • That I know ;) our English teacher never understood why we would use passive voice all the time over "I did that" and "We did that" in publications ;)
    – Ghanima
    Commented Jul 21, 2017 at 5:01

Write a php script for your website. This is the page you will see when you access your pi.

Send the button values via POST method to the script.

Extract the message and call a python script which will do the actual change on the GPIO.

$command = escapeshellcmd(sudo python /home/pi/path/to/script.py led 1); exec("$command > /dev/null &");

The first line would call the python script located in /home/pi/path/to/ and execute it. The > /dev/null will tell it to put all output into an non-existing file and the & will run it in the background. The last 2 are not really necessary but without it your website will load as long as it takes to execute the python script.

At the moment php could not start python because of the sudo command. It is nessesary because we need the privileges to work with the GPIO Pins. So we need to do: sudo nano /etc/sudoers and at the end add www-data ALL=(ALL) NOPASSWD: ALL

Write a Python Script that takes the input variables from php and performs the task.

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.