Project Description

Arduino-Ethernet-Database

Description

In this project, we will be logging data to database using Arduino Ethernet shield. We will store the data of the DHT22 temperature and humidity sensor in the database and we will then show this on the webpage. For creating the database, we will use the Xampp server and we will then send the data from the Arduino using the Ethernet shield through the internet router. This data will get stored in the webpage and we can access this data anytime we want. We will also create a webpage that will show this data.

 

Hardware Requirements

The components required for logging data to database using Arduino Ethernet shield are as follows

  • Arduino Uno
  • Arduino Ethernet Shield
  • DHT22 Temperature and Humidity sensor
  • Connecting Wires
  • Breadboard

 

Skill Required

The skills you require logging data to database using Arduino Ethernet shield are as follows

  • Arduino IDE
  • HTML
  • PHP
  • MYSQL

 

Getting Started

Step 1          Installing and Setting the Xampp

1. The first step you need to do is to install Xampp. Go to the below link and install it from there.

Xampp

After installing it, run the Xampp as administrator by right clicking on it and selecting run as administrator as shown in below picture.

Run xampp as admin.png

2. After running it, click on config button next to apache and select “Apache (httpd.config)” as shown below.

Xampp port selection.png

3. A notepad file will open, change the port written next to word “listen” from 80 to 8095. The reason of why we need to change the port is that the port 80 is sometimes used by other applications so it will cause error. Therefore, we have selected a separate port to avoid any error.

change port.PNG

4. After that, click on config button in the top right corner as highlighted in the below figure

press config button.PNG

5. While pressing the config button, a window will open; check the mysql and apache boxes as shown below

check boxes.PNG

6. After doing all the above steps, restart you PC and run again the Xampp as administrator. You will see the apache and mysql service started.

apache and mysql started.PNG

Step 2

Creating the Database

Open your browser and type “localhost:8095/phpmyadmin/”. It will ask for a username and password, the default username is “root” and leave the password as blank. After giving the username and password, it will open the phpmyadmin.

Then go to databases tab and create the new database. I have created the database with the name “ethernet” as shown below

create database.PNG

Then go to SQL tab next to databases tab and paste the below code there. It will create a table where we will enter the data of the sensors from the Arduino

 

sql.PNG

Step 3

Creating the PHP pages

Now we are going to create the php pages which will help in making connection between the Arduino and the Database.

1. First go to C:\xampp\htdocs and create a new folder named ‘ethernet’. Create a new text file in this folder and save the name as ‘connection.php’. Paste the below code there and save the file. This PHP file will make a connection between the Arduino and the database.

 

2. Then create a new text file and save the name as ‘data.php’. Paste the below code there and save it. This PHP file will log the data into the database from the Arduino.

 

3. You can manually check that if the database is receiving the data from these pages or not or you can say that if the connection between the database and these pages is made or not. To check that, place the below line in your browser

 

After entering the above line in your browser, if “Done” will be shown, then it means that the values are stored in the database. You can see these by going into the data base.

Step 4

Sending Data from Arduino

Now, we are going to send the data from Arduino to the database.

Make the connections of the DHT22 with the Arduino as shown in the figure below

To read more about interfacing DHT22 with Arduino, read this Tutorial | Temperature and Humidity Sensor DHT22 Interfacing with Arduino

DHT22 with arduino_bb.png

Arduino Code

Download the DHT22 library from the below link. No need to download the Ethernet and SPI library because it comes with the Arduino IDE installation.

DHT22 Library

Now paste the below code in the Arduino IDE

 

Code Explanation

You need to make changes in the following two lines. Make sure that the Ethernet wire is connected to the Ethernet shield.

In the Arduino IDE, go to files-Examples-Ethernet-DHCPAdressPrinter. A new window will open up, upload the code and in the serial printer, it will show you the IP Address. Enter it in “byte ip[] = {192, 168, 1, 18 };”

DHCP IP Adress.PNG

Then open the command prompt (cmd) and type in “ipconfig” and it will show you the IPv4 address. Enter it in “byte serv[] = {192, 168, 1, 5} ; ”

IPv4 Adress.PNG

The remaining code is explained with the comments.

Step 5

Displaying the Data

To display the data, you will need to create another PHP file that will read the data from the database and will show it on the webpage.

Create another PHP file with the name “display.php” and place it in the same folder where you have placed the other PHP files (C:\xampp\htdocs\ethernet\). Place the following code in the display.php and save the file.

 

Now type the following line in the browser, webpage with data will be shown like shown in below figure

 

display.PNG

 

Tags: 201802, W5100, Ethernet, Arduino, Database, MySQL