This step-by-step tutorial is made to
help you to make your own database on a Raspberry Pi.
Our work was to create a web page for the company Materia Nova to encode data on samples of their products. Once encoded, the data will be stored in a database. Each encoded product will save in the database with a QR code specific to the product. This QR code allows you to go back to the data related to the product from a tablet or a smartphone.
You will find through this instructable and video, how we made it work. We begin by quoting the equipment used, the price of the components, we will then present a schematic of principle and the project stages.
To follow this tutorial, you need some equipment.
Step 1: Required Equipment
· Raspberry Pi (± 50€)
· MicroSD card 8Go (± 7€)
· Router (± 100€)
· Smartphone with QR Code reader app (Everyone has one – Free app)
· Computer mouse (to program) (± 10€)
· Screen (to program) (20€ - 100€)
· Keyboard (to program) (20€ - 100€)
· All your brain capacity (optional) (no price)
Step 2: Video of Our Project.
In this video, you can find :
- Hardware & Connections
- How to use? It is easy !
Step 3: Preparation of the Raspberry Pi
For this tutorial, we work on the second model of the Raspberry Pi. The model B v1.1.
We recommend you to get the last version to be sure to have the best performance with your hardware.
To use your Raspberry Pi, you need a MicroSD card. To give you the best performance, you need to take at least a Class 10 (write speed class) with 8 GB of capacity.
To do this step-by-step tutorial, you also need a router with Wireless Access Point and an Internet Connection to download Raspbian and to install additional packages (not required after the installation). You connect the Raspberry thanks a RJ45 cable.
SECOND STEP: Raspbian OS
Raspbian OS is a free Linux distribution optimised for the Raspberry Pi.
You can download it here : https://www.raspberrypi.org/downloads/raspbian/
We recommend you to download the last version and the full desktop version.
After the downloading, you need to install Win32DiskImager (https://sourceforge.net/projects/win32diskimager/ )
The manipulation is different on Linux or MacOS follow this link if you use one of these OS : https://www.raspberrypi.org/documentation/install... .
On Windows, unzip the Raspbian zip and run as Administrator Win32DiskImager with your MicroSD in it SD adapter on your computer.
Browse to your Raspbian image, select the right letter into Device and click on Write.
Warning: if you do not select the right Device, you can corrupt your computer.
For the next step, you can plug your MicroSD card on your Raspberry Pi and connect all the devices (keyboard, mouse, screen, Ethernet connection,etc.). The Raspberry Pi turns ON automatically when you plug the power supply.
Follow the schematic drawing to connect correctly all devices to the Raspberry Pi.
Warning: Turn ON the Raspberry Pi ONLY after connecting all devices. If you plug a device after ignition, it may not be recognised.
For the first launch, we recommend you to use this command in the terminal:
- sudo apt-get update
- sudo apt-get upgrade
- sudo raspi-config and then select expand_rootfs. Reboot the raspberry after all is done. This command allows you to use all the capacity of your MicroSD card.
For your information the default account is:
- username : pi
- password : raspberry
THIRD STEP: Packages to install for the DataBase
For the next steps, this tutorial is based on a French tutorial from Raspbian France.
The translation of the title is “Installing a web server on your raspberry”.
- update and upgrade.
- sudo aptitude update
- sudo aptitude upgrade
- Installing Apache Server : this is our web server
- sudo aptitude install apache2
- sudo chown -R www-data:pi /var/www/html/
- sudo chmod -R 770 /var/www/html/
You can verify Apache works fine. To do this, you can connect on the localhost address on the Raspberry Pi (127.0.0.1).
All your web pages will be placed into this repertory: /var/www/html/
- Installing PHP : PHP is an interpreted language. So we needed to install an interpreter to interpret the language.
- sudo aptitude install php5
- Installing MySQL : MySQL is a database management system.
- sudo aptitude install mysql-server php5-mysql
Be careful when you chose your password.
- Installing PHPMyAdmin : using to manage your database
- sudo aptitude install phpmyadmin
Here is the link to access to PHPMyAdmin : http://127.0.0.1/phpmyadmin
FOURTH STEP: QRcode package
You can download this package here: https://sourceforge.net/projects/phpqrcode/files/
After unzip this, you can move the content into /var/www/html/phpqrcode
Step 4: The Database
Create a DB with PHPMyAdmin.
Here is the web page where we arrive when we connect to PHPMyAdmin. (First picture)
We find all the databases that were created to the left of the screen. To create a new database, simply click on "New Database" and give it a name.
In each new database, we need to create tables. Those tables will contain all desired data through "fields". This requires when a new table is created, enter the field names and the type of data they may contain.
Each field created it will require a name, type, etc. It will be the names to be used in the program which will manage the registration data of the GUI to the database created here.
- Name: Set the field name
- Type: the type of data that will store the field (integer, text, date ...);
- Size / Value: Specifies the maximum size of the field
- AUTO_INCREMENT: allows the field to increase itself with each new entry. It is frequently used on fields of type id.
Warning: It is very important to choose the names because the judicious choice of these will facilitate your task programming afterwards. That is to say, keep a logical structure for naming data to avoid errors later such as: Missed capital, hyphens instead of an underscore. ...
Here for example the structure of our electric table : second picture.
Step 5: PHP and HTML Codes
To make the Raspberry Pi works correctly, you need to create some files which contain the code lines in PHP and/or HTML language. Why use two different languages? Some operations are easier to write in HTML and other in PHP language. So we use one or the other according to what should be done. The ease of changing from one to the other is an advantage with this kind of language.
To create those files, you may use a program like WordPad or Bloc-notes and write all the code lines as if you were in a compiler. You can see all the code lines writing in each file annexed at the end of this instructable.
All the files can be downloaded in this step.
Here are the files to create:
- File name: “Accueil”
This file takes you to the home page. This page contains the links to the different samples that you can encode. It also contains the “Print” button that allows you to print the QR Code.
- File name: “BHT”
It takes you to the page when you can encode all the characteristics of a BHT sample. It also contains the “Save” button that allows you to save the sample characteristics into the database.
- File name: “DC”
It takes you to the page when you can encode all the characteristics of a DC sample. It also contains the “Save” button.
- File name: “DC_Pulse”
It takes you to the page when you can encode all the characteristics of a DC_Pulse sample. It also contains the “Save” button.
- File name: “HiPIMS”
It takes you to the page when you can encode all the characteristics of a HiPIMS sample. It also contains the “Save” button.
- File name: “Save”
With this file, you save all the characteristics you have encoded, into the data base. The file work when the user click on the “save” button. This file also contains the lines which generate the QR Code.
- File name: “Echantillon”
This file allows to display the characteristics of the sample you read the QR code.
- File name: “Print”
When the user click on the “Print” button, this file shows a page with only the QR Code. Here he can print the picture.
Notice : 192.168.31.244 is the personal IP Address of our Raspberry Pi. Do not forget to modify the lines code to adapt it to your configuration.
Step 6: How to Use It ?
- Connect a computer to the network of the router.
- Open a web browser (Firefox, Chrome, Internet Explorer, etc.).
- Enter the link of the home page (192.168.31.244/Accueil.php). It is the IP address we have used. It will be different.
- In the home page, select the kind of sample you need to encode. (HiPIMS, BHT, DC, DC_Pulse).
- Here, enter all the characteristics of the sample.
- Click on the “Valider” button.
- You go back to the home page with a QR Code displayed. Click on the “Page d’impression” button.
- On the QR Code page you can print the picture of the code.
- Past the QR Code to the physical sample.
- Take your smartphone or tablet and run the QR Code reader.
- Scan the QR Code you pasted.
- Click on the link that your app offers you.
- Admire the page you created. All the sample characteristics are there.
Step 7: If You Want to Know More About the Progress of Our Project
If you want to know more about the progress of our project, you can follow this link :