Skip to main content

Making Ethernet Web Server with Arduino

 By Ali Hamza  ArduinoEmbeddedProjects  ArduinoArduino UnoBread BoardDHT22Ethernet ShieldServerWebWebServer  

Contents

·         1 Components Required

·         2 Hardware

o    2.1 Circuit Diagram

o    2.2 Ethernet shield

o    2.3 DHT22

§  2.3.1 Specifications

·         3 Software

o    3.1 Arduino Uno Program

o    3.2 Code Explanation

o    3.3 Ethernet configuration

§   

§  3.3.0.1 IP ADDRESS

§  3.3.0.2 MAC ADDRESS

·         4 Practical Implementation of  Circuit

·         5 Working

·         6 Video

Build your own IOT service! Collect sensor data and send it to a WebServer.

In this article, we are going to create a webserver using the Arduino and the Ethernet shield. The Arduino Ethernet makes everything so easy that it looks like magic. The Arduino Ethernet Shield allows you to easily connect Arduino to the internet. This shield enables Arduino to send and receive data from anywhere in the world with an internet connection. This shield opens up endless amounts of possibility by allowing you to connect your project to the internet in no-time flat.

Components Required

  • Arduino
  • Ethernet shield
  • DHT22
  • Connecting wires
  • Breadboard

Hardware

Circuit Diagram

Firstly place the Ethernet shield on the Arduino and enter the Ethernet cable in the Ethernet shield. Then make the connections for the DHT22 with the Arduino as follows

  • First pin of DHT22 with the 5V of Arduino.
  • Second pin of DHT22 to the pin 8 of Arduino.
  • Last pin of the DHT22 to the GND of Arduino.

                                                    

        

Ethernet shield

The Ethernet shield connects the Arduino to the Internet. The Ethernet Shield is based upon the W51000 chip, which has an internal 16K buffer. It has a connection speed of up to 10/100Mb. The setup is very simple: just plug the header pins of the shield into your Arduino, then connect an Ethernet cable to the shield.

DHT22

The DHT22 is a basic, low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin. The sensor use a proper protocol, using one wire transmission, modulating the value in time, first it will get the temperature, then the humidity value. The others used pins are Vdd and Gnd.

Specifications

  • power supply: 3.3V – 6V DC
  • output signal: single-bus
  • sensing element: polymer humidity capacitor & DS18B20
  • measuring range: humidity 0-100% RH / temperature -40°C – 125°C
  • accuracy: humidity ±2% / temperature ±0.2°C
  • sensing period: ~2s

Software Arduino Uno Program

#include "DHT.h"

#include <SPI.h>

#include <Ethernet.h>

#define DHTPIN 8

#define DHTTYPE DHT22

 

DHT sensor(DHTPIN, DHTTYPE);

byte mac[] = {

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

             };

 

IPAddress ip(192, 168, 1, 177);

EthernetServer server(80);

 

void setup()

{

  Serial.begin (9600);

  sensor.begin( );

  Ethernet.begin(mac, ip);

  server.begin( );

  Serial.print("Your IP Adress is ");

  Serial.println(Ethernet.localIP( ) );

}

 

void loop( )

{

  float humidity = sensor.readHumidity( );

  float temperature_C = sensor.readTemperature( );

  float temperature_F = sensor.readTemperature (true);

  if (isnan(humidity) || isnan(temperature_C) || isnan(temperature_F))

  {

    Serial.println("Failed to read from DHT sensor!");

    return;

  }

  float heat_indexF = sensor.computeHeatIndex(temperature_F, humidity);

  float heat_indexC = sensor.convertFtoC(heat_indexF);

   

  EthernetClient webpage = server.available();

  if (webpage)

    {

      Serial.println("new webpage");

      boolean currentLineIsBlank = true;

      while (webpage.connected ( ) )

        {

          if (webpage.available ( ) )

            {

              char character = webpage.read ( );

              Serial.write(character);

              if (character == '\n' && currentLineIsBlank)

                {

                  webpage.println ("HTTP/1.1 200 OK");

                  webpage.println ("Content-Type: text/html");

                  webpage.println ("Connection: close");

                  webpage.println ("Refresh: 5");

                  webpage.println ( );

                  webpage.println ("<!DOCTYPE HTML>");

                  webpage.println ("<html>");

                  webpage.print ("<Title>Arduino Ethernet Webserver                </Title>");

                  webpage.print ("<h1>Arduino Ethernet Shield Webserver </h1>");

                  webpage.print ("<h3><a href='http://electrosome.com'>Visit us for Amazing

                                  Projects</a> </h3>");

                  webpage.print ("<h4>Temperature in C: ");

                  webpage.print (temperature_C);

                  webpage.print ("</h4><h4>Temperature in Fah: ");

                  webpage.print (temperature_F);

                  webpage.print ("</h4><h4>Humidity: ");

                  webpage.print (humidity);

                  webpage.print ("</h4><h4>Heat Index in F: ");

                  webpage.println (heat_indexF);

                  webpage.println ("</h4><h4>Heat Index in C: ");

                  webpage.println (heat_indexC);

                  webpage.println ("<br />");

                  webpage.println ("</html>");

                  break;

                }

                 

                if ( character == '\n')

                  {

                    currentLineIsBlank = true;

                  }

                else if (character != '\r')

                  {

                    currentLineIsBlank = false;

            }

        }

    }

    delay(1);

    webpage.stop();

    Serial.println("webpage disconnected");

  }

}

Code Explanation

First of all, we included the libraries for the DHT22 temperature and humidity sensor and for the Arduino Ethernet shield. Then we defined the pin where we have to connect the data pin of DHT22 sensor and then we defined the type of DHT22 sensor. There are different types of DHT sensors available so we have to define the type that we are going to use.

#include "DHT.h"

#include <SPI.h>

#include <Ethernet.h>

#define DHTPIN 8

#define DHTTYPE DHT22

DHT sensor(DHTPIN, DHTTYPE);

Then we defined the mac address of Ethernet shield, this is the default mac address of Ethernet shield and leave it as it is. After that we give the IP and port where it will send the data.

byte mac[] = {

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

             };

IPAddress ip(192, 168, 1, 177);

EthernetServer server(80);

Then in the loop function, we read the humidity and the temperature from the DHT22 sensor and checked that if the sensor is reading or not. If the sensor won’t be reading, then an error will be printed on the serial monitor. Then using the humidity and temperature values we calculated the heat index.

float humidity = sensor.readHumidity ( );

float temperature_C = sensor.readTemperature ( );

float temperature_F = sensor.readTemperature (true);

if ( isnan( humidity ) || isnan( temperature_C ) || isnan( temperature_F ) )

  {

    Serial.println( "Failed to read from DHT sensor!" );

    return;

  }

float heat_indexF = sensor.computeHeatIndex (temperature_F, humidity);

float heat_indexC = sensor.convertFtoC (heat_indexF);

Then we set the server using the internet router on the IP and port that we give in the start. If the connection between the Ethernet shield and the router will be successful, then a webpage will be created and we can send the data of the sensors on the webpage.

EthernetClient webpage = server.available ( );

if (webpage)

{

Serial.println( "new webpage" );

boolean currentLineIsBlank = true;

while (webpage.connected ( ) )

  {

    if ( webpage.available ( ) )

      {

        char character = webpage.read ( );

        Serial.write (character);

        if ( character == '\n' && currentLineIsBlank )

          {

            webpage.println (" HTTP/1.1 200 OK ");

            webpage.println("Content-Type: text/html");

            webpage.println("Connection: close");

Then we used the HTML commands to write the data on the webpage. First we write the title and then we write the sensors data using different headings.

          webpage.println (" <!DOCTYPE HTML> ");

          webpage.println  (" <html> ");

          webpage.print (" <Title>Arduino Ethernet Webserver </Title> ");

          webpage.print (" <h1>Arduino Ethernet Shield Webserver </h1> ");

          webpage.print (" <h3><a href='http://electrosome.com'>Visit us for Amazing Projects</a>

                           </h3>");

          webpage.print ("  <h4>Temperature in C:  ");

          webpage.print (temperature_C);

          webpage.print ( " </h4><h4>Temperature in Fah:  ");

Ethernet configuration

To control the Ethernet shield, we have to use Ethernet.h library.

The shield must be assigned a MAC and IP address using the Ethernet.begin() function. For a particular device, a MAC address is a globally unique identifier. Validity of IP addresses depends on the configuration of one’s network. If DHCP is used, it may dynamically assign an IP to the shield.

IP ADDRESS

IP address (Internet Protocol address) is a numerical label assigned to each device participating in a computer network that uses the Internet Protocol for communication. 

MAC ADDRESS

MAC address (media access control address) is a unique identifier assigned to each device participating in a physical network. Each piece of networking equipment has a unique serial number to identify itself over a network and this is normal hard-programmed into the equipment’s firmware. However, with Arduino, we can define the MAC address ourself.

Practical Implementation of  Circuit


Arduino Based Ethernet WebServer Practical Implementation

Working

This module is designed to monitor the temperature and humidity of the environment where it is located by using DHT22 sensor and updates that to WebServer by using Ethernet Shield.

The DHT22 sensor monitors and measures the temperature and humidity. Heat Index can be calculated by using the measured temperature and humidity values.

 


Comments

Popular posts from this blog

Interfacing L298N Motor Driver with Arduino Uno

1 May Interfacing L298N Motor Driver with Arduino Uno In this tutorial we will learn how to interface  L298N  motror driver with  Arduino Uno . You might be thinking why we need L298N for controlling a motor. The answer is very simple,  Arduino  board or a  microcontroller  IO pins don’t have enough current/voltage driving capability to drive a motor. For driving the motor in both directions (clockwise and anti-clockwise) we need to use an  H-Bridge . Please read our article  H-Bridge – DC Motor Driving  for more information. L298N is an integrated monolithic circuit with dual H-Bridge. It can be used to rotate the motor in both directions and to control the speed of the motor using  PWM  technique. Components Required Arduino Uno L298N Motor Driver 12V battery 2x DC Motors Jumper wires L298N Motor Driver Module L298N Motor Driver Connections Explained Specifications Output A, Output B – To connect two motors. Driver Power Input – Board can accept 5V to 35V which will act as the power

E-TECH SOLAR INVERTER SOLUTION

    DEEP CYCLE BATTERIES The reason many projects fail can mostly be put at the door step of batteries due to inadequate charging and low charge/discharge cycles. Our batteries are  among the very top 3 in the industry and capable of undergoing several cycle  of up to 5 years @ 20% DOD. Hence, we are confident of giving as much as 2 years warranty for some of our batteries SOLAR SYSTEM FOR DOMESTIC AND INDUSTRIAL USE  Many businesses such as hospitals, schools, shopping malls, hotels etc are beginning to embrace solar to completely eliminate unnecessary spending on diesels ……. why not come on board now and earn yourself some carbon credit apart from huge savings in millions of Naira.   SOLAR PANEL MODULES Our panels are carefully selected and tested from the very best among  the Tier one range of solar panels. We guarantee a sustainable yield for at least 25 years and 15 years warranty Inverter Our hybrid inverter chargers are super-powered with

Using the TLP250 Isolated MOSFET Driver

Using the TLP250 Isolated MOSFET Driver - Explanation and Example Circuits I’ve already shown how to drive an N-channel MOSFET (or even an IGBT) in both high-side and low-side configurations in a multitude of ways. I’ve also explained the principles of driving the MOSFETs in these configurations. The dedicated drivers I’ve shown so far are the TC427 and IR2110. Some people have requested me to write up on MOSFET drive using the very popular TLP250. And I’ll explain that here. The TLP250, like any driver, has an input stage, an output stage and a power supply connection. What’s special about the TLP250 is that the TLP250 is an optically isolated driver, meaning that the input and output are “optically isolated”. The isolation is optical – the input stage is an LED and the receiving output stage is light sensitive (think “photodetector”). Before delving any further, let’s look at the pin configuration and the truth table. Fig. 1 - TLP250 Pin Configuration Fig.