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
- PHPMyadmin (Not needed but recommended)
Step 4: Creating the database using phpmyadmin
First open a web-browser and go to:
Login to phpmyadmin
Create new database: Parts
Open the database Parts.
Create new table Parts with 3 fields.
Auto increment: v
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:
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:
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.
For more detail: Raspberry Pi Android App communication
Current Project / Post can also be found using:
- raspberry pi android communication
- android studio raspberry pi mysql server
- raspberry pi mysql android app
- raspberry pi mysql database android app