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 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.

Comments

author
Yulia1994 made it!(author)2017-07-21

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.

author
MohitT27 made it!(author)2017-07-06

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

author
samBytes made it!(author)2017-06-30

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

author
DevanP1 made it!(author)2017-06-12

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

author
syedsoubanw97 made it!(author)2017-01-13

Where is Part-II

author
gammuush made it!(author)2017-04-28

I ALSO NEED THIS ANSWER...PLS SOME ONE HELP!!!

author
Jo%C3%A3o+PedroS12 made it!(author)2017-06-03

OPlease can anyone telll me where part 2 is?

author
Ian+HenryP made it!(author)2017-04-26

Hello is it also possible to use GPS/GPRS shield instead of Ethernet shield ?

author
Vitor+Teixeira made it!(author)2016-11-18

Hello, good project. Its possible to substitute the Ethernet Shield with a Wifi Shield ? There are libraries to use this method ? Thanks.

author
jbarkby made it!(author)2017-04-25

Ethernet, Wifi, Bluetooth, USB.... Its all just a transport protocol. As long as you get the correct library and know the syntax for output they will all work.

author
Syahsyahsyah made it!(author)2016-12-15

Hello Apais, how about adding an LCD 16x2 ? Any suggestions?

author
jbarkby made it!(author)2017-04-25

I was going to recommend using and SPI 16x2 LCD but you will need to share that with the Ethernet Shield. Possible if you make sure you use differenty SS pin per device but not tried that yet. If you use a normal shield, you might have to re arrange the pins in the code to make it all work.

author
oscarg127 made it!(author)2017-04-21

HI

my question is, can I monitor this system with nagios?

author
goutham. made it!(author)2017-04-07

Hi

Can u tell me whether i can send sensor readings via WiFi to a php page which i create.I wanted to use Wemos D1 in which esp8266 wifi module is embedded.I have read the sensor values attached to Arduino uno and displayed it in serial monitor.Now i want to read the values and display it in a webpage and later send http POST requests to trigger an event.

author
BhaiaJ made it!(author)2017-03-29

Hey! I am using SIM900A GSM module instead of ethernet shield. Can you help me with arduino and AT codes?

author
HemantJ made it!(author)2017-03-24

can any one tell me why this error is shown in Arduino IDE?


FSA6ST4HXB3V05E.ino:1:17: fatal error: DHT.h: No such file or directory
compilation terminated.
Error compiling.

author
jocoman made it!(author)2017-03-15

Please tell me where i palce the 3 PHP files. Where do they go?

Thanks

author
VimukthiS1 made it!(author)2016-12-22

Hello,

I'm doing a project with RFID-RC522, for that I need PORTS 5,50,51,52,53,GND,3.3v. Will they be still usable if I use the mentioned Ethernet shield ?

author
GeoR12 made it!(author)2017-03-11

no the will busy when the code run

author
DerekF38 made it!(author)2017-03-11

Hi Guys, link for index.php with bootstrap theme and fontawesome icons

https://drive.google.com/open?id=0B1P39ysLCifoSUtRc0hDZEpwMms

author
Ahnaf+Shariar made it!(author)2017-03-06

Is there any way to download file from any website in SD card using PHP in Arduino ? I need to do it for a project.

author
Pawe%C5%82S43 made it!(author)2017-02-20

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?

author
drouard made it!(author)2017-01-17

hello, it is a nice project! i try to do it with an arduino mega2650 (because i want to join pressure value and speed wind). i take an account for a server with mysql datbase (alwaysdata) and copy and modify with my data ypour files

but at this time nothing works, tell me if something is wrong in your project or what i can do for doing work this project

author
Karanronin made it!(author)2017-01-07

will this be possible if I connect two or more sensors and also and 1602 display. Can u please help me with a code. karanbellak03@gmail.com

author
VimukthiS1 made it!(author)2016-12-30

Following is my code. I'm not using any sensors. Just trying to send the value to the server. PHP codes are ok. But nothing added to the DB. Nothing printed on the Serial port as well.

#include <Ethernet.h>

#include <SPI.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

byte ip[] = { 192,168,1,5}; //The Arduino device IP address

byte subnet[] = { 255,255,255,0};

byte gateway[] = { 192,168,0,1};

IPAddress server(192,168,1,2);

EthernetClient client;

String data;

void setup() {

Serial.begin(9600);

// start the Ethernet connection:

while (!Serial);

if (Ethernet.begin(mac) == 0) {

Serial.println("Failed to configure Ethernet using DHCP");

for (;;)

;

}

Serial.print("My IP address: ");

for (byte thisByte = 0; thisByte < 4; thisByte++) {

Serial.print(Ethernet.localIP()[thisByte], DEC);

Serial.print(".");

}

delay(4000); // GIVE THE SENSOR SOME TIME TO START

data = "";

}

void loop(){

data = "&temp1=10&hum1=20";

if (client.connect(server,1234)) { // REPLACE WITH YOUR SERVER ADDRESS

Serial.println("Server connection OK");

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

client.println("Host:" + server);// 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

}

delay(5000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN

}

author
hananana made it!(author)2016-12-19

hello apais, i'm stuck bout how to get data from arduino to the phpmyadmin database.

author
alebadywallaa made it!(author)2016-10-01

execuse me >>> can help me >>>
i cant connect to my server >>>

please ؟?

author
ashahri90 made it!(author)2016-09-06

Hi Apais,

I liked your project, actually i want do one project like your project, but i want to change to motion sensors, any code or sample project you have and can share with me?

author
apais made it!(author)2016-09-06

hi mate thanks. well the arduino code should be pretty much the same just adapted for the new hardware, but since its a motion sensor you should be reading sensors values more often.

author
JoshuaF87 made it!(author)2016-09-19

hello apais! is it possible to connect rainfall sensor to your project? thanks in advance!!!

author
ParisM2 made it!(author)2015-09-29

hello all,

After a lot of trial and error I got this to working BUT I think you should change the

client.connect if you are using local network.

If you use

client.connect("192.168.1.2" , 80) it will not work

Instead you should define

byte php_server[] = {192 , 168 , 1 , 2};

and later connect like this:

client.connect(php_server ,80)

Hope it helps!!!

So far I set up a simple web server on a linux machine and I got the arduino to report temperature, humidity and activation of a motion sensor. This is one of the best guides I ve found on the net so THANK you very much

next step is using esp8266 on another arduino to send POST requests via wifi

author
asha++ramoliya made it!(author)2016-09-17

hii,Here you use ip address='192.168.1.2'...

how to get this ip address??

author
Muhammad+SaimN made it!(author)2015-10-23

Hi Paris ..i did the same as you done i,e" byte php_server[] = {192 , 168 , 1 , 2};" but it gives me errors so i tried this

byte ip[] = { 192,168,0,103}; //The Arduino device IP address

byte subnet[] = { 255,255,255,0};

byte gateway[] = { 192,168,0,1};

IPAddress server(192,168,0,105);

and then in void loop i wrote

if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS

client.println("POST /data_recieve/add.php?");

client.println( " HTTP/1.1");

client.println(server); // 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

}

there is no compilation error ..i also write a code to serially print the data ..it works well but ..the data weren't sent to the server ip yet ... kindly help !

author
ParisM2 made it!(author)2015-11-04

Hello

do you achieve connection with the Server?

try

if (client.connect(server,80)) {

Serial.println("Server connection OK");

}

and watch out the serial terminal

author
Muhammad+SaimN made it!(author)2015-11-05

I am sucessful in making a connection but still the values shown in the serial monitor doesnt display in my local wamp server's database.. instead of values shown ..it add zeros every time ..here is my complete code ..kindly guide me ...

#include <DHT.h>

#include <Ethernet.h>

#include <SPI.h>

byte mac[] = {

0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

byte ip[] = { 192,168,0,103}; //The Arduino device IP address

byte subnet[] = { 255,255,255,0};

byte gateway[] = { 192,168,0,1};

IPAddress server(192,168,0,106);

EthernetClient client;

#define DHTPIN 2 // SENSOR PIN

#define DHTTYPE DHT11 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS

DHT dht(DHTPIN, DHTTYPE);

long previousMillis = 0;

unsigned long currentMillis = 0;

long interval = 250000; // READING INTERVAL

int t = 0;// TEMPERATURE VAR

int h = 0;// HUMIDITY VAR

String data;

void setup() {

Serial.begin(115200);

// start the Ethernet connection:

while (!Serial) {

; // wait for serial port to connect. Needed for Leonardo only

}

// start the Ethernet connection:

if (Ethernet.begin(mac) == 0) {

Serial.println("Failed to configure Ethernet using DHCP");

// no point in carrying on, so do nothing forevermore:

for (;;)

;

}

// print your local IP address:

Serial.print("My IP address: ");

for (byte thisByte = 0; thisByte < 4; thisByte++) {

// print the value of each byte of the IP address:

Serial.print(Ethernet.localIP()[thisByte], DEC);

Serial.print(".");

}

dht.begin();

delay(4000); // GIVE THE SENSOR SOME TIME TO START

h = (int) dht.readHumidity();

t = (int) dht.readTemperature();

data = "";

}

void loop(){

currentMillis = millis();

if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL

previousMillis = currentMillis;

h = (int) dht.readHumidity();

t = (int) dht.readTemperature();

}

Serial.print("Current humidity = ");

Serial.print(h);

Serial.print("% ");

Serial.print("temperature = ");

Serial.print(t);

Serial.println("C ");

data = "&temp1=" + String(t)+ "&hum1="+ String(h);

if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS

Serial.println("Server connection OK");

client.println("POST /data_recieve/add.php");

client.println( " HTTP/1.1");

client.println(server); // 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

}

delay(5000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN

}

picimp.png
author
%E7%85%9C%E5%87%B1%E7%8E%8B made it!(author)2016-09-06

client.println("POST /data_recieve/add.php");

client.println( " HTTP/1.1");

client.println(server); // SERVER ADDRESS HERE TOO

modify the above code to :

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

client.println(Host:server);// SERVER ADDRESS HERE TOO

then it work!! not add zero everytime

author
Sam1393 made it!(author)2016-09-03

Hey..really liked your project. I am planning on connecting multiple sensors to the arduino like a DHT11, smoke detector and gas detector. Any advice that you want to give?

author
BlueEye.dk made it!(author)2016-09-06

i did connect a MQ-9 Sensor...

and i get readout in serial but not any print out at data.php on my webpage, any one got his script to work???

author
BlueEye.dk made it!(author)2016-09-06

got som wizzard kids to make me a formula for calculating ppm from the MQ-9 Senso (this sensor er calibrated to my arduino and air flow)

put MQ9-SensorSetup begin before void setup

put MQ9-SensorRead Begin in void loop

[CODE]

//MQ9-SensorSetup begin

const float ya = 0.68; // calculated value for CO

const float yb = 0.36; // calculated value for CO

const int xa = 1000; // calculated value for CO

const float xb = 3000; // calculated value for CO

const float Rx0 = 0.89; // this you need to calibrate your self

float sensor_volt;

float RS_gas; // Get value of RS in a GAS

float ratio; // Get ratio RS_GAS/RS_air

float yt = log(yb/ya);

float xt = log(xb/xa);

float a = yt/xt;

float ppm;

//MQ9-SensorSetup end

// MQ9-SensorRead Begin

int sensorValue = analogRead(sensorPin);

sensor_volt=(float)sensorValue/1024*5.0;

RS_gas = (5.0-sensor_volt)/sensor_volt; // omit *RL

ratio = RS_gas/Rx0; // ratio = RS/R0

float y = ratio;

float b = ya/(pow(xa,a));

float ppm = pow(y/b,(1/a));

// MQ9-SensorRead End

[/CODE]

author
rbright made it!(author)2016-07-26

Apais I hope we all find you in good health..... Will there ever be a 'Part 2' ???

On behalf of your fans, thanks in anticipation

author
apais made it!(author)2016-08-27

i mate. thanks it's all good. i don't think i will make a part 2.... although, i will share here my git hub repository with the full application code, with the D3.js part.... https://github.com/alexpais/iot-arduino-weather-station

author
stupscarlet07 made it!(author)2016-08-15

hei, thank yuo this is nic..
but i wonder, if you can reverse the condition...
i mean the arduino receiving data from web (php/mysql) because i am doing some kind of project and require a command from web.
i am using a wemos D1 R2 actually and not using ethernet but using WiFi,
do you know how? i am kinda really beginner at ths stuff
thank you .

author
apais made it!(author)2016-08-27

Hi nic. If you want to reverse the thing, i think you need to create a server with the arduino.... to revert the function.... the arduino server will be accepting connections and reading the data that those connect clients send...

author
shimonbd made it!(author)2016-06-16

Hey, I connected the sensor as you did, but the result is always 0 even temperature and humidity
what could be the problem

author
Tayyab+Tahir made it!(author)2016-06-07

Good Evening ..
I am trying to make a fuel monitoring system for generators .. I have already made the code to read sensor value but couldn't figure out the database part can please someone help me out ,want to make 3 columns
1 level
2 Power
3 Efficiency

Please help me by modifying the codes . really grateful.
Thanks

author
ChaniduM made it!(author)2016-06-02

Hi.... I'm new to arduino programming....but i want to make student attendance system using arduino.. I also need to make an database using MySQl or some other software to store students attendance records..plz help me to do that...specially making a database and connet with arduino...Plz help mee as soon as possible....

author
nagavikas made it!(author)2016-06-06

hey hii,

I am also doing attendance management system,,, can u share u r project details with me i ll help u for u r project and also i was stuck at one place that will require u r help,...

author
RohitD13 made it!(author)2016-06-06

I am using this Arduino for the first time, i need some urgent help.

This below line never works .

if(Ethernet.begin(mac)==0)

I tried with all options but never works. and i think due to that the below line also doesnt work

if(client.connect(server, 80)) {

author
achaniago made it!(author)2016-06-03

thanks a lot for your information. it worked. yea

author
shimonbd made it!(author)2016-04-03

Hello
I'm trying to measure the voltage at the point A 0 Analog and send the given website.
I tried to use some of your sketch but I could not connect to the server which I am trying to send the figure.
Would appreciate help .
Thanks Shimon