PART 1 - Send Arduino Data to the Web ( PHP/ MySQL/ D3.js )

418,294

254

163

Intro: PART 1 - Send Arduino Data to the Web ( PHP/ MySQL/ D3.js )

The objective of this project was to use and Arduino to read a sensor and send the values to the internet, to be stored in a Web Server and displayed.

It consists in an Arduino Uno with an Ethernet Shield and a DHT 11 temperature / moisture sensor, acting as a Web Client. It sends POST requests with the readings to a web server running a custom Database and PHP application.

The PHP app stores the values when new POST requests are received and also serves the pages that display the information. In Part 2, i will explain the use of D3.js to dynamically show the data stored in the Database.

The Arduino it's configured to use a Dynamic IP Address, in order to solve any conflicting IP issues, and also to work easily with most home networks/routers.

This project is divided in 2 main parts:

PART 1
- Arduino Web client Application: reads the sensor values and sends them to the webserver.
- PHP/MySQL Application: handles the POST requests that are sent to the server and serves the pages to clients who connect 
PART 2
- Data Visualization: The PHP application will use the Javascript Framework D3.js to display the values stored in the DB with graphics. It will allow to navigate to the past days to observe the readings
REQUIREMENTS


HARDWARE

  1. Arduino Uno
  2. Ethernet Shield (eBay clone)
  3. DHT 11 sensor
  4. breadboard
  5. 10k Ohm resistor
  6. USB cable
  7. Ethernet cable
  8. wires
  9. piece of acrylic
  10. PCB spacers

Software

- You need access to a web server ( can be from a free hosting company ) with capability to run PHP applications and also to create databases. ( possibly cPanel with phpMyAdmin)


RESOURCES

Request Maker: This online tool is very useful to test the PHP application. You can simulate the POST requests that will be made by the Arduino and check if everything is working well.

DHT11 sensor library from Adafruit

Arduino Web Client

Arduino IDE

Arduino Ethernet

Step 1: Arduino Web Client + DHT11 Sensor

The code is very simple, all the important section are commented. If you have any doubt feel free to ask.

Step 2: PHP / MySQL Application

In this second part i will explain briefly the PHP application and the database. The database is used obviously to store the sensor readings, so that they can be accessed later. It's a very simple DB, with just one table with 3 columns. It stores the time stamp and the corresponding temperature and humidity values.

	CREATE TABLE tempLog (
		timeStamp TIMESTAMP NOT NULL PRIMARY KEY,
		temperature int(11) NOT NULL,
		humidity int(11) NOT NULL,
	);

The PHP application consists of 3 files:

- connect.php: this file is loaded every time we need to access to the database. It's loaded in the beginning of the almost each file. It contains a function that returns a new connection to be used by the PHP to execute query's to the DB. You need to store the DB configs (hostname, database, user, password) in this file.

- add.php: when the Arduino sends POST requests to the server, is to to this page. The PHP receives the values sent in the request and executes an insertion query with those values.

Sometimes you need to change the permissions of this file (should be 644), because it might be protected to allow only executions from the localhost.

- index.php: this is the website landing page. It displays the values that are stored in the database. Right now, it will display all the values in a single HTML table, just to show that works.

So, this concludes the first part of this Instructable. Feel free to ask questions about anything related, i'm glad to help.

3 People Made This Project!

Recommendations

  • Plastics Contest

    Plastics Contest
  • Optics Contest

    Optics Contest
  • Halloween Contest 2018

    Halloween Contest 2018

163 Discussions

0
None
PawełS43

1 year ago

hi, nice work, on local network is work fine, but when i try to connect with domain, it;s didnt work. Someone can help me?

1 reply
0
None
dave markcPawełS43

Reply 11 days ago

Sir please teach me how did you do it in local netqork thankyou. Your reply is a releif

0
None
madhud6

Question 6 months ago on Step 1

Respected sir, Good morning sir, i am madhu from Madanapalle. Sir, I want know about how we will connect IoT things to website. can you please expalin it sir.

0
None
banjing

Question 8 months ago

Hi, tried this tutorial of yours. But its not working, data wont feed into my SQL database. I used a server, saved my php script to that server to fetch the data from arduino and send to database. But seems to be not working :( Please help me. Thanks

0
None
PrernaJ2

Question 8 months ago on Step 1

Sir , i want to connect my gsm module to apache server! Is it possible ?

0
None
SamadK7

Question 9 months ago

I have to control an Home automation system through Web so would php be enough for backend and how would i connect the server through IP address if i am able to send ardino data over wifi module

0
None
JeevanT

Question 9 months ago

How to put the ph files in my subdomain? I tried but showing server not found

0
None
subhanil

Question 9 months ago

In the add.php section,you said that sometimes we have to change the permission of this file. What do you mean by that?

0
None
subhanil

10 months ago

from where will the ethernet shield get internet?

0
None
ArminSt

11 months ago

Hello

First thanks for this project, it is exactly what I wanted to start Arduino-WEB experiences.

Everthing is fine for measuring on arduino, connecting with ethernet shield, and also connection to my WEBside returns = 1 so it connects correctly. Database is also created.

But The add.PHP dosent work ( no data apears in the table of database).

Who can I debug for finding solution ? Who can I know if it is a problem to not access to PHP file / not connecting to database / SQL execution ?

Your help would greatly be apreceated !!

I copy only the code of the part that doesnt work :

(I modified the sql to write fix values to test, but still does not work)

Arduino

Conect
= client.connect("www.steinhilber.eu", 80);

if (Conect) {

client.println("POST /add.php HTTP/1.1");

client.println("Host: www.steinhilber.eu"); // SERVER ADDRESS HERE TOO

client.println("Content-Type:
application/x-www-form-urlencoded");

client.print("Content-Length: ");

client.println(data.length());

client.println();

client.print(data);

}

if (client.connected()) {

client.stop(); //
DISCONNECT FROM THE SERVER

}





PHP
connect




<?php

function
Connection(){

$server="steinhiltearmin.mysql.db";

$user="steinhiltearmin";

$pass="*******";

$db="steinhiltearmin";


$connection = mysql_connect($server, $user,
$pass);

if (!$connection) {


die('MySQL ERROR: '
. mysql_error());

}

mysql_select_db($db)
or die( 'MySQL ERROR: '. mysql_error() );

return $connection;

}

?>

PHP add



<?php

include("connect.php");$link=Connection();

$temp1=$_POST["temp1"];

$hum1=$_POST["hum1"];

$query =
"INSERT INTO `tempLog` (`temperature`, `humidity`) VALUES
(25,35)";

mysql_query($query,$link);

mysql_close($link);

header("Location:
index.php");

?>

0
None
MohdZ39

1 year ago

what version of xampp need to use ?

0
None
aahmed60

1 year ago

enthernet shield is unable to send data on WAN i mean hosting companies are unisg ssl and ethernet shield unable to handle it please help me i want to send data on my website on WAN ....

0
None
rawest

1 year ago

I'm having trouble compiling for the wemos d1. The error I''m getting has to do with the concatination in the data = line. Can you not concat in the Arduino IDE?

0
None
KamaldeepS5

1 year ago

Thank you for the detailed instructions.

Just curious if there is a quick way to have event based data logging and still have time stamp in database.

0
None
Yulia1994

1 year ago

In file included from C:\Users\YPK\Documents\Arduino\libraries\DHT-sensor-library-master\DHT_U.cpp:22:0:

C:\Users\YPK\Documents\Arduino\libraries\DHT-sensor-library-master\DHT_U.h:25:29: fatal error: Adafruit_Sensor.h: No such file or directory

#include <Adafruit_Sensor.h>

^

compilation terminated.

exit status 1

Ошибка компиляции для платы Arduino/Genuino Uno.

1 reply
0
None
rbanuajiYulia1994

Reply 1 year ago

try to use older version of DHT library, it works for me

0
None
MohitT27

1 year ago

how to connect Arduino + mysql + android, means i need to access that mysql data from android it self

0
None
samBytes

1 year ago

Thank you for the instructable..can we add a relay to the arduino and control it by server...

0
None
DevanP1

1 year ago

How about instead of monitoring temp we monitor voltage? Say, from a solar panel battery bank!