WEB controlled GPIO

From emboxit
Jump to: navigation, search


A successful installation [15/08/2013]

Install phpmyadmin

broken link:Tutorial – Install PhpMyAdmin on your Raspberry Pi

sudo apt-get install phpmyadmin
sudo nano /etc/apache2/apache2.conf
       add a new line: Include /etc/phpmyadmin/apache.conf
sudo /etc/init.d/apache2 restart

Step 1

Must have the following running on the Raspberry Pi:

  • Apache web server
  • PHP5
  • MySQL Server
  • phpMyAdmin

Step 2 enable root account

  • Login to your Raspberry Pi with your username and password, then type the following:
sudo -i
passwd root
  • Now, type in (and confirm) a password for the root account.
  • Then you need to close the SSH session, and restart it - logging in as root.

Step 3 database set up

Configure phpMyAdmin using a browser

This solution relies on a MySQL Database, so let's set it up!
I am assuming that you have phpMyAdmin set up, alongside PHP5.

Login to your phpMyAdmin control panel, then press the "Import" button on the top bar.

Now, under the "File to Import" heading, click the

"Choose File" button

, and select the file you downloaded previously (gpio.sql).

Finally, at the bottom of the page,

click the "Go" button

This will set up all of the tables needed to ensure the the script functions as it should on your Raspberry Pi.

Now, you need to add a user to the database from within phpMyAdmin. To do this:

Click the "Users" button on the top bar.

Now click the

"Add User" link 

(about half way down the page on the left).
In the

"User name" field, 

enter a suitable username. I went with

gpio

In the

"Host" field, enter "localhost".

Then in the two password fields, enter a suitable password. (No spaces, Hypehens or special characters). I went with "pr03ND2". Now leave all of the rest as default, then click the

"Add User" button 

on the bottom right

  • The final part for this step is to give the user the correct privileges.

Click the "Users" button on the top bar, then scroll down until you see the user you have just added in the

"Users Overview" table.

Across from the username, click the "Edit Privileges" link. Scroll down to the heading

"Database-specific privileges" 

and select

"gpio" 
from the drop down list box, and click the "Go" button.
Select 
ALL of the check boxes, 

then click the

"Go" 

button on the bottom left.

Step 4 Shell scripting

  • This script is pretty simple, but does require setting up.
  • Firstly, download the script
sudo -i .
wget http://raspberrypi-gpio.googlecode.com/files/GPIOServer.sh
  • Once this has downloaded, type in the following:
chmod +x GPIOServer.sh
nano GPIOServer.sh
  • This will allow you to edit the script.
  • You must change the following variables at the top of the file:
mysqlusername="USERNAME HERE"
mysqlpassword="PASSWORD HERE"

These must be changed to the username and password you created previously in phpMyAdmin.

  • Once these have been changed, save it:
Ctrl x, 
Y

Step 5

For this, type the following commands, ensuring you are logged in as root. (type sudo -i if you're not).

wget http://raspberrypi-gpio.googlecode.com/files/control.php
wget http://raspberrypi-gpio.googlecode.com/files/off.jpg
wget http://raspberrypi-gpio.googlecode.com/files/on.jpg

Once they have downloaded, change file permissions:

mv control.php /var/www/control.php
chmod 755 /var/www/control.php
mv off.jpg /var/www/off.jpg
chmod 755 /var/www/off.jpg
mv on.jpg /var/www/on.jpg
chmod 755 /var/www/on.jpg

You must edit a few variables in the file before use, so type:

nano /var/www/control.php

Change the following variables:

$MySQLUsername = "USERNAME HERE";
$MySQLPassword = "PASSWORD HERE";

Now, navigate in your web browser to the control.php page. (mine is http://raspberryPi/control.php) where raspberryPi is your host name. It will ask you to login with the following credentials:

Username: admin
Password: gpio

I recommend clicking the "Change Password" link at the top of the page, and changing the password for obvious reasons.

Step 6

GPIOServer.sh is running
control.php provides the web page

Start an SSH session with your Raspberry Pi, and login as root, then type in:

./GPIOServer.sh

sudo -i does not help, so use instead:

sudo su 

which gives root privilages but does not change directory. It will ask you to input a wait time, this will depend upon your application needs, but the shorter the wait time, the more resources the script will use. (I generally use 5).

Now login to the web control interface (HOSTNAME = the IP address of R-Pi)

(http://HOSTNAME/control.php)

login and enjoy.

Step 7

  • For security reasons, you should now disable the root account
  • To keep updated on the latest releases, or to report a bug / glitch - go to
http://code.google.com/p/raspberrypi-gpio/


Source

GPIOServer.sh


# Script created by Daniel Curzon (http://www.instructables.com/member/drcurzon)
# Initial version created 10th June 2012
# Version: 1.0

###################################
#####  EDIT THESE BEFORE USE  #####
###################################
mysqlusername="gpio"
mysqlpassword="nx"

#############################################################################################################################
################################################### DO NOT EDIT BELOW THIS LINE ##############################################
##############################################################################################################################

#Set  Refresh
echo "How long do you want the wait time to be?          "
read waitTime

#Invoke GPIO
echo "4" > /sys/class/gpio/export
echo "17" > /sys/class/gpio/export
echo "18" > /sys/class/gpio/export
echo "21" > /sys/class/gpio/export
echo "22" > /sys/class/gpio/export
echo "23" > /sys/class/gpio/export
echo "24" > /sys/class/gpio/export
echo "25" > /sys/class/gpio/export

#Start Loop
while :
do
#Read MySQL Data
#Direction
direction4=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='4'";)
direction17=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='17'";)
direction18=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='18'";)
direction21=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='21'";)
direction22=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='22'";)
direction23=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='23'";)
direction24=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='24'";)
direction25=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='25'";)
#Status
status4=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='4'";)
status17=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='17'";)
status18=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='18'";)
status21=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='21'";)
status22=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='22'";)
status23=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='23'";)
status24=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='24'";)
status25=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='25'";)

#Run Commands
if [ "$direction4" == "out" ]; then
	echo "out" > /sys/class/gpio/gpio4/direction
	if [ "$status4" == "1" ]; then
		echo "1" > /sys/class/gpio/gpio4/value
		echo "GPIO 4 Turned On"
	else
		echo "0" > /sys/class/gpio/gpio4/value
		echo "GPIO 4 Turned Off"
	fi
else
	echo "in" > /sys/class/gpio/gpio4/direction
fi
if [ "$direction17" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio17/direction
	if [ "$status17" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio17/value
                echo "GPIO 17 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio17/value
                echo "GPIO 17 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio17/direction
fi
if [ "$direction18" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio18/direction
	if [ "$status18" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio18/value
                echo "GPIO 18 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio18/value
                echo "GPIO 18 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio18/direction
fi
if [ "$direction21" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio21/direction
	if [ "$status21" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio21/value
                echo "GPIO 21 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio21/value
                echo "GPIO 21 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio21/direction
fi
if [ "$direction22" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio22/direction
	if [ "$status22" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio22/value
                echo "GPIO 22 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio22/value
                echo "GPIO 22 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio22/direction
fi
if [ "$direction23" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio23/direction
	if [ "$status23" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio23/value
                echo "GPIO 23 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio23/value
                echo "GPIO 23 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio23/direction
fi
if [ "$direction24" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio24/direction
	if [ "$status24" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio24/value
                echo "GPIO 24 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio24/value
                echo "GPIO 24 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio24/direction
fi
if [ "$direction25" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio25/direction
	if [ "$status25" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio25/value
                echo "GPIO 25 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio25/value
                echo "GPIO 25 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio25/direction
fi
#Complete Loop
sleep $waitTime
done


External links