Simple WEB GPIO

From emboxit
Jump to: navigation, search
File:RPI-SIMPLE-WEB-GPIO.jpg
Diagram based on DIVERTEKA-2), shows the setup for simple Raspberry-Pi WEB-controlled GPIO.



Why

  • No Security, no passwords
  • Only 3 simple files needed
  • The files are small and easy to understand
  • helps to understand:
  • GPIO virtual files creation
  • GPIO virtual files permissions
  • rc.local
  • Simple script file (gpio.sh)
  • The minimal php program needed
  • Is easy to expand for more GPIO lines


Setup of Raspberry-Pi WEB-controlled GPIO

  • Copy gpio.sh to directory /etc/rc.local
This can be done with ftp client but not directly as ther is no permission to access /etc/rc.local
First brink gpio.sh to /home/pi with FTP
Then from/home/pi
sudo su
cp ./gpio.sh /etc/

Now gpio.sh is in /etc directory

  • Copy xue1.php to /var/www
cp ./xue1.php /var/www/
  • Edit rc.local
sudo nano /etc/rc.local

at the end of the file should be like this

fi

# Pin GPIO14 (OUT)
echo “14″ >  /sys/class/gpio/export
chmod 777 -R /sys/class/gpio/gpio14
echo “out” > /sys/class/gpio/gpio14/direction

# Pin GPIO8 (IN)
echo “8″  >  /sys/class/gpio/export
chmod 777 -R /sys/class/gpio/gpio8
echo “in” >  /sys/class/gpio/gpio8/direction

exit 0

The call to gpio.sh from rc.local did not work, but having the script inside the rc.local, as above, works!



Source code

xue1.php

<php> <?php

$lectura=exec("cat /sys/class/gpio/gpio8/value");

   if (isset($_POST['button1']))
   {
        exec("echo \"1\" > /sys/class/gpio/gpio14/value");
   }
   if (isset($_POST['button2']))
   {
        exec("echo \"0\" > /sys/class/gpio/gpio14/value");
   }
   if (isset($_POST['button3']))
   {
        $lectura=exec("cat /sys/class/gpio/gpio8/value");
   }

?>

<html> <head>

 <title> ** GPIO Control ** </title>

</head>

<body bgcolor="#c0c0c0">

== GPIO Control - XUE001 Board ==

   <form method="post">

DIGITAL (OUTPUT) => <button name="button1">ON </button> <button name="button2">OFF</button>

   </form>
   <form method="post">

DIGITAL (INPUT) <button name="button3"> READ </button> <input type="text" name="valor" value= "<?php echo $lectura; ?>" >

   </form>

</body> </html> </php>

gpio.sh

#!/bin/sh 
#
#
# GPIO-Pin  14 (OUT)
echo "14" >  /sys/class/gpio/export
chmod 777 -R /sys/class/gpio/gpio14
echo "out" > /sys/class/gpio/gpio14/direction
# GPIO-Pin 8 (IN)
echo "8"  >  /sys/class/gpio/export
chmod 777 -R /sys/class/gpio/gpio8
echo "in" >  /sys/class/gpio/gpio8/direction


rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

# GPIO export script
# /etc/gpio.sh

# GPIO-Pin  14 (OUT)
echo "14" >  /sys/class/gpio/export
chmod 777 -R /sys/class/gpio/gpio14
echo "out" > /sys/class/gpio/gpio14/direction
# GPIO-Pin 8 (IN)
echo "8"  >  /sys/class/gpio/export
chmod 777 -R /sys/class/gpio/gpio8
echo "in" >  /sys/class/gpio/gpio8/direction

exit 0


Links