Project Description

Description:

Using an Ethernet shield, you can use an Arduino as a Web server.By equipping an Arduino with an Ethernet shield you can turn it into a simple web server, and by accessing that server with a browser running on any computer connected to the same network as the Arduino, you can:
  • Control hardware from the webpage (using Javascript buttons).
  • Read the state of a switch (using simple HTML).
  • Read value of a sensor (using simple HTML).

109

Hardware required

To use an Arduino as a Web server, you need the following:

  • Arduino Mega2560 (or Arduino UNO)
  • Ethernet shield
  • 9V Adaptor
  • pushbutton
  • Wired LAN Connection with speed of 10/100Mb

The Ethernet shield connects the Arduino to the Internet. The setup is very simple: just plug the header pins of the shield into your Arduino, then connect an Ethernet cable to the shield.

Wiring Diagram:

112

Connect the components as shown above. Arduino’s pin 8 is connected to the pushbutton and is configured as INPUT. When the button is pushed, the Arduino will read a LOW value on this pin. The Arduino will then set the status of the OUTPUT to ON. When it is released, the output will be set to OFF. The status of the switch will be sent to the Web server.
Ethernet configuration:
To control the Ethernet shield, you use the 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. Current Ethernet shields come with a sticker indicating the MAC address. For older shields, a random one should work, but one should not use the same one for many boards. 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. Specifying the IP address is done by writing the line:

and change it to match one own setup. For example, to assign the IP of Ethernet shield to 192.168.0.50, write the line:

 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.

You can set the subnet and gateway with the help of following commands:

So, to setup Ethernet Shield, the block of code is given below:

Below is a photo of the system, showing how the Arduino connects to the Wi-Fi router. The Ethernet cable connects shield with the router and router then connects wirelessly with the laptop.

113

Below is a program that loads a simple Web page.

 

You can also set this up to run without the router. To do this you need to:

  1. Assign a manual IP address to the Arduino’s Ethernet say 192.168.0.2 and Subnet mask 255.255.255.0 default Gateway empty.
  2. Use a cross-over Ethernet cable to link the two (laptop and Arduino).
  3. We should then be able to get your Arduino site up on http://192.168.0.2 from the laptop.

Below is the code that you would load into the Arduino to connect it directly to the PC without the router:

 

 Demo: