0

I want to be able to toggle a GPIO pin from from a web browser. I've found this forum topic (http://tiny.cc/d1eixx) and even the though code seems fine, it doesn't toggle the pin:

#!/usr/bin/python

import os
import wiringpi
from time import sleep

os.system("/usr/local/bin/gpio export 17 out")
wiringpi.wiringPiSetupSys()

io = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_SYS)
#io.pinMode(17,io.OUTPUT)

print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello World</title>'
print '</head>'
print '<body>'
print '<h2>Hello World</h2>'
print '</body>'
print '</html>'

io.digitalWrite(17,io.HIGH)
sleep(1)
io.digitalWrite(17,io.LOW)
sleep(1)

The script lives in the /cgi-bin folder, an Apache server is running and in fact I've managed to toggle an LED using Lighttpd, Flup and FastCGI. Still, I'd like to use WiringPi and understand what went wrong with the code above. When I run it in the browser ('192.168.1.3/cgi-bin/cgi.p') it displays the 'Hello World' message with no errors but the pin's state doesn't change (unless it's ran from the terminal). It seems to me that I still don't have permission to access the GPIOs even though the line 'os.system("/usr/local/bin/gpio export 17 out")' is supposed to do that. Any ideas why doesn't it work?

Edit: the apache error log:

[Mon Apr 27 16:43:36 2015] [error] [client 192.168.0.3] Unhandled exception in thread started by
[Mon Apr 27 16:43:36 2015] [error] [client 192.168.0.3] sys.excepthook is missing
[Mon Apr 27 16:43:36 2015] [error] [client 192.168.0.3] lost sys.stderr
[Mon Apr 27 16:55:42 2015] [error] [client 192.168.0.3] /usr/lib/cgi-bin/doStuff.py:9: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.$
[Mon Apr 27 16:55:42 2015] [error] [client 192.168.0.3]   G.setup(17, G.OUT)
[Mon Apr 27 16:55:48 2015] [error] [client 192.168.0.3] /usr/lib/cgi-bin/doStuff.py:9: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.$
[Mon Apr 27 16:55:48 2015] [error] [client 192.168.0.3]   G.setup(17, G.OUT)
  • 1
    Anything in /var/log/apache2/error.log? – joan Apr 27 '15 at 17:33
  • Strangely not today. I got this yesterday:/usr/lib/cgi-bin/doStuff.py:9: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.$ [Mon Apr 27 16:55:42 2015] [error] [client 192.168.0.3] G.setup(17, G.OUT) – alkopop79 Apr 28 '15 at 18:46
  • the IP address ending with '.03' is my laptop. – alkopop79 Apr 28 '15 at 18:47
  • Updated the post with the error log. – alkopop79 Apr 29 '15 at 18:54

Your Answer

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

Browse other questions tagged or ask your own question.