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
66 Discussions
Question 6 months ago on Introduction
How to control raspberry pi PWM:java?
Question 10 months ago
If i want to control it through remote location then what to do?
1 year ago
Is there a way to read also GPIOs pins using only PHP?
1 year ago
Great work. I've download the whole package (Android Studio) to see how it works but unfortunately I got around 19 errors while compiling :O Is it the code obsolete?
1 year ago
Hi there. Great work. Is code for Android Studio still valid? I've downloaded the whole files and it showed me a lot of errors. Thanks in advance
1 year ago
is there any way to receive the request using python??
Reply 1 year ago
Hafiz did you found anything on the Python side? to receive the request using python from android java.
1 year ago
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
1 year ago
can you explain same with android things.......................
2 years ago
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?
Reply 2 years ago
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
Reply 1 year ago
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
1 year ago
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?
1 year ago
How to change the ip address of the app..????
2 years ago
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 :)
2 years ago
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........
2 years ago
Very interesting work! Does it work with other webservers such as webiopi? Thanks
2 years ago
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?
Reply 2 years ago
Have you changed the ip address in the app to the ip address of your raspberry pi?
Laurens
Reply 2 years ago
Yes, I have.