Hardware and software requirements
All the hardware and software requirements are mentioned within each experiment. Most of the hardware used in this project are open source, which allows you to freely learn and hack them to make more creative projects based on the blueprints of this chapter.
Arduino Ethernet Shield is used to connect your Arduino UNO board to the Internet. It is an open source piece of hardware and is exactly the same size as the Arduino UNO board. The latest version of the Arduino Ethernet Shield is
R3 (Revision 3). The official Arduino Ethernet Shield is currently manufactured in Italy and can be ordered through the official Arduino website (https://store.arduino.cc). Also, there are many Arduino Ethernet Shield clones manufactured around the world that may be cheaper than the official Arduino Ethernet Shield. This project is fully tested with a clone of Arduino Ethernet Shield manufactured in China.
Plug your Arduino Ethernet Shield into your Arduino UNO board using wire wrap headers so that it's exactly intact with the pin layout of the Arduino UNO board. The following image shows a stacked Arduino UNO and Arduino Ethernet Shield together:
Arduino Ethernet Shield consists of an Ethernet controller chip—WIZnet W5100—the only proprietary hardware used with the shield. The WIZnet W5100 includes a fully hardwired TCP/IP stack, integrated Ethernet
MAC (Media Access Control), and
PHY (Physical Layer).
The hardwired TCP/IP stack supports the following protocols:
- TCP (Transport Control Protocol)
- UDP (User Datagram Protocol)
- IPv4 (Internet Protocol Version 4)
- ICMP (Internet Control Message Protocol)
- ARP (Address Resolution Protocol)
- IGMP (Internet Group Management Protocol)
- PPPoE (Point-to-Point Protocol over Ethernet)
The WIZnet W5100 Ethernet controller chip also simplifies the Internet connectivity without using an operating system.
Throughout this chapter, we will only work with TCP and IPv4 protocols.
The Arduino UNO board communicates with the Arduino Ethernet Shield using digital pins 10, 11, 12, and 13. Therefore, we will not use these pins in our projects to make any external connections. Also, digital pin 4 is used to select the SD card that is installed on the Arduino Ethernet Shield, and digital pin 10 is used to select the Ethernet controller chip. This is called SS (Slave Select) because the Arduino Ethernet Shield is acting as the slave and the Arduino UNO board is acting as the master.
However, if you want to disable the SD card and use digital pin 4, or disable the Ethernet controller chip and use digital pin 10 with your projects, use the following code snippets inside the setup()
function:
- To disable the SD card:
- To disable the Ethernet Controller chip:
The Arduino Ethernet board
The Arduino Ethernet board is a new version of the Arduino development board with the WIZnet Ethernet controller built into the same board. The USB to serial driver is removed from the board to keep the board size the same as Arduino UNO and so that it can be stacked with any Arduino UNO compatible shields on it.
You need an FTDI cable compatible with 5V to connect and program your Arduino Ethernet board with a computer.
You can visit the following links to get more information about the Arduino Ethernet board and FTDI cable:
You can build all the projects that are explained within this chapter and other chapters throughout the book with the Arduino Ethernet board using the same pin layout.
Connecting Arduino Ethernet Shield to the Internet
To connect your Ethernet shield to the Internet, you require the following hardware:
Use the following steps to make connections between each hardware component:
- Plug your Ethernet shield into your Arduino board using soldered wire wrap headers:
- Get the Ethernet cable and connect one end to the Ethernet jack of the Arduino Ethernet Shield.
- Connect the other end of the Ethernet cable to the Ethernet jack of the network router or switch.
- Connect the 9VDC wall adapter power supply to the DC barrel connector of the Arduino board.
- Use the USB A-to-B cable to connect your Arduino board to the computer. Connect the type A plug end to the computer and the type B plug end to the Arduino board.
Testing your Arduino Ethernet Shield
To test you Arduino Ethernet Shield, follow these steps:
- Open your Arduino IDE and navigate to File | Examples | Ethernet | WebServer:
- The sample sketch WebServer will open in a new Arduino IDE:
- You can also paste the code from the sketch named
B04844_01_01.ino
from the code folder of this chapter. The following header files should be included for serial communication and Ethernet communication in the beginning of the sketch: - Replace the MAC address with your Ethernet shield's MAC address if you know it. You can find the printed sticker of the MAC address affixed to the back of your Ethernet shield. (Some clones of Arduino Ethernet Shield don't ship with a MAC address affixed on them). If you don't know the MAC address of your Arduino Ethernet Shield, use the one mentioned in the sample code or replace it with a random one. But don't use network devices with the same MAC address on your network; it will cause conflicts and your Ethernet shield will not function correctly. (Read Finding the MAC address and obtaining a valid IP address for more information on MAC addresses).
- Replace the IP address with a static IP in your local network IP range. (Read the Finding the MAC address and obtaining a valid IP address section for selecting a valid IP address).
- Then, create an instance of the Arduino Ethernet Server library and assign port number 80 to listen to incoming HTTP requests.
- Inside the
setup()
function, open the serial communications and wait for the port to open. The computer will communicate with Arduino at a speed of 9600 bps. - The following code block will start the Ethernet connection by using the MAC address and IP address (we have assigned a static IP address) of the Arduino Ethernet Shield and start the server. Then it will print the IP address of the server on Arduino Serial Monitor using
Ethernet.localIP()
: - Inside the
loop()
function, the server will listen for incoming clients. - If a client is available, the server will connect with the client and read the incoming HTTP request. Then, reply to the client by the standard HTTP response header. The output can be added to the response header using the
EthernetClient
class's println()
method: - Finally, close the connection from the client using the
EthernetClient
class's stop()
method: - Verify the sketch by clicking on the Verify button located in the toolbar.
- On the menu bar, select the board by navigating to Tools | Board | Arduino UNO. If you are using an Arduino Ethernet board, select Tools | Board | Arduino Ethernet.
- On the menu bar, select the COM port by navigating to Tools | Port and then selecting the port number.
- Upload the sketch into your Arduino UNO board by clicking on the Upload button located in the toolbar.
- Open your Internet browser (such as Google Chrome, Mozilla Firefox, or Microsoft Internet Explorer) and type the IP address (
http://192.168.1.177/
) assigned to your Arduino Ethernet Shield in the sketch (in Step 4), and hit the Enter key. - The web browser will display analog input values (impedance) of all the six analog input pins (A0-A5). The browser will refresh every 5 seconds with the new values. Use following code to change the automatic refresh time in seconds:
- To make your sketch more stable and to ensure that it does not hang, you can do one of the following:
- Remove the SD card from the slot.
- Add the following two lines inside your
setup()
function:
Now you can be assured that your Arduino Ethernet Shield is working properly and can be accessed through the Internet.