Controlling the RaspberryPi GPIO remotely, over a network or across the internet, seems like an interesting trick.
I copied some code from the net and gave it a try, but unfortunately it didnāt want to play.
So I went back to basics and created my own simple test routine.
The web server
I built a fresh web server by installing Lighttpd and PHP as described in this earlier post. My new server can only be accessed on my personal network, and this is by using a browser on another networked computer.
The RaspberryPi web server IP is (in my case) 192.168.0.22 and Iāve set the lighttpd port to 9080. So the url: 192.168.0.22:9080/ takes me to the /var/www folder on the web server.
In order to communicate with the GPIO I also installed Gordon Hendersonās wiringPi as covered in this post.
My php test program (which Iāve added to /var/www) is called gpio_test.php and looks like this:
It basically reads the current output level (or status) of GPIO pin 7, and tries to invert it (e.g. toggle ā0ā to ā1ā or ā1ā to ā0ā). So each time I refresh the page in the web browser, I should see the output change state.
I havenāt actually bothered to connect a light to pin 7, and it doesnāt matter which GPIO pin I choose for this test.
When this is run by remotely entering: 192.168.0.22:9080/gpio_test.php in a web browser, I get this simple black on white page:-
The program returns an error code each time it tries to read the state of GPIO Pin 7. Error code 127 is ācommand not foundā and this is happening because the user account does not have permission to access the GPIO.
By adding āsudoā to each of the execute commands like this:
ā¦the output changes slightly to this:-
Now the error code is 1 which is a pretty non-specific Linux error, but I think it is being returned because Iām not providing a password after sudo. Obviously I donāt want to code-in a password, but I can add the user to sudoers.
The āuserā on the web server is called www-data, so I can open /etc/sudoers as root and add this line at the end of the file:-
For more detail: RaspberryPi Web server: GPIO access