So I'm trying to make a website that has buttons you can press that activate PHP scripts, which in turn run a python program to edit a JSON file. When I try the PHP script on the command line of the server it all works perfectly, however, when I then try to run it over the internet it refuses to work for some reason. I know that the PHP script is definitely running because it is also told to switch a GPIO pin on the raspberry pi that everything is running on.
Here is the javascript that activates the PHP scripts:
function relay(rly) {
var b = new XMLHttpRequest();
b.open("GET", 'switchScripts/' + rly + '.php');
b.onreadystatechange = function() {
if( b.readyState == 4) {
if( b.status == 200) {
}else
alert("HTTP error "+b.status+" "+b.statusText);
}
}
b.send();
return false;
}
This is the PHP script that is then activated:
<?php
system ( "gpio -g mode 18 out" );
system ( "gpio -g write 18 1" );
system ( "sudo python3 /var/www/html/home/switchScripts/updater.py RLY1 1" );
?>
This then activates the python program which is as follows:
import json
import sys
GPIOlabel = sys.argv[1]
state = sys.argv[2]
with open('/var/www/html/home/switchScripts/data.json', 'r') as datafile:
data = json.load(datafile)
data[GPIOlabel] = state
with open('/var/www/html/home/switchScripts/data.json', 'w') as datafile:
json.dump(data, datafile)
As I said previously, when trying this logged into the command line it all functions perfectly (The JSON is edited), so I'm at a complete loss as to why it won't work when I try it in a web browser.
As an aside I'm relatively new to this so if you have a better way of doing things then id be more than happy to hear it.