HOW TO SHOW ARDUINO SENSOR DATA ON A WEBPAGE
HOW TO SHOW ARDUINO SENSOR DATA ON A WEBPAGE
0
Project description
ARDUINO ETHERNET SHIELD

BUILDING THE PROJECT
For this project, we are going to display DHT22 temperature and humidity sensor data from an Arduino and an Ethernet shield paired to a web page you can access anywhere in the world. To accomplish that, connect the following components, as shown in Figure 2:
THE CODE
Once you have connected all of the parts, upload this code to the Arduino:#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 99);
EthernetServer server(80);
void setup()
{
Serial.begin (9600);
dht.begin( );
Ethernet.begin(mac, ip);
server.begin( );
}
void loop( )
{
float h = dht.readHumidity( );
float t = dht.readTemperature( );
EthernetClient client = server.available();
if (client)
{
boolean currentLineIsBlank = true;
while (client.connected ( ) )
{
if (client.available ( ) )
{
char character = client.read ( );
Serial.write(character);
if (character == 'n' && currentLineIsBlank)
{
client.println ("HTTP/1.1 200 OK");
client.println ("Content-Type: text/html");
client.println ("Connection: close");
client.println ("Refresh: 5");
client.println ( );
client.println ("<!DOCTYPE HTML>");
client.println ("<html>");
client.print ("<Title>Arduino Ethernet Webserver </Title>");
client.print ("<h1>Arduino Ethernet Shield Webserver </h1>");
client.print ("<h4>Temperature in C: ");
client.print (t);client.print("C");
client.print ("</h4><h4>Humidity: ");
client.print (h);client.print("%");
client.println ("<br />");
client.println ("</html>");
break;
}
if ( character == 'n')
{
currentLineIsBlank = true;
}
else if (character != 'r')
{
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
}
}
CODE EXPLANATION
First, we need to include the required libraries. We will use?SPI.h?and?Ethernet.h?to communicate with the Ethernet shield. Both are already available in the Arduino core library so no need to install. Additionally, we will use?DHT.h?to interface with the sensor. You can download it?here. Alternatively, you can go to Tools > Manage Libraries, and get the DHT library there.

IPAddress ip(192, 168, 1, 99)?and?EthernetServer server(80)?to establish a web server at port 80 with a static IP address of “192.168.1.99”. If you want to use a different IP, just change the values from that variable. Be sure to use an unused IP address or else you’ll encounter networking problems.
In the loop function, we read the data from DHT sensor using?readTemperature()?and?readHumidity(). Then using?print(), we send the HTML commands to the client.
DEMONSTRATION
Visit the static IP address (192.168.1.99) you set in the code in any web browser inside your network. It should show this and should update every 5 seconds as indicated in this line from the code:?client.println ("Refresh: 5").

