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:

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


  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


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


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.

		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.

Where is Part-II
<p>Hello is it also possible to use GPS/GPRS shield instead of Ethernet shield ? </p>
<p>Hello, good project. Its possible to substitute the Ethernet Shield with a Wifi Shield ? There are libraries to use this method ? Thanks. </p>
<p>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.</p>
Hello Apais, how about adding an LCD 16x2 ? Any suggestions?
<p>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.</p>
<p>HI</p><p> my question is, can I monitor this system with nagios?</p>
<p>Hi</p><p>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.</p>
<p>Hey! I am using SIM900A GSM module instead of ethernet shield. Can you help me with arduino and AT codes?</p>
<p>can any one tell me why this error is shown in Arduino IDE?</p><p><br>FSA6ST4HXB3V05E.ino:1:17: fatal error: DHT.h: No such file or directory<br>compilation terminated.<br>Error compiling.</p>
<p>Please tell me where i palce the 3 PHP files. Where do they go?</p><p>Thanks</p>
<p>Hello,</p><p>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 ?</p>
<p>no the will busy when the code run</p>
<p>Hi Guys, link for index.php with bootstrap theme and fontawesome icons</p><p>https://drive.google.com/open?id=0B1P39ysLCifoSUtRc0hDZEpwMms</p>
<p>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. </p>
<p>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?</p>
<p>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</p><p>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</p>
<p>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</p>
<p>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.<br><br>#include &lt;Ethernet.h&gt;</p><p>#include &lt;SPI.h&gt;</p><p>byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };</p><p>byte ip[] = { 192,168,1,5}; //The Arduino device IP address</p><p>byte subnet[] = { 255,255,255,0};</p><p>byte gateway[] = { 192,168,0,1};</p><p>IPAddress server(192,168,1,2);</p><p>EthernetClient client;</p><p>String data;</p><p>void setup() {</p><p> Serial.begin(9600);</p><p> // start the Ethernet connection:</p><p> while (!Serial);</p><p> if (Ethernet.begin(mac) == 0) {</p><p> Serial.println(&quot;Failed to configure Ethernet using DHCP&quot;);</p><p> for (;;)</p><p> ;</p><p> }</p><p> Serial.print(&quot;My IP address: &quot;);</p><p> for (byte thisByte = 0; thisByte &lt; 4; thisByte++) {</p><p> Serial.print(Ethernet.localIP()[thisByte], DEC);</p><p> Serial.print(&quot;.&quot;);</p><p> }</p><p> delay(4000); // GIVE THE SENSOR SOME TIME TO START</p><p> data = &quot;&quot;;</p><p>}</p><p>void loop(){</p><p> data = &quot;&amp;temp1=10&amp;hum1=20&quot;;</p><p> if (client.connect(server,1234)) { // REPLACE WITH YOUR SERVER ADDRESS</p><p> Serial.println(&quot;Server connection OK&quot;);</p><p> client.println(&quot;POST /add.php HTTP/1.1&quot;);</p><p> client.println(&quot;Host:&quot; + server);// SERVER ADDRESS HERE TOO</p><p> client.println(&quot;Content-Type: application/x-www-form-urlencoded&quot;);</p><p> client.print(&quot;Content-Length: &quot;);</p><p> client.println(data.length());</p><p> client.println();</p><p> client.print(data);</p><p> }</p><p> if (client.connected()) {</p><p> client.stop();// DISCONNECT FROM THE SERVER</p><p> }</p><p> delay(5000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN</p><p>}</p>
<p>hello apais, i'm stuck bout how to get data from arduino to the phpmyadmin database. </p>
execuse me &gt;&gt;&gt; can help me &gt;&gt;&gt;<br>i cant connect to my server &gt;&gt;&gt;<br><br>please ؟?
<p>Hi Apais,</p><p>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?</p>
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.
<p>hello apais! is it possible to connect rainfall sensor to your project? thanks in advance!!!</p>
<p>hello all,</p><p>After a lot of trial and error I got this to working BUT I think you should change the</p><p>client.connect if you are using local network.</p><p>If you use </p><p>client.connect(&quot;; , 80) it will not work</p><p>Instead you should define </p><p>byte php_server[] = {192 , 168 , 1 , 2};</p><p>and later connect like this:</p><p>client.connect(php_server ,80)</p><p>Hope it helps!!!</p><p>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</p><p>next step is using esp8266 on another arduino to send POST requests via wifi</p>
<p>hii,Here you use ip address=''...</p><p>how to get this ip address??</p>
<p>Hi Paris ..i did the same as you done i,e&quot; byte php_server[] = {192 , 168 , 1 , 2};&quot; but it gives me errors so i tried this </p><p> byte ip[] = { 192,168,0,103}; //The Arduino device IP address</p><p>byte subnet[] = { 255,255,255,0};</p><p>byte gateway[] = { 192,168,0,1};</p><p>IPAddress server(192,168,0,105);</p><p>and then in void loop i wrote</p><p>if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS</p><p>client.println(&quot;POST /data_recieve/add.php?&quot;); </p><p> client.println( &quot; HTTP/1.1&quot;);</p><p>client.println(server); // SERVER ADDRESS HERE TOO</p><p>client.println(&quot;Content-Type: application/x-www-form-urlencoded&quot;); </p><p>client.print(&quot;Content-Length: &quot;); </p><p>client.println(data.length()); </p><p>client.println(); </p><p>client.print(data); </p><p>} </p><p>if (client.connected()) { </p><p>client.stop();// DISCONNECT FROM THE SERVER</p><p>}</p><p>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 !</p>
<p>Hello</p><p>do you achieve connection with the Server?</p><p>try </p><p>if (client.connect(server,80)) {</p><p>Serial.println(&quot;Server connection OK&quot;);</p><p>} </p><p>and watch out the serial terminal</p>
<p>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 ...</p><p>#include &lt;DHT.h&gt;</p><p>#include &lt;Ethernet.h&gt;</p><p>#include &lt;SPI.h&gt;</p><p>byte mac[] = { </p><p> 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };</p><p> byte ip[] = { 192,168,0,103}; //The Arduino device IP address</p><p>byte subnet[] = { 255,255,255,0};</p><p>byte gateway[] = { 192,168,0,1};</p><p>IPAddress server(192,168,0,106);</p><p>EthernetClient client;</p><p>#define DHTPIN 2 // SENSOR PIN</p><p>#define DHTTYPE DHT11 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS</p><p>DHT dht(DHTPIN, DHTTYPE);</p><p>long previousMillis = 0;</p><p>unsigned long currentMillis = 0;</p><p>long interval = 250000; // READING INTERVAL</p><p>int t = 0;// TEMPERATURE VAR</p><p>int h = 0;// HUMIDITY VAR</p><p>String data;</p><p>void setup() { </p><p>Serial.begin(115200);</p><p> // start the Ethernet connection:</p><p> while (!Serial) {</p><p> ; // wait for serial port to connect. Needed for Leonardo only</p><p> }</p><p> // start the Ethernet connection:</p><p> if (Ethernet.begin(mac) == 0) {</p><p> Serial.println(&quot;Failed to configure Ethernet using DHCP&quot;);</p><p> // no point in carrying on, so do nothing forevermore:</p><p> for (;;)</p><p> ;</p><p> }</p><p> // print your local IP address:</p><p> Serial.print(&quot;My IP address: &quot;);</p><p> for (byte thisByte = 0; thisByte &lt; 4; thisByte++) {</p><p> // print the value of each byte of the IP address:</p><p> Serial.print(Ethernet.localIP()[thisByte], DEC);</p><p> Serial.print(&quot;.&quot;);</p><p> }</p><p>dht.begin(); </p><p>delay(4000); // GIVE THE SENSOR SOME TIME TO START</p><p>h = (int) dht.readHumidity(); </p><p>t = (int) dht.readTemperature(); </p><p>data = &quot;&quot;;</p><p>}</p><p>void loop(){</p><p>currentMillis = millis();</p><p>if(currentMillis - previousMillis &gt; interval) { // READ ONLY ONCE PER INTERVAL</p><p>previousMillis = currentMillis;</p><p>h = (int) dht.readHumidity();</p><p>t = (int) dht.readTemperature();</p><p>}</p><p> Serial.print(&quot;Current humidity = &quot;);</p><p> Serial.print(h);</p><p> Serial.print(&quot;% &quot;);</p><p> Serial.print(&quot;temperature = &quot;);</p><p> Serial.print(t); </p><p> Serial.println(&quot;C &quot;);</p><p>data = &quot;&amp;temp1=&quot; + String(t)+ &quot;&amp;hum1=&quot;+ String(h);</p><p>if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS</p><p> Serial.println(&quot;Server connection OK&quot;);</p><p> client.println(&quot;POST /data_recieve/add.php&quot;); </p><p> client.println( &quot; HTTP/1.1&quot;);</p><p>client.println(server); // SERVER ADDRESS HERE TOO</p><p>client.println(&quot;Content-Type: application/x-www-form-urlencoded&quot;); </p><p>client.print(&quot;Content-Length: &quot;); </p><p>client.println(data.length()); </p><p>client.println(); </p><p>client.print(data); </p><p>} </p><p>if (client.connected()) { </p><p>client.stop();// DISCONNECT FROM THE SERVER</p><p>}</p><p>delay(5000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN</p><p>}</p>
<p>client.println(&quot;POST /data_recieve/add.php&quot;);</p><p>client.println( &quot; HTTP/1.1&quot;);</p><p>client.println(server); // SERVER ADDRESS HERE TOO<br><br>modify the above code to :</p><p>client.println(&quot;POST/data_recieve/add.php HTTP/1.1&quot;);</p><p>client.println(<strong>Host</strong>:server);// SERVER ADDRESS HERE TOO<br><br>then it work!! not add zero everytime</p>
<p>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?</p>
<p>i did connect a MQ-9 Sensor...</p><p>and i get readout in serial but not any print out at data.php on my webpage, any one got his script to work??? </p>
<p>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) </p><p>put MQ9-SensorSetup begin before void setup </p><p>put MQ9-SensorRead Begin in void loop</p><p> [CODE]</p><p>//MQ9-SensorSetup begin</p><p> const float ya = 0.68; // calculated value for CO</p><p> const float yb = 0.36; // calculated value for CO</p><p> const int xa = 1000; // calculated value for CO</p><p> const float xb = 3000; // calculated value for CO</p><p> const float Rx0 = 0.89; // this you need to calibrate your self </p><p> float sensor_volt;</p><p> float RS_gas; // Get value of RS in a GAS</p><p> float ratio; // Get ratio RS_GAS/RS_air</p><p> float yt = log(yb/ya);</p><p> float xt = log(xb/xa);</p><p> float a = yt/xt;</p><p> float ppm;</p><p>//MQ9-SensorSetup end</p><p>// MQ9-SensorRead Begin</p><p> int sensorValue = analogRead(sensorPin);</p><p> sensor_volt=(float)sensorValue/1024*5.0;</p><p> RS_gas = (5.0-sensor_volt)/sensor_volt; // omit *RL</p><p> ratio = RS_gas/Rx0; // ratio = RS/R0 </p><p> float y = ratio;</p><p> float b = ya/(pow(xa,a));</p><p> float ppm = pow(y/b,(1/a));</p><p> // MQ9-SensorRead End </p><p>[/CODE]</p>
<p>Apais I hope we all find you in good health..... Will there ever be a 'Part 2' ???</p><p>On behalf of your fans, thanks in anticipation </p>
<p>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</p>
<p>hei, thank yuo this is nic..<br>but i wonder, if you can reverse the condition...<br>i mean the arduino receiving data from web (php/mysql) because i am doing some kind of project and require a command from web.<br>i am using a wemos D1 R2 actually and not using ethernet but using WiFi,<br>do you know how? i am kinda really beginner at ths stuff<br>thank you .</p>
<p>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...</p>
<p>Hey, I connected the sensor as you did, but the result is always 0 even temperature and humidity<br>what could be the problem</p>
Good Evening ..<br>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 <br>1 level<br>2 Power<br>3 Efficiency<br><br>Please help me by modifying the codes . really grateful.<br>Thanks<br><br>
<p>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....</p>
<p>hey hii,</p><p>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,...</p>
<p>I am using this Arduino for the first time, i need some urgent help.</p><p>This below line never works .</p><p> if(Ethernet.begin(mac)==0)</p><p>I tried with all options but never works. and i think due to that the below line also doesnt work</p><p>if(client.connect(server, 80)) {</p>
<p>thanks a lot for your information. it worked. yea</p>
<p>Hello<br>I'm trying to measure the voltage at the point A 0 Analog and send the given website.<br>I tried to use some of your sketch but I could not connect to the server which I am trying to send the figure.<br>Would appreciate help .<br>Thanks Shimon</p>
<p>its really easy. Follow this tutorial here <a href="https://www.instructables.com/id/Arduino-Based-Solar-PV-Energy-Meter-With-Xively-Co/">https://www.instructables.com/id/Arduino-Based-Sola...</a></p><p>its basically for sensing current and voltages and upload it to a website for graph :)</p>
<p>I have encountered this problem. How do I resolve it?</p><p>Arduino: 1.6.7 (Windows 10), Board: &quot;Arduino/Genuino Uno&quot;<br><br>C:\Users\***\Desktop\DHT\client\client.ino: In function 'void loop()':<br><br>client:45: error: invalid operands of types 'const char*' and 'const char [7]' to binary 'operator+'<br><br> data = &quot;temp1=&quot; + t + &quot;&amp;hum1=&quot; + h;<br><br> ^<br><br>exit status 1<br>invalid operands of types 'const char*' and 'const char [7]' to binary 'operator+'<br></p>
<p>Try: </p><p>data = &quot;temp1=&quot; + String(t) + &quot;&amp;hum1=&quot; + String(h);</p>
<p>It should read: </p><p>data = &quot;temp1=&quot; + String(t) + &quot;&amp;hum1=&quot; + String(h);</p>
<p>I wanted to post the value of ultrasonic sensors on my web server but I do not know wha I had to do because there are more references using a temperature sensor, please give me a solution please .... admin, thanks verymuch</p>

About This Instructable



More by apais:PART 1 - Send Arduino data to the Web ( PHP/ MySQL/ D3.js ) 
Add instructable to: