Finding the MAC address and obtaining a valid IP address
To work with this project, you must know your Arduino Ethernet Shield's MAC address and IP address to communicate properly over the Internet.
Finding the MAC address
Current Arduino Ethernet Shields come with a dedicated and uniquely assigned 48-bit MAC (Media Access Control) address which is printed on the sticker. Write down your Ethernet shield's MAC address so you can refer to it later. The following image shows an Ethernet shield with the MAC address of 90-A2-DA-0D-E2-CD:
You can rewrite your Arduino Ethernet Shield's MAC address using hexadecimal notations, as in 0x90
, 0xA2
, 0xDA
, 0x0D
, 0xE2
and 0xCD
, with the leading 0x
notation recognized by C compilers (remember that the Arduino programming language is based on C) and assembly languages.
If not present, you can use one that does not conflict with your network. For example:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
Obtaining an IP address
You can assign an IP address to your Arduino Ethernet Shield by one of the following methods:
- Using the network router or switch to assign a static IP address to your Ethernet shield.
- Using DHCP (Dynamic Host Configuration Protocol) to dynamically assign an IP address to your Ethernet shield. In this chapter, we will only discuss how to assign an IP address using DHCP.
The network devices we will use for this experiment are the following:
- Huawei E517s-920 4G Wi-Fi Router
- DELL computer with Windows 8.1 installed and Wi-Fi connected
- Nokia Lumia phone with Windows 8.1 installed and Wi-Fi connected
- Arduino Ethernet Shield connected to the Wi-Fi router's LAN port using an Ethernet cable
Assigning a static IP address
The following steps will explain how to determine your network IP address range with a Windows 8.1 installed computer, and select a valid static IP address.
- Open Network and Sharing Center in Control Panel:
- Click on Connections. The Connection Status dialog box will appear, as shown here:
- Click on the Details… button. The Network Connection Details dialog box will appear, as shown in the following screenshot:
- The IPv4 address assigned to the Windows 8.1 computer by the Wireless router is 192.168.1.2. The IPv4 subnet mask is 255.255.255.0. So, the IP address range should be 192.168.1.0 to 192.168.1.255.
- The Wi-Fi network used in this example currently has two devices connected, that is, a Windows 8.1 computer, and a Windows phone. After logging in to the wireless router product information page, under the device list, all the IP addresses currently assigned by the router to the connected devices can be seen, as shown here:
- Now, we can choose any address except 192.168.1.1, 192.168.1.2, and 192.168.1.3.
- Let's assign 192.168.1.177 to the Arduino Ethernet Shield as a static IP address using the following sketch. Upload the following sketch into your Arduino board and open the Serial Monitor to verify the static IP address assigned.
- Open your Arduino IDE and type or paste the following code from the sketch named
B04844_01_06.ino
from the code folder of this chapter.#include <SPI.h> #include <Ethernet.h> byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; byte ip[] = { 192, 168, 1, 177 }; EthernetServer server(80); void setup() { Serial.begin(9600); Ethernet.begin(mac, ip); server.begin(); Serial.print("IP Address: "); Serial.println(Ethernet.localIP()); } void loop () {}
Obtaining an IP address using DHCP
The DHCP can be used to automatically assign a valid IP address to the Arduino Ethernet Shield. The only address you need is the MAC address of the Ethernet shield. Pass the MAC address as a parameter to the Ethernet.begin()
method.
Upload the following Arduino sketch to your Arduino board, and open the Arduino Serial Monitor to see the auto-assigned IP address by the DHCP. Use this IP address to access your Ethernet shield through the Internet. Remember, this IP address may be changed at the next start up or reset.
Open your Arduino IDE and type or paste the following code from the sketch named B04844_01_07.ino
from the code folder of this chapter:
#include <SPI.h> #include <Ethernet.h> byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; EthernetServer server(80); void setup() { Serial.begin(9600); Ethernet.begin(mac); server.begin(); Serial.print("IP Address: "); Serial.println(Ethernet.localIP()); } void loop () {}