HUM: ARDUINO ETHERNET SHIELD WITH THE W5100

You are a beginner with Dasduino. Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module (HUM) is a blog tutorials series by soldered where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect the module with Dasduino and the basic code. Everything else is left to your imagination.
INTRODUCTION
In this tutorial, we will show you how to use the Ethernet Shield based on the W5100 integrated circuit. This integrated circuit is the “brain” of this whole module as it is responsible for overall communication with the Internet, sending and receiving data, i.e. packing and unpacking packets that come or go from it. This module can be used if we want to connect our Dasduino to the Internet using wires while retrieving data such as air temperature in our city or posting some of our own measurements (air temperature, air humidity, air pressure, wind speed, etc.) on a web site (e.g. ThingSpeak service) or we want to do some kind of home automatization, so we want to set some parameters using our local network (e.g. adjusting the central heating temperature by accessing Dasduino via our mobile phone that is connected to the same router to which the Dasduino is connected). But, in the beginning, let us find out a bit more about our module.
HOW DOES THE MODULE WORK
Module characteristics:
• Power supply voltage: 5V i 3.3V
• Current: max 183 mA
• Supported Ethernet connection speeds: 10Base-T (10 Mbit/s) i 100Base-Tx (100 Mbit/s)
• The type of communication with Dasduino: SPI (only via ICSP headers and it is used for the SD card and W5100)
• W5100 CS pin: Digital pin 10
• SD Card CS pin: Digital pin 4
For the beginning, let us get acquainted with some basic terms such as Ethernet, IP and MAC addresses, TCP and UDP protocol, HTTP and HTML.
Ethernet is a network communication technology that is highly praised for its robustness, simplicity, and speed. The first versions of Ethernet technology did not use the Ethernet cable we know today (and that is the UTP – Unshielded Twisted Pair and the STP – Shielded Twisted Pair, and even optics), but rather a coaxial cable, that is nowadays used to connect the antenna and the radio device. Due to the high attenuation at longer distances, limited signal transfer rates, one-way communication at a given time, it was quickly rejected and the Ethernet cable we are familiar with today was developed. (The picture shows today’s Ethernet cable [top] and the coaxial Ethernet cable [bottom]).
Ethernet technology uses the CSMA/CD protocol for data transfer, and one of the most important characteristics of this protocol is collision detection (when two devices try to send data at the same time on the same line).
After we have learned something about the physical medium through which the data is sent, we can move on to IP addresses. The IP address is an address that consists of 4 numbers that are separated by full stops. One of the best-known addresses is the popular 192.168.1.1 using which you can access your home router. The address has the purpose of routing the packets through the network, that is, each node in the network has an address of its own, and to know where the data packet needs to be delivered, it is necessary to know the exact IP address of the destination. Also, it is necessary to know the address of the source so that, if unsuccessful packet delivery occurs, the source could be notified. That’s exactly what the TCP protocol does. TCP (Transmission Control Protocol) is a type of protocol used in networks, and its task is to break a large packet into smaller parts (because it is not desirable to send a packet of several gigabytes at once), number each packet, assign a destination address to each packet, and finally, at the destination, return everything to the way it was, and if it comes to a loss of the packet, tell the source to resend this packet. This method of sending a packet is called sending a packet with a confirmation or reliable packet sending. There is another method where the packet is only divided into smaller parts and sent through the network without checking whether the packet has arrived. This protocol is called UDP (User Datagram Protocol). When it comes to MAC address (Media Access Control), it consists of 6 numbers separated by a colon, and they are usually written in a hexadecimal record. Then it accurately defines the device (hardware) it wants to communicate with since there may be more devices on one IP address and it is necessary to know to which device it should forward the received data. The first three numbers signify the manufacturer, while the other three signify the device itself. Now that we know how to receive, route, and send packets, we will deal with how to use these components to open a Web site. To open a Web site, first, we need to make a file request (which is essentially our Web site), which is performed using the HTTP (Hyper Text Transfer Protocol) protocol. It is composed of the requests and responses to these requests, so when we connect to a particular web address and we want to access the web site, we make a GET request after which we have the link to what we want (or if we just want the home page of that address, we write /, and in the end comes a version of HTTP (HTTP 1.0 or HTTP 1.1) . If the server has received it, it responds with 200 OK, which means that our request is received and if it is correct, it sends us what we asked for. If this does not happen, we receive a popular error 404 Not found.
Now that we have received data (a web page in our example), it is necessary to know how it is compiled and in which language it is written. For Websites, the most commonly used language is HTML (HyperText Markup Language), which is actually more a kind of descriptive language than a typical programming language such as C. The HTML code always starts with <html> and ends with </ html> and the HTML code is written in between. Sometimes HTML itself is not enough to create an interactive Web site, so we use a combination of HTML with JavaScript and CSS or PHP. Here, we will focus on HTML, and in one example we will show how to use CSS with HTML in order to create a more complex page with a simple graphical display.
HOW TO CONNECT
Connecting Dasduino and Ethernet Shield is simple, but you should bear a couple of things in mind. Although the pins 10, 11, 12 and 13 on the Dasduino are pins used for SPI communication, on this Shield, they are not. On the contrary, SPI communication is made through the ICSP headers on the Shield. The pins on the Shield are 5V tolerant, which means that Dasduino and Shield are connected without lever converters.
For the second example of the code, we need LEDs. For the third and last DHT11 or DHT22 sensor, depending on what you currently have, just keep in mind that if you are using DHT22, it should also be set in the program code.
ARDUINO CODE
In this part of the tutorial, we will show some examples of the code for the module to retrieve a website (HTML file of the website), how the module should act as a Web server using which we can, e.g. turn the LEDs on or off, and how, with the help of ThingSpeak service, we can send data to our site as well as make a request for parsing the website (parsing is a procedure that extracts the specific part that we are interested in, from the entire website, e.g. a text).
For these examples, it is not necessary to install an Ethernet Shield library because it is already installed with the Arduino IDE, but you just need to check if the latest version of the library is installed by selecting Sketch from the upper menu in the Arduino IDE -> Include Library -> Manage Libraries, find the Arduino Ethernet library and see if you have an Update offered. For the DHT sensor, you will need to install a library that you can find here. If you do not know how to install the library, read our tutorial.
The first example shows how to retrieve a web page (in this case a weather forecast from the pages of the State Hydrometeorological Institute – DHMZ). It should be kept in mind that the HTML file of the default Web site will be displayed.
#include "SPI.h" //Attach the library for SPI communication (using which W5100 Ethernet and Croduino communicate)
#include "Ethernet.h" //Attach the library for controlling the W5100 Ethernet Shield that contains all important functions for opening Web pages, connecting to the Internet, sending requests, etc.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//Ethernet Shield's MAC address (some Shields have labels with printed MAC addresses, enter that address instead of this one).
IPAddress ip(192, 168, 0, 177);
//Set the static IP address if the DHCP does not succeed
IPAddress myDns(192, 168, 0, 1);
EthernetClient client;
//Initialization of the library that executes all the functions related to connecting to the Web page, Internet, sending and receiving data, etc.
void
setup
() {
Serial.begin(115200);
//Serial communication initialization between Croduino and the computer. (115200 Baud).
Ethernet.init(10);
//Defining the CS (Chip Select) pin of our W5100 Ethernet on the Croduino. In our case, it is the pin 10.
Serial.println(F(
"Ethernet initialization using the DHCP..."
));
if
(Ethernet.begin(mac) == 0) {
Serial.println(F(
"Unsuccessful Ethernet setup using the DHCP."
));
if
(Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println(F(
"W5100 Ethernet not found. Check whether the W5100 Ethernet is connected properly.."
));
do
{
//Infinite loop, it does not make sense to go on with the program if W5100 Ethernet has not been found.
}
while
(
true
);
}
if
(Ethernet.linkStatus() == LinkOFF) {
Serial.println(F(
"Ethernet cable is not connected! Connect the Ethernet cable to W5100 Ehternet and restart it."
));
do
{
//Infinite loop, it does not make sense to go on with the program if Ethernet is not connected to anything.
}
while
(
true
);
}
Ethernet.begin(mac, ip, myDns);
//If everything is fine, but it is not possible to assign the DHCP, work with a static IP address.
}
else
{
Serial.print(F(
"Successfully assigned dynamic IP address using the DHCP: "
));
//If it is possible, assign a dynamic IP address via DHCP and print it.
Serial.println(Ethernet.localIP());
}
delay
(1000);
//Wait a moment for the Ethernet to initialize to the new IP address.
//Let us try to open a certain Web page, e.g. weather forecast from www.meteo.hr
Serial.println(F(
"Trying to connect to www.meteo.hr ..."
));
if
(client.connect(
"www.meteo.hr"
, 80)) {
//Let us try to connect to meteo.hr, since we want the Web page (which is http), in this case we look for port 80 (it can also be 88, 8080 or 8008)
client.println(F(
"GET /index.php?tab=prognoza&it=sutra HTTP/1.1"
));
//Since we want to open a Web page, we will make a GET request in order to retrieve it and we want the HTTP version to be 1.1
client.println(F(
"Host: www.meteo.hr"
));
//It is necessary to state who is the host
client.println(F(
"Connection: close"
));
//And after the host sends the Web page, end the connection.
client.println();
}
else
{
Serial.println(F(
"Unsuccessful connection to www.meteo.hr :("
));
//If we were not able to connect to the desired Web page, notify the user.
}
while
(client.connected()) {
//As long as the host (i.e. the server where the Web page is located) is connected to us, save and print what it sends to us
while
(client.available()) {
//As long as there is data, save them to the buffer
uint8_t buf[100];
//Since Croduino has small RAM memory, we can not save all the data sent by the server (in this case, HTML site) at once, we have to do it piece by piece
int
n = client.available();
//Checking how many characters/data is left to be received
if
(n>100) n = 100;
//If it exceeds the size of our buffer, limit the number to the dimension of our buffer (in this case 100 characters)
client.read(buf, 100);
//Save data/characters to our buffer
Serial.write(buf, 100);
//Display it on the Serial Monitor
}
}
client.stop();
//After everything is received, end the connection with the server / Web page host
}
void
loop
() {
// put your main code here, to run repeatedly:
}
The second example shows us how Dasduino can be used as a small Web server that can be accessed within a local network, with the help of Ethernet Shield. After connecting the Ethernet Sheild to the network, on a computer or mobile phone, it is connected to the same network as Dasduino, it is necessary to open the Web browser and enter the IP address from the example code. A Web site that allows the LEDs connected to the Dasduino to turn on or off should open.