Control Raspberry Pi GPIO Using an App

by Laurens-Wuyts in Circuits > Raspberry Pi

86558 Views, 218 Favorites, 0 Comments

Control Raspberry Pi GPIO Using an App

front.jpg
Control Raspberry pi GPIO using an app

Many people have asked for a tutorial on how to control the Raspberry pi GPIO pins using a app(you made yourself).

So here is a quick tutorial on how to do this. It will be mostly based on previous tutorials I made.

Let's get started!

Parts

front.jpg

For this tutorial, you don't need many parts. You'll need:

  • A raspberry pi (any model should work, as long as it's connected to your network)
  • An android phone
  • A computer to put the app on the phone.

The App

app layout.jpg

For the app, you can use the app from this tutorial I made. Just change the IP address in the app to the IP address of your raspberry pi.

So this app should do it just fine and you can change it to how you like it the most.

Raspberry Pi

leds.png
IMG_20160207_132248.jpg

On the raspberry pi, you should install 2 things.

  • Wiring pi
  • apache2 with php5

Wiring pi is used to control the GPIO pins from the php page. Here you can see how to install siring pi.

Follow these instructions to install apache2 on a raspberry pi and also follow 'ADDITIONAL - INSTALL PHP' lower on the page.

Now the only thing you need to do is in '/var/www/html' remove index.html and make a new file index.php.

In index.php you add:

<?php 
exec("gpio mode 0 out");
exec("gpio mode 2 out");
exec("gpio mode 3 out");
if (isset($_GET['led1'])) {	
	if($_GET['led1'] == 1) {
		exec("gpio write 0 1");// pin 0 in wiring pi is gpio 17
	} else {
		exec("gpio write 0 0");
	}
}
if(isset($_GET['led2'])) {
	if($_GET['led2'] == 1) {
		exec("gpio write 2 1");// pin 2 in wiring pi is gpio 27
	} else {
		exec("gpio write 2 0");
	}
}
if(isset($_GET['led3'])) {
	if($_GET['led3'] == 1) {
		exec("gpio write 3 1");// pin 3 in wiring pi is gpio 22
	} else {
		exec("gpio write 3 0");
	}
}
?>

That's it. Now you should be able to control the GPIO pins of your raspberry pi using an app.

Resume

front.jpg

That's it. I hope you liked it.

If you have any question whatsoever feel free to ask. If you have something you would like to see done, please let me know in the comments.

Laurens