I would like to be able to connect a html website to the GPIO pins so that when a button is pushed on the website it turns on a GPIO pin on the pi. I am also wondering if it is possible to take data from another GPIO pin and print it in the html website
1 Answer
Yes, this is possible with a website.
I have built something similar here. It's about controlling a LED strip over the internet. I think you can take the sourcecode from it.
I wrote Python scripts to control the GPIOs. These Python scripts are called and executed by PHP scripts. The PHP script is called with "execute" a URL (e.g. http://raspberrypi/poweron.php). This could be done by a button on your website.
So this could be your Pyhton script: (saved as: /home/pi/Code/App/power_on.py)
#!/usr/bin/env python3
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(12, GPIO.OUT)
GPIO.output(12, GPIO.HIGH)
logging.info("{0}Power on".format(log_time))
And then your PHP script
<?php
$command = escapeshellcmd('/home/pi/Code/App/power_on.py');
$output = shell_exec($command);
echo $output;
?>
-
thank you @jjandke for your help but when I go to the website raspberrypi/test_led_file.php, it will just sit there doing nothing, just a blank white screen. I have tested the code separately and it runs perfectly when run via Geaney. Commented Jan 21, 2022 at 0:17
-
Your script will run with the permissions of the web server which might not have permission to set GPIO pins. See raspberrypi.stackexchange.com/a/39191/36775 for this. When you run it youself, the permissions used are your own and probably include the 'gpio' group which gives you access. Commented Jan 21, 2022 at 7:58
-
I think this page will help you: raspberrypi.stackexchange.com/questions/40105/… . As @patthoyts has already mentioned, the "www-data" user has no rights to /dev/mem.– JJandkeCommented Jan 23, 2022 at 19:33