Introduction: 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!

Step 1: Parts

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.

Step 2: The App

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.

Step 3: Raspberry Pi

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.

Step 4: Resume

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

Comments

author
EltonV3 (author)2017-05-19

There is a way to use $ Put to get some information from an input of raspberry pi and use that information in android studio?

pleaaaaaaaaseeeee

author
SurabhiD4 (author)2017-05-13

can you explain same with android things.......................

author
HafizMuhammadS (author)2017-04-28

is there any way to receive the request using python??

author
VictorZ5 (author)2017-01-17

Awesome! Just learned a ton fumbling my way through this one! Question: I tried using my public IPaddress in the hopes that I could control the rpi from anywhere using the app. No dice. It brings me to an optimum login (testing it in my browser with the PublicIP/?led1=1). Is there anyway to do this or will this app only work if my phone is on the same wifi network as the rpi?

author
Laurens-Wuyts (author)VictorZ52017-01-18

Hello,

I'm glad you liked it.
To use this app with your external IP, you have to open port 80 of your router, but as you get a 'site' already, your router is probably using port 80 for his own configuration page, you could try to change the port of the app and the port of the server. But you than have to open this port to use it.

This app + server aren't very safe, so there are risks connecting it to the internet.

Laurens

author
anmolh12 (author)Laurens-Wuyts2017-03-17

Hello Laurens,

Thank you so much for
such a wonderful and easy tutorial.This helped me a lot in understanding
the basics and also learn new things.I'm glad that i was able to get
this done in the first time.

Now,I'm trying to control servo motor
applying the same concept, but I'm not able to write php code for
controlling servo.It will be very thankful if somebody can help me on
this

author
anmolh12 made it! (author)2017-03-17

Hello Laurens,

Thank you so much for such a wonderful and easy tutorial.This helped me a lot in understanding the basics and also learn new things.I'm glad that i was able to get this done in the first time.

Now,I'm trying to control servo motor applying the same concept, but I'm not able to write php code for controlling servo.It will be very thankful if somebody can help me on this

IMG_20170317_172920.jpgIMG_20170317_172835.jpg
author
anmolh12 (author)2017-03-16

I'm using this tutorial to operate a servo motor.I've connected my servo motor to the rpi an d want to control it using the app.In '/var/www/html' i've stored a 'servo.py' file in which code for motor operation is present.However,i dont know the php code for operating motor via app.Can somebody help me with this?

author
ChiragA13 (author)2017-03-16

How to change the ip address of the app..????

author
BramwellS (author)2017-02-12

Android studio says

Error:(33, 49) error: cannot find symbol variable led1

Error:(34, 61) error: cannot find symbol variable led2

Error:(35, 49) error: cannot find symbol variable led3

Thanks for any help :)

author
CharlesP107 (author)2017-02-08

Very nice! I would like to connect the pi with a car remote fob. I have a remote starter and if I hit the lock button 3 times it will start the car. I can get a $29 hot spot that has 300meg free/month with no contract that the pi can be connected to. So then I could start the car from far away through the phone! What do you think?

I also took apart a battery powered remote door bell wired it up to the pi power and all and then with a php script that fires every time one of the doorbell buttons are pressed I get an email and txt. The small door bell receiver actually fits in the case of the pi! Great Panic button for my 83 year old mom who lives alone.

Oh I just so love Pi........

author
ZahraddeenP (author)2017-01-21

Very interesting work! Does it work with other webservers such as webiopi? Thanks

author
ivanovishado (author)2016-04-22

Hi, I followed all the steps. But when I use the app, the LEDs don't do anything.

I know that the server is running on the RPi, because when I access to the RPi's IP through a web browser, all the LEDs turn on.

The app has the same code you use.

Anything else I have to check?

author

Have you changed the ip address in the app to the ip address of your raspberry pi?

Laurens

author

Yes, I have.

author

Is your phone connected to the same network as your Raspberry pi?
Can you check if you get an error using LogCat (android studio)?

Laurens

author
priyank123 (author)Laurens-Wuyts2017-01-04

how did you connect your raspberry pi local network

author
Laurens-Wuyts (author)priyank1232017-01-04

I used a wired ethernet connection to connect the Raspberry Pi, but WiFi should also work fine.

Laurens

author

Yes, it is.

I see a lot of errors, but I assume that they are there because I can't launch my AVD to debug the app. Also, I can't connect my phone through ADB to debug due to some unknown errors.

I forgot to mention that I commented some lines of your code in order to work on my project.

I commented the onOptionsItemSelected() and onCreateOptionsMenu() code.

author

Have you added the permission to use internet to the app? (Other tutorial step 7)
If you open the file "/var/log/apache2/access.log" on your raspberry pi, can you see your phone? (Or some error)

Laurens

author

Yes, I have added the permission.

I can't see my phone in the access.log, I can only see other requests made by my browser.

author

That means that your phone can't connect to your raspberry pi.

It's difficult to find the problem if you can't see if there are any error's on the phone.

What android version are you running on your phone?

Laurens

author

I built the APK and there were no errors. May I pass you the list of the errors that appear when I try to debug?

I'm using CyanogenMod 13 on an Asus Zenfone 2 (Z00A.)

author

Yes, you may send it, but use something like pastebin.com.

What is the IP address of your Raspberry pi, then I can build the app and you can try this one.

Laurens

author

Here it is: http://hastebin.com/oqaworejef.vhdl

I don't have a fixed IP address on the Pi, but my router assign range is in 192.168.0.0/24.

I always check for the IP that the Pi has in that moment and build the apk with the respective address.

author

The error messages are not from the app.

Is your php file index.php or something else?

Laurens

author

Yes, it's index.php

author
AungK17 (author)ivanovishado2016-09-09

Hi. Did you manage to work? Please let me know.

author
ivanovishado (author)AungK172016-09-09

Hi. Yes, I did. Although I don't remember anymore what was the solution to the problems that I asked on this thread.

author
JonR101 (author)ivanovishado2016-10-11

I got the same issue as you, If i connect to the host through my webbrowser, the leds will turn on. But if i try it with the app, nothing happens. My phone also doesnt show up in the access log.

author
ivanovishado (author)JonR1012016-10-11

It seems that the problems with the phone were caused by the drivers. Have you tried installing them again?

About the code... Instead of using HTTPURLConnection, I used AsyncHTTPClient, you should review that.

author
JonR101 (author)ivanovishado2016-10-12

Tnx for the answer, which drivers do you mean? I tested out the android virtual machine, the app works fine through the AVD. Another thing is that my Pi is connected to my laptop and is given internet through ethernet(laptop is using wifi).

author
ivanovishado (author)JonR1012016-10-12

Then you should test your program with the AVD, no problem.

I've never connected my Pi in that way, but your Pi should have its own IP address.

author
JonR101 (author)ivanovishado2016-10-13

Its working now, it didnt work because i connected the rpi to my laptop and shared the internet. Somehow that setup wouldnt allow it to work. I connected the rpi directly to my router and now its working.

author
daOnlyQuan (author)2016-10-02

Thanks for the tutorial! I added in some Python scripts to the project that will get temperature readings. PHP will execute the script when I toggle a button from the android app.

How do I retrieve the temperature reading and display as TextView in the android app?

author
AdslakhdA (author)2016-09-29

It is possible using the same method to make the motor move?

author
AungK17 (author)2016-09-08

Hi. I followed through the steps you've given but the app doesn't seem to work. Anyway, how to run the the index.php script? I am new in php language. I put it under var/www/html.

author
Laurens-Wuyts (author)AungK172016-09-09

Hello,

If you have apache installed, it will run the index.php when you send a HTTP request to the IP address of your raspberry pi.
Are you using the newest version of apache? If so, you probably need to place the file in var/www/public_html

Laurens

author
AungK17 (author)Laurens-Wuyts2016-09-09

Thanks for your quick response. So, whenever I want to run the Android app do I have to leave the web browser open like this in the image attached? E.g. http://Raspberry'sIP//.

20160909_174940.jpg20160909_175019.jpg
author
Laurens-Wuyts (author)AungK172016-09-09

No, the android app, says to the server to run the php file when he sends the http request.
I think you should change the name of your document from 'leafpad' to 'index.php'.

Laurens

author
AungK17 (author)Laurens-Wuyts2016-09-09

Ok. I renamed the file to "index.php". However, the app is still not working. I copied the three files for Android app: AndroidManifest.xml, MainActivity.java and styles.xml by creating a new project and I just changed the IP to my Raspberry Pi and put INTERNET permission. Removed the onOptionsItemSelected() and onCreateOptionsMenu() codes. I am still figuring out why its still not working for me :( Please help.

author
Laurens-Wuyts (author)AungK172016-09-09

What is the problem with the app?
Is it crashing or is it not changing the outputs?
What do you get when you open the IP address of your raspberry pi on a browser on your android device?

Laurens

author
AungK17 (author)Laurens-Wuyts2016-09-10

The app has no errors in Android studio. I couldn't control the LEDs with the app. Might be any other issue or something missing in the app?

author
Laurens-Wuyts (author)AungK172016-09-10

Do the lights change when you go to:
http://IPAddress/?led1=1

On the browser of your android device.

Laurens

author
AungK17 (author)Laurens-Wuyts2016-09-11

No. :( Maybe you could send me the working app solution might be missing something I think. Anyway, the app code you've given in the link is intended for Arduino, can it be able to work for Raspberry Pi 3?

https://www.instructables.com/id/Control-Arduino-using-android-app/#step5

author
mjd1290 made it! (author)2016-06-02

Awesome tutorial! Got this working in under an hour. Have lots of ideas planned now.

Screenshot_20160602-230510[1].png20160602_230502[1].jpgScreenshot_20160602-230514[1].png20160602_230448[1].jpg
author
AungK17 (author)mjd12902016-09-09

Hi. Could you please send me the Android app code?

author
Laurens-Wuyts (author)mjd12902016-06-02

I'm glad you liked it and got it working.

Have fun making other projects using it.

Laurens

author
wayneweezy (author)2016-09-07

Hi Laurens, great work. I managed to get this working. I was wondering if you have done a project that reads and writes to/from gpio. I tried to follow your "Raspberry pi android app communication" hoping to read and write data to/from raspberry but running into many error and problems with deprecation. (p.s working on a home automation project, trying to find easiest way to communicate with raspberry remotely from android phone). Thanks

author
AungK17 (author)wayneweezy2016-09-08

Hi. I am also trying out the project how do you run the index.php? Could you please help me! Do I have to key in any command to run it?

About This Instructable

37,742views

192favorites

License:

Bio: twitter: @Laurens_Wuyts
More by Laurens-Wuyts:How to Use the Beelink SEA I Media PlayerMinimalistic Led ClockControl Raspberry pi GPIO using an app
Add instructable to: