This is an updated version of my previous Instructable: Control Stuff with your Raspberry Pi (GPIO) Over the internet.
This version is much more secure and could easily be used across the internet with very few security concerns.
A brief outline:
- A server program that runs on the Raspberry Pi to read variables
- A MySQL database to store the variables in
- An Apache2 served webpage to allow control of the variables
Step 1: Knowledge Base
- Apache web server
- MySQL Server
- phpMyAdmin would help
You can find instructions for this here.
It may help to read up on my other Instructable: Control Stuff with your Raspberry Pi (GPIO) so you know what;s going on.
Step 2: Enable Root Account
Login to your Raspberry Pi with your username and password, then type the following:
Now, type in (and confirm) a password for the root account.
Then you need to colse the SSH session, and restart it - logging in as root.
Step 3: PhpMyAdmin & Database Setup
I am assuming that you have phpMyAdmin set up, alongside PHP5. If not, click here for an Instructable on how to do this.
Firstly, download the sql file from here.
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.
Congratulations - That's the database set up.
Step 4: Shell Script
This script is pretty simple, but does require setting up.
Firstly, download the script by inputting the commands below (on your Raspberry Pi).
sudo -i and hit Return / Enter.
wget http://raspberrypi-gpio.googlecode.com/files/GPIOServer.sh and hit Return / Enter.
Once this has downloaded, type in the following:
chmod +x GPIOServer.sh and hit Return / Enter.
nano GPIOServer.sh and hit Return / Enter.
This will allow you to edit the script.
You must change the following variables at the top of the file:
These must be changed to the username and password you created previously in phpMyAdmin.
Once these have been changed, hold the Ctrl key and press x, then release the Ctrl key and press y, then hit Return / Enter.
That's it for the shell scripting session.
Step 5: Web Page Setup
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 and hit Return / Enter.
wget http://raspberrypi-gpio.googlecode.com/files/off.jpg and hit Return / Enter.
wget http://raspberrypi-gpio.googlecode.com/files/on.jpg and hit Return / Enter.
Once they have downloaded, type in:
mv control.php /var/www/control.php and hit Return / Enter.
chmod 755 /var/www/control.php and hit Return / Enter.
mv off.jpg /var/www/off.jpg and hit Return / Enter.
chmod 755 /var/www/off.jpg and hit Return / Enter.
mv on.jpg /var/www/on.jpg and hit Return / Enter.
chmod 755 /var/www/on.jpg and hit Return / Enter.
You must edit a few variables in the file before use, so type:
nano /var/www/control.php and hit Return / Enter.
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:
I recommend clicking the "Change Password" link at the top of the page, and changing the password for obvious reasons.
That's the end of the Web Page section.
Step 6: Usage
Start an SSH session with your Raspberry Pi, and login as root, then type in:
./GPIOServer.sh and hit Return / Enter.
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, (http://HOSTNAME/control.php), login and enjoy.
Step 7: Overview
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/
All comments and criticisms are welcome.
Hope this helps, Daniel.