Let’s access the Samples tab, click on the Ethernet section and find the WebServer sample. Let’s see what happens there. At first, we have the comments, and then the interesting part
comes in. To begin, there are two libraries connected: SPI and Ethernet. We need the first one to work with the SPI interface, and the second one to work with the Ethernet shield. Next, we can see some values already saved in the MAC database. This is the MAC-address itself, which we can find on the reverse side of the Ethernet shield. It is written in hexadecimal notation, that’s why it has an 0x prefix. I have already copied the digits of our Ethernet shield here, and you need to do the same with yours. Let me remind you that in the hexadecimal number system, symbols A,B, C, D, E,
and F are also digits. Therefore, we’ve written a set of digits Here and now we can see that an IP object has been created. You must have already heard about IP addresses, with the help of which you can access some remote computers. We too are going to access our Arduino service with our IP address. What address can you write in here? Try to access the admin panel of your router. Normally, when you are connected to Wi-Fi or to the router with a cable, when you access 192.168. 0.1 or 1.1, the admin panel will appear. The standard username and password are admin admin or admin for the username,
and the password is empty. If the standard ones do not work, find the instruction or get hold of the person who configured your network and explain to them why you need to access it. When you’ve finally accessed it, look for the section on DHCP. In the settings, they usually have a certain range of addresses: the
starting and the ending addresses. In our case, this is going to be from 192.168.0.100 to 192.168.0.199. We can choose any value from this range and set it as the IP address of our device. If this subject interests you, you can Find other examples where the IP address Is assigned automatically, But now we are considering the simplest example of all. In our range, I have picked the address 192, 168, 0, 50. It’s important to mention that it’s separated with commas here and not
with periods, as usual. Next, we will create another object, where we shall write out port 80, through which we are going to communicate with our server. Afterwards, some bookkeeping operations take place in the Setup section: we need to connect the computer with a USB cable though a serial
port, create the connection with the server, and input a signal message to the serial port, where, by the way, our local IP address is also output. The main cycle begins next. There are many things to consider. The cycle contains comments, but still let’s go through it very quickly. A client object is created, which checks if there is a client sending a request to the server. And if there is one, the serial port receives the information about it, and then the client’s request is verified. In reality, we don’t need to know everything noted there – just the ending message in the request, because we are outputting a single
page without any command options and we need to look for an empty line here. For this purpose, a corresponding variable is created. Then, as long as the client is connected, we will be receiving their request based on the symbols we get. We shall then output the request to the serial port if we want to read what
information exactly we are getting, i.e. which request is being sent from the browser. Then we need to check that we’ve inserted a line break, and that this new line is empty. In this case, we start sending data from Arduino to the client who has requested this data. Most probably, we will be sending the data to our own laptop connected to
Wi-Fi. These are control headings which we usually can’t see. Here is the message that everything is OK. Then come some other signal messages. Let me bring to your attention this special heading, which will automatically refresh the page every 5 seconds. In fact, this heading is not a very functional one, but I’ll tell you later why we might need it. After that, the output of the page begins, which consists of the markup that we can look up in the browser by right clicking on the content of the source code. I have no intention to explain all the tags in detail right now. I’ll just say that first comes the tag which marks the beginning of
the html-markup, and so on. Here comes the part when we start outputting the data from Arduino to the Internet. There is a counting loop which runs through all the analog pins from 0 to 5. Every time the information from the corresponding analog pin is read off, the message "analog input" will be output. The counter value is the content of the message, read off in the corresponding code, so the line break tag will be output, i.e. the following message will start from a new line. In the end, after the cycle, comes the closing tag which tells us that the markup
text has ended, and the computer checks for the empty line. In one case, the information about the empty line will be saved, but in the other case it won’t. A short delay is created at the end. The client closes the connection, and a signal message is output to the serial port about the client being disconnected from the Internet. We are now going to upload this sketch, although apart from the MAC address and the IP address I am going to change the point until when the cycle is run. This cycle outputs values from analog inputs. Let’s try and connect a single sensor and run the cycle from 0 to 0, as I don’t want to erase it just yet. Let’s see how it works. I have assembled a test device, where I have placed the Ethernet shield on Iskra. The shield has been
connected to the router and
to the Troyka shield. The Troyka shield is installed on top of the Ethernet shield. I have also
connected the potentiometer module, the information from which will be read off and then displayed on a web page. Let’s now try to access the web page, but before that let me start the serial port, where we have our output. We are getting a message that our server has been accessed with this address. Now when I access the server with this address from the browser, I’ll see what information Arduino will output: analog input 1023. You'll notice that every 5 seconds the page is refreshed. I’ll rotate the potentiometer now, and after the page has been
refreshed, the value becomes 537. When I rotate it to 0, the value becomes 0. And so on and so forth. Excellent, now we’ve learnt to transmit data from Arduino to the network. Let me bring to your attention once again the Port Monitor. Let’s open it and we shall see that every 5 seconds we are getting requests from the browser. That’s what they look like, but let’s not examine them in detail. We are just going
to wait until they are run till the end. I want to bring your attention to these 3 lines of the sketch, which are necessary for Arduino Leonardo and Iskra Neo, Leonardo’s analog, to get connected to the computer before running the next sketch. Thing is, like I mentioned before, Arduino Uno and Arduino Leonardo are connected to the computer a bit
differently. When we connect Arduino Uno to the computer, the sketch is run anew, therefore we will be able to see all the information that Arduino had sent on the Port Monitor. As for Leonardo, if we don’t include these 3 lines, the board will be sending some info to the serial port even when there is
no connection, so we may miss some important messages. That’s why we often insert these 3 lines into the sketches, which take part in the data exchange process. In this particular example, if you don’t erase these 3 lines, you will have to start the serial port to allow your server operate correctly. Otherwise you won’t be able to run the sketch which comes after these 3 lines. So you either need to erase these 3 lines or start the Port Monitor to be able to start the web server once all the above-mentioned
actions have been performed.