MKR ETH Shield Web Server
Learn the basics of Ethernet, how it compares to Wi-Fi and how to setup a web server that can be accessed from a browser on the same network.
Introduction
In this tutorial, we will test the functionality of the MKR ETH Shield, by creating a web server. The MKR ETH Shield is designed to be mounted on top of any MKR family board, where it can provide a solid connection to the Internet, through its ethernet connector.
We will test it out, by first reading the value of 5 analog sensors on the board, and print them to a web server. Any client connecting on the same network will then be able to view the values in a browser.
Goals
The goals of this project are:
- Understand what Ethernet is.
- Learn how Ethernet compares to Wi-Fi.
- Build a simple web server example.
- Access the board from another device (through the browser).
Hardware & Software Needed
- Arduino IDE (online or offline).
- Ethernet library installed.
- MKR ETH Shield (link to store)
- Arduino MKR family board (link to store)
- Ethernet cable.
Ethernet
Ethernet is best described as a collection of computer networking technologies used in different type of networks, such as LAN (Local Area Network) and WAN (Wide Area Network). As one of the main carriers of the Internet Protocol, it is considered to be one of the main technologies that makes up the Internet.
So Ethernet is an incredibly useful technology to understand and work with, seeing as without it, the Internet would not be the same. Often when we refer to the Internet, or IoT, we typically think wireless, such as Wi-Fi and mobile networks. The truth is, we are still, and very likely will, depend entirely on cables that stretch around the world.
While our smartphones, laptops and smart objects do not physically connect to the Internet, it is very common that our router does. Wi-Fi operates on a very local scale, such as your home and office, and mobile networks covers as much as the radio tower can reach. But behind it all, cables are connected everywhere, and Ethernet is often the key technology here.
Ethernet vs Wi-Fi
So you might wonder, in what scenario would I prefer to choose Ethernet as a means of getting online? Why not use Wi-Fi? Let's take a look!
If we are building a project that requires Internet, but is built for an inside environment, Wi-Fi is a great option. We do not have to worry about cables, we can power it with batteries and the connection will be stable as long as it is in range of a gateway.
But let's say we are building something that requires a very stable Internet connection, in a rough environment (e.g. outside, garage, industrial environment), the Wi-Fi option may become limited. If this is the case, an Ethernet connection might be much more suited.
Another example is a larger industrial facility that may have several sensors, machines and other electronic equipment. This may disturb the Wi-Fi signals from the device to a gateway, but if we are using a cable: no problem at all. It is always guaranteed a higher speed through an Ethernet cable than Wi-Fi, as there are little to no disturbance, as it ignores surrounding elements.
Let's take a look at some pros and cons of using Ethernet vs Wi-Fi:
Ethernet pros
- High speed connection.
- Robust connectors.
- Suitable for noisy environments.
Ethernet cons
- Needs physical connection.
- Less flexible
Wi-Fi pros
- Needs no physical connection.
- More flexible (e.g. battery powered).
Wi-Fi cons
- Lower speed.
- Can be problematic in noisy environments.
But enough arguing whether Ethernet or Wi-Fi is the best option: they are both simply great ways of connecting to our beloved Internet, and can be combined to make really good applications.
Circuit
The circuit in this tutorial is easy to follow. First mount the MKR ETH Shield on top of an Arduino MKR board.
Then, connect an Ethernet cable to the connector on the shield.
Programming the Board
We will now get to the programming part of this tutorial.
1. First, let's make sure we have the drivers installed for the board we are using. If we are using the Web Editor, we do not need to install anything. If we are using an offline editor, we need to install it manually. This can be done by navigating to Tools > Board > Board Manager.... Here we need to look for the Arduino SAMD boards (32-bits Arm® Cortex®-M0+) and install it.
2. There is no need to install any additional library, the Ethernet library is already installed on both the Offline and Online IDEs. Instead, let's take a look at some of the core functions we will use in the sketch:
- IP address for your controller.IPAddress ip(192, 168, 0, 78)
- set the port (default is 80).EthernetServer server(80)
- start the Ethernet connection.Ethernet.begin(mac, ip)
- checks whether a cable is connected or not.Ethernet.linkStatus()
- check if a client is available.client.available()
- checks if a client is connected.connected()
- reads the data from the client.read()
- prints to the client.client.println()
The sketch can be found in the snippet below. Upload the sketch to the board.
1#include <SPI.h>2#include <Ethernet.h>3
4// Enter a MAC address and IP address for your controller below.5// The IP address will be dependent on your local network:6byte mac[] = {7 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED8};9IPAddress ip(192, 168, 0, 78);10
11// Initialize the Ethernet server library12// with the IP address and port you want to use13// (port 80 is default for HTTP):14EthernetServer server(80);15
16void setup() {17
18Ethernet.init(5); //MKR ETH Shield19
20 // Open serial communications and wait for port to open:21 Serial.begin(9600);22 while (!Serial) {23 ; // wait for serial port to connect. Needed for native USB port only24 }25 Serial.println("Ethernet WebServer Example");26
27 // start the Ethernet connection and the server:28 Ethernet.begin(mac, ip);29
30 // Check for Ethernet hardware present31 if (Ethernet.hardwareStatus() == EthernetNoHardware) {32 Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");33 while (true) {34 delay(1); // do nothing, no point running without Ethernet hardware35 }36 }37 if (Ethernet.linkStatus() == LinkOFF) {38 Serial.println("Ethernet cable is not connected.");39 }40
41 // start the server42 server.begin();43 Serial.print("server is at ");44 Serial.println(Ethernet.localIP());45}46
47
48void loop() {49 // listen for incoming clients50 EthernetClient client = server.available();51 if (client) {52 Serial.println("new client");53 // an http request ends with a blank line54 boolean currentLineIsBlank = true;55 while (client.connected()) {56 if (client.available()) {57 char c = client.read();58 Serial.write(c);59 // if you've gotten to the end of the line (received a newline60 // character) and the line is blank, the http request has ended,61 // so you can send a reply62 if (c == '\n' && currentLineIsBlank) {63 // send a standard http response header64 client.println("HTTP/1.1 200 OK");65 client.println("Content-Type: text/html");66 client.println("Connection: close"); // the connection will be closed after completion of the response67 client.println("Refresh: 5"); // refresh the page automatically every 5 sec68 client.println();69 client.println("<!DOCTYPE HTML>");70 client.println("<html>");71 // output the value of each analog input pin72 for (int analogChannel = 0; analogChannel < 6; analogChannel++) {73 int sensorReading = analogRead(analogChannel);74 client.print("analog input ");75 client.print(analogChannel);76 client.print(" is ");77 client.print(sensorReading);78 client.println("<br />");79 }80 client.println("</html>");81 break;82 }83 if (c == '\n') {84 // you're starting a new line85 currentLineIsBlank = true;86 } else if (c != '\r') {87 // you've gotten a character on the current line88 currentLineIsBlank = false;89 }90 }91 }92 // give the web browser time to receive the data93 delay(1);94 // close the connection:95 client.stop();96 Serial.println("client disconnected");97 }98}
Testing It Out
Now that we uploaded our code to the board, we can test and see if it is working! After it has successfully uploaded, let's open the Serial Monitor. We should now see the following printed:
1Ethernet WebServer Example2server is at 192.168.0.78
Let's copy the IP address and go to the browser of your choice (needs to be connected to the same network). If everything is working, we should see the following:
The content is simply the recent reading of pin A0-A5 on the board, which is then printed to the client. Congratulations, you have now managed to access data from your MKR board from a different device. The page is set to refresh every 5 seconds, so the values update along with it.
If we go back to the Serial Monitor, we can see information on what is going on when a client is connecting to the server.
Troubleshoot
If the code is not working, there are some common issues we can troubleshoot:
- Check that the Ethernet cable is not broken.
- Make sure that your MKR ETH Shield is mounted on a compatible MKR board.
- If you have any compilation issues, try updating the IDE and the Ethernet library.
- If you fail to access it from the browser, make sure the device you connect from is on the same network.
Conclusion
In this tutorial, we have used the MKR ETH Shield to create a local web server that can be accessed from a device on the same network. This is a very basic example, but you can build on it to create really cool projects.
In this tutorial, we used the MKR Zero board, which has no module to connect to the Internet, where the MKR ETH Shield provides the connection.
Feel free to explore the Ethernet library further, and try out some of the many cool functions.
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.