Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Internet of Things with Arduino Blueprints

You're reading from   Internet of Things with Arduino Blueprints Develop interactive Arduino-based Internet projects with Ethernet and WiFi

Arrow left icon
Product type Paperback
Published in Oct 2015
Publisher
ISBN-13 9781785285486
Length 210 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Pradeeka Seneviratne Pradeeka Seneviratne
Author Profile Icon Pradeeka Seneviratne
Pradeeka Seneviratne
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Internet-Controlled PowerSwitch FREE CHAPTER 2. Wi-Fi Signal Strength Reader and Haptic Feedback 3. Internet-Connected Smart Water Meter 4. Arduino Security Camera with Motion Detection 5. Solar Panel Voltage Logging with NearBus Cloud Connector and Xively 6. GPS Location Tracker with Temboo, Twilio, and Google Maps 7. Tweet-a-Light – Twitter-Enabled Electric Light 8. Controlling Infrared Devices Using IR Remote Index

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:

Finding the MAC address

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.

  1. Open Network and Sharing Center in Control Panel:
    Assigning a static IP address
  2. Click on Connections. The Connection Status dialog box will appear, as shown here:
    Assigning a static IP address
  3. Click on the Details… button. The Network Connection Details dialog box will appear, as shown in the following screenshot:
    Assigning a static IP address
  4. 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.
  5. 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:
    Assigning a static IP address
  6. Now, we can choose any address except 192.168.1.1, 192.168.1.2, and 192.168.1.3.
  7. 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.
  8. 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 () {}
    Assigning a static IP address

    A static IP address

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 () {}
Obtaining an IP address using DHCP

DHCP assigned IP address

You have been reading a chapter from
Internet of Things with Arduino Blueprints
Published in: Oct 2015
Publisher:
ISBN-13: 9781785285486
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime