Introduction
We continue our course of the IoT and Arduino. In the previous article, found at this
link, it has created an application that uses a sensor TMP36 and a display with which we were able to display the current temperature in the place where you were. The final goals of all is to view and change by phone or tablet thresholds of temperature sensors, all using the services of Azure for the IoT. But to interact with the internet, the Arduino Uno is not enough, you need to equip it with other components. What we will add is called an Ethernet shield, or a card connected to the Arduino Uno and the network via Ethernet cable and RJ45 connectors that will allow access to the Internet. We will see in the order:
- Key features of the Ethernet card
- How to connect the Arduino Uno
- Configuration for Internet access
- Test the connectivity of Ethernet network
- Conclusion
Key features of the Ethernet card
- Supply voltage 5V DC.
- To operate must be installed above the Arduino Uno, we will see later in the article how.
- It has an Ethernet controller W5100 with 16k internal memory buffer
- Connection speed ranging from 10 to 100 MB
- It must be connected directly with the SPI port of Arduino.
- It has a slot for SD card.
These are the main features, but for more information, you can see the considerable documentation that already exists
here, where we start from the hardware to the final program. The following is the image of the Ethernet Shield.
How to connect Ethernet shield on the Arduino Uno
To connect the Ethernet card and very simple, as you can see in the following pictures, just overlay the Arduino Uno, being careful not to bend or damage the pins. You will see that you replicate the network adapter, all links of the Arduino Uno, then inputs PWM, Analog inputs, inputs of transmission and reception, the output voltage of 5v and zero volts to power the circuits and external components and finally the reset button that will perform the reset, both of the Ethernet card that the Arduino board as in the following:
Arduino Uno Ethernet Shield right and left before being assembled.
The two boards after assembly. As you can see, the assembly is simple. As you have seen be careful not to damage the pins of the Ethernet card.
Configuration for Internet access
We are past the assembly stage, it's time to configure the adapter so that it can connect to the internet. First, we must connect the Arduino Uno to a power supply, or with the power supply that provides an output voltage of 5V DC and at least 2 amperes, or attaching it directly to the PC through a USB port, providing a voltage necessary for the operation of the card. We must have an Ethernet cable with an RJ45 connector, to be connected to the router that will be the gateway. Now we start the Arduino IDE and create a new sketch of code called "EthernetTest" as shown and we execute a project save.
The new project EthernetTest.
Now for the part of the code. The first thing to do after you create and save the file sketch is to include the necessary libraries to use the Ethernet Shield, exactly SPI.h and Ethernet.h.
-
- #include <SPI.h>
- #include <Ethernet.h>
Find these libraries from the menu Sketch -> Include Library -> Ethernet. You will find that more libraries will be added, but we can have a look at the two mentioned above.
Adding libraries SPI and Ethernet.h on the project.
Now that the necessary libraries have been added, we can start using them. First, we need to define a Media Access Control (MAC) address. It is a unique address for each device that is usually issued by the manufacturer of the network card necessary to identify the device, in our case the Ethernet Shield case if there were more than one. If you do not have this address, we can safely create one, taking care to enter the letter "x" after the first number, because it is an address in hexadecimal numbers. We will enter the MAC address below.
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
We define a data type with a byte length of six elements, representing the MAC address of the network card. We can still define an Internet Protocol (IP) address. Also, in this case, it is a unique address that identifies a device in a host network. This step is optional and can directly assign the router an IP address automatically to the Ethernet Shield, this means the DHCP service. The last thing to do and implement the begin() method, passing as arguments the array values mac[], whereas for the IP address of the router will assign one dynamically. We have everything you need to configure the network card, it just must implement the methods setup() and loop(). We insert inside the initialization of the serial port, so you can see on the serial monitor the connection status.
The following sketch is complete.
-
-
-
-
-
-
- #include <SPI.h>
- #include <Ethernet.h>
-
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- void setup()
- {
-
- Serial.begin(9600);
-
- delay(1000);
-
- if(Ethernet.begin(mac) == 0)
- {
- Serial.println("Connesso");
- }
-
- else
- {
- Serial.println("Errore nella connessione");
- }
- }
- void loop()
- {
- }
Test the connectivity of Ethernet network
We load the ad code that we can do from the button with an arrow symbol to the right, as shown.
After loading the sketch of code to the Arduino Uno, we connect the entire network. In my case, I used an Ethernet output of my router.
Arduino Uno and the Ethernet Shield connected to a router.
Now to see if we have actually networked everything we can connect by IP address, to our service providers, in my case Telecom Italia. Again, to get the IP address of a contact where you have a contract ADSL and more.
The status of the Ethernet devices before connecting the Arduino Uno to the router.
The status of the Ethernet devices after connecting the Arduino Uno to the router.
And as you see, we have a new device connected to the network, it is, in fact, our Arduino Uno as WINZnetEFFEED, clicking the Details button, we will show all the information, such as IP address that the router has assigned to the board. If we do not trust it yet, we can use the command "CMD.EXE", ping the IP address that the router has assigned.
Now we type the command: ping and the IP address provided by your router, in my case 192.168.1.4.
Statistics after pinging an IP address.
And as can be seen, the Arduino board has correctly answered the request.
Conclusion
In this article, we saw the ease with which you can configure an Ethernet shield, assemble it with an Arduino Uno and connect it to a router, then verifying proper operation. In the next article, we will start to delve into Microsoft Azure and be sure to communicate with the Arduino Azure services for IoT and Mobile services.