I want to turn on a gpio output and receive input from a gpio input. This is all initiated from a webpage. I am making an ajax call to a php file which itself runs the python script. It does not work. When I click on the button nothing happens. I can run the php file with php script.php and it all works. If I hash out all the references to gpio in the python script then the python runs. As a test I can make it write a string to a text file. I can also make it work by having the php write a string to a text file and having the python read the file and then access the gpio. Here is some code:-

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
    $status = "onn";
//    $stet = "lkjhgfdsa";
    $tmp = exec("python oneshot.py $status");
    echo $tmp;
    file_put_contents("/home/pi/boiler/function.pick" , $status);



#!/usr/bin/env python
import sys
import RPi.GPIO as GPIO
from  time import sleep

#import sys

GPIO.setup(12, GPIO.OUT)
GPIO.output(12, 1)

mode= sys.argv[1]
print mode
f = open('/home/pi/boiler/edom.pick', 'w')

print (mode)
if  mode == ('onn'):
       GPIO.output(12, 0)

elif  mode == ('off'):
       GPIO.output(12, 1)

Any help would be much appreciated.

  • When you call php in command line it's probably not the same user as your webserver is running and your current user is probably in gpio group for gpio authorized access. Try to add your webserver user to gpio group, for example : sudo usermod -a -G gpio www-data... You can read here – Ephemeral May 15 '20 at 20:53

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.