Introduction: 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 connectPART 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 readingsREQUIREMENTS
HARDWARE
- Arduino Uno
- Ethernet Shield (eBay clone)
- DHT 11 sensor
- breadboard
- 10k Ohm resistor
- USB cable
- Ethernet cable
- wires
- piece of acrylic
- 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.
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.
Attachments
5 People Made This Project!
- VivieanA made it!
- ScriptKKiddie made it!
- KumaraswamyS made it!
- sspence made it!
See 1 More
169 Comments
2 years ago
Great project - I've only just started - However, when creating the tables the script may throw up an error on the last comma. If it does, remove the last comma - see below:
CREATE TABLE tempLog (
temperature int(11) NOT NULL,
humidity int(11) NOT NULL, <- remove this comma :-)
Thanks for all the effort you put into this.
Huggie Duggie
Question 2 years ago
Where is part 2?
- Data Visualization: The PHP applicatio...
Question 3 years ago on Introduction
This is a great project to save a live stream of data to a database via Ethernet. I would like to do this same thing except, do it wirelessly with an ESP8266-01S. Could anyone point me in the right direction?
Question 3 years ago on Step 2
How to get readings sent by gsm module to another gsm and import readings to mysql
Question 3 years ago
Can I add data for voltage sensor and current sensor to measure data for solar panel through arduino and php? Please help me out on this, thanks in advance.
Question 4 years ago
Hi,I am making a project on automated street light using arduino and want to connect it to the web to store data about the switching of lights and display in web..Will be happy if you help me out in this project.
6 years 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?
Reply 4 years ago
Sir please teach me how did you do it in local netqork thankyou. Your reply is a releif
Question 4 years 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.
Question 5 years 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
Question 5 years ago on Step 1
Sir , i want to connect my gsm module to apache server! Is it possible ?
Question 5 years 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
Question 5 years ago
How to put the ph files in my subdomain? I tried but showing server not found
Question 5 years 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?
5 years ago
from where will the ethernet shield get internet?
5 years 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");
?>
5 years ago
what version of xampp need to use ?
5 years 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 ....
5 years 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?
5 years 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.