Raspberry Pi Android App Communication




Introduction: Raspberry Pi Android App Communication

About: I'm an embedded ICT student.

Hello everyone

Because app's are being used almost everywhere, I decided to make my own tutorial on how to make an app and connect to a MySQL database hosted on a raspberry pi.

For exchanging the data between the server and the client, I'm going to use JSON.

Step 1: Parts

For this instructables you'll need several things.

  • An android phone or the emulator from android studio
  • A raspberry pi (I tried B and 2B)
  • An internet connection with port 80 forwarded to the IP of your Raspberry Pi (How to forward port 80?)
    you can do it without forwarding, but then it's only going to work inside your network
  • A computer (I'm running windows 8.1. But ,as mentioned in the comments, you can also use mac or linux)

Step 2: Software for the Computers

For this instructables you'll need android studio from Google.

Step 3: Software Raspberry Pi

I'm assuming you already have a running Raspberry Pi with raspbian. Otherwise: How to set up a raspbian?.

For the Raspberry Pi you'll also need software. As server I'm using:

  • Apache 2
  • PHP
  • MySQL
  • PHPMyadmin (Not needed but recommended)

To install apache 2, PHP and MySQL I used this tutorial.
To install PHPmyadmin I used this tutorial.

Step 4: Creating the Database Using Phpmyadmin

First open a web-browser and go to:

Login to phpmyadmin
Click Databases
Create new database: Parts
Press create.

Open the database Parts.
Create new table Parts with 3 fields.
Press Start

First column:

column: ID
Type: INT
Auto increment: v

Second column:
column: Name
Length: 30

Third column:
column: part_nr
Type: INT

Press Save

Step 5: Server Side Scripting

On the Raspberry Pi we'll need 4 files. You need to place them in the www directory of your raspberry pi:

UPDATE: On raspbian Jessie, you have to put them in /var/www/html

The db_config file is used for storing the username, password and database name
The db_connect file is used to connect to the database specified in the db_config file.
The db_create file is used to create a row in the table in the connected database.
The db_view file is used to get all data stored in the database.

Step 6: Writing an App (Layout)

In this step I'll explain how to make a simple app using android studio.
Here we will make the layout of the app and in the next step I'll explain how to make the app working and how to communicate with the raspberry pi server.

So first start Android Studio.

Give your app a name and choose the directory to place it. Press Next.
Choose which devices you would like to support. I'm using phones starting from android 4.0.3.
After that you need to choose the activity. Choose: "Blank Activity".
Let the last screen as it is.

After all these steps, you get an example "hello world" app.

In activity_main.xml remove the textview element at the right bar.

In values/strings.xml remove:

Hello world!

Go back to main_activity.xml

Drag a linear layout (vertical) to the top left corner of the screen.

Drag 2 text fields to the screen. The first one plaintext and the other one Number.

At the right scroll down untill you see "id".
Change this for the first one to Name and for the second one to part_nr.

At the right scroll down untill you see "hint".
Change this for the first one to Name and for the second one to Part Number.

Now drag a button onto the screen and change "text" to Send.

Now is the layout done.

You can test the app by connecting your phone to your computer and compiling the app.(The green arrow a the top)
This will take a while.

It should look something like the last image.

Up to the coding of the app.

Step 7: Writing an App (Java Code)

For making an app work, you don't only need a layout. You also need the code running in the background.
The language we are going to use is Java, because all android apps are written in Java. So lets start coding.

First of all, you need to download the attached file.
This file is a library so you can easily use JSON.

Place this file in:

For me it's:

After that you can open JSONParser in android studio. You now need to change the first line to:
package company.app_name;

Ok now we can start writing the main code.
Open the file MainActivity.java

First make a new Progress-dialog. This is used to display when the code is executing and you need to wait.

Place this in the main class.
private ProgressDialog pDialog;

Then we need a JSONparser object to send the data. So place this line under the previous line:
JSONParser jsonParser = new JSONParser();

Now we need two EditText items, so we can extrude the inserted text. This goes under the JSONParser.
EditText editText;
EditText editText2;

We move on to the oncreate function. Here we need to link the EditTexts from above with the real edittexts in the layout. You do it like this:
editText = (EditText) findViewById(R.id.Name);
editText2 = (EditText) findViewById(R.id.part_nr);

At the end we need to create a new class for connecting to the server and sending the previous typed info.
This is the class Create_part:

class Create_Part extends AsyncTask {

@Override protected void onPreExecute() {
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Sending part to the database..."); //Set the message for the loading window
pDialog.show(); //Place the loading message on the screen

@Override protected String doInBackground(String... args) {

String String_name = editText.getText().toString(); //Get the inserted text from the editText files
String Int_Part = editText2.getText().toString();

List params = new ArrayList<>();
params.add(new BasicNameValuePair("Name", String_name)); //Add the parameters to an array
params.add(new BasicNameValuePair("part_nr", Int_Part));

// Do the HTTP POST Request with the JSON parameters
// Change "RaspberryPi_IP to your home IP address or Noip service

JSONObject json = jsonParser.makeHttpRequest("RaspberryPi_IP/db_create.php", "POST", params);

try {

int success = json.getInt("success");

if(success == 1){

Intent i = new Intent(getApplicationContext(), ConfirmActivity.class);
// Open a new activity to confirm it was sent. We're going to create it later.


} catch (JSONException e) {



return null;


protected void onPostExecute(String file_url){

pDialog.dismiss(); // Close the loading window when ready



Now you can make a new function called Send. In this function you call Create_Part. Like so:

public void Send(View view) {

new Create_Part().execute();


In the activity_main.xml you now change the onclick action of the button on the right to:

Ok your almost done, now you only need to make a new activity like you see in the image.

Change the activity name to: ConfirmActivity

Click Finish

Now go again to strings.xml and change the "hello_world" string from Hello World! to The part was created.

Now it should be done.

Rebuild the app and test it on your phone!

Step 8: Resume

In this instructables we saw how to make a basic android app and connect it to a php mysql server.

I'm going to make a video for this tutorial, but now I don't have time for it.

If you have questions/remarks please share them and I will see if I can answer them.

Thank you very much for following this instructables.

Laurens Wuyts

P.S. Please be kind, it's my first instructables.

Mind for Design

Participated in the
Mind for Design

5 People Made This Project!


  • Tinkercad to Fusion 360 Challenge

    Tinkercad to Fusion 360 Challenge
  • Stick It Challenge

    Stick It Challenge
  • Trash to Treasure Contest

    Trash to Treasure Contest



1 year ago

Hi, maybe you could revisit this project because a lot of the tools used are deprecated by android. so most of the code isn't working anymore.

For example in "public class MainActivity extends ActionBarActivity {}" the actionBarActivity went deprecated in or before 2015 .
Since the version 22.1.0, the class ActionBarActivity is deprecated. You should use AppCompatActivity.


2 years ago

Hello , Myself ATharva KAryakarte . WHile Putting the files in ww directory of raspberry pi i am getting an error of permission denied what should I do ??


3 years ago

Hello, You ve done a great job. !! Thanks for all the information.
Please i ahave a question. At the creation of the table Parts you write:
column: ID
Type: INT
Auto increment: v

What do you mean by "Auto increment: v" ?
Is v 1 or 2 orf 3?


Tip 3 years ago

Please make a video tutorial thanks


3 years ago on Step 2

I find the images of the software almost impossible to read. Thus this entire post is essentialy useless to me. Please post software to github or someplace where it can be downloaded or read.

Jesscy Tey.
Jesscy Tey.

Question 3 years ago on Step 8

Hello, I m jesscy and doing my fyp (smart home) which I had design interface in android studio and firebase as database. I want to connect raspberry pi with my apps but I failed. Can you tell me how to do it? Thanks in advance


Avinash KumarP
Avinash KumarP

3 years ago

Hi, Can I use this app to send voice commands from Andoid phone to raspberry pi.


Question 3 years ago on Step 8


Do you know what else changed since Android studio didn't use dimens.xml, because since then (i guess) there alse isnt any blank project anymore to choose from.

Is it possible to review this project again?

hope you will.

with kind regads, Joost


Answer 3 years ago


I've allready thought about it and i'm probably going to revisit the project with up to date software somewhere in the coming month or two.



4 years ago

hey laurens ,

I got an error that "Unfortunately stopped" . Also have some errors in logcat and this are images . Please help ..

Screenshot (1).pngScreenshot (2).png

Tip 4 years ago

While adding JSONParser you may get some errors, kindly do this two step:

For me the path was like: C:\Users\vaidu\Instructable2\app\build.gardle

Add this two things to buildgradle file

1)android {
useLibrary 'org.apache.http.legacy'}

2)dependencies{ compile 'com.android.support:appcompat-v7:26.1.0' }

Arc of Imperfection
Arc of Imperfection

4 years ago

Okay... I'm very new to this but I do learn quickly so please excuse my lack of knowledge. I understand that these are the steps to create an android app the communicates with the Raspberry Pi but how does the Raspberry Pi respond? I guess what im really asking here is for a given input how does the R-Pi respond. What does it return? What im trying to do is use the R-Pi to either control an arduino or replace it all together. Either way, i need to understand just how r-pi is responding to input so that i can use said input as a series of switches or change my design to accompany it.


4 years ago

Could anyone help me with step four? I keep getting a 1064 error. This is my code:



4 years ago

according to this tutorial we have to make the pc where we creating
the android ap and the raspberry pi connected to the same network over

is there a way to make the same thing but without connecting the raspberry bi to a network

i mean connecting the app to the raspberry pi directly using it's wifi or something like that .


4 years ago

I wish to realize a broadcaster of sound announcement pillotable by smartphone for a digital space. Compared to the framework of my formation it would be good for me seen that I would like to continue in embedded electronics to realize it by joining the dimension software and hardware I Need your advice and guidance thank you


4 years ago

When I pressed the send button,the app just stopped

Can anyone help me figure it out??

Shivang Trivedi
Shivang Trivedi

4 years ago

(1) Is there any simulator/emulator to test raspberry pi? So we can test without physical device.

(2) Which language is better for raspberry pi Android or Python?


Reply 4 years ago

1)Hope you can setup a Bitwise SSH client and access on your personal laptop and accessing it via wifi other than using a monitor as a display device if you are referring to that issue.


5 years ago

hello sir,

I am trying to make same project as yours. so do I need to install android studio on raspberrypi? or i can install it on my window desktop?