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! 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
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arduino Networking
Arduino Networking

Arduino Networking: Connect your projects to the Web using the Arduino Ethernet library

Arrow left icon
Profile Icon Marco Schwartz
Arrow right icon
€18.99 per month
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.9 (9 Ratings)
Paperback Aug 2014 118 pages 1st Edition
eBook
€8.99 €17.99
Paperback
€21.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Marco Schwartz
Arrow right icon
€18.99 per month
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.9 (9 Ratings)
Paperback Aug 2014 118 pages 1st Edition
eBook
€8.99 €17.99
Paperback
€21.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€8.99 €17.99
Paperback
€21.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Arduino Networking

Chapter 1. Discover the Arduino Ethernet Shield

In this first chapter of the book, we will only focus on the basics and get started with the Arduino Ethernet shield. In order to build more complex projects in the next chapters of the book, we first need to be absolutely sure that our shield is functioning correctly, and it can connect to our local network and the Web.

To ensure this, we'll first assemble the hardware, and then build a simple Arduino sketch that will connect to the Web, grab a test web page, and display it back inside the Arduino Serial Monitor.

These will be the major takeaways of this chapter:

  • First, we'll make sure that you have all the required hardware and software components. We are also going to assemble the shield and the Arduino Uno board, and connect everything to our local network via a router.
  • Then, we will write the sketch that will connect your shield to your local network as well as the Web, and this sketch will grab a test page to make sure your connection is working correctly.
  • Finally, we are going to test this sketch and monitor the status of the Ethernet shield on the Arduino Serial Monitor. From this first test of the shield, we'll be able to tell whether the shield is correctly connected to the Internet or not.

Hardware and software requirements

You don't need a lot of hardware for this first project. You only need an Arduino Uno board, and of course, the Arduino Ethernet shield. Other boards such as the Arduino Mega will work as well for the projects in the first chapter of the book, but you might face difficulties for the projects in the following chapters in the book. Therefore, I recommend that you use an Arduino Uno board for all the projects of this book.

Before actually assembling the shield with the board, make sure to write down the MAC address of the shield, which is written on the back of the shield. We will need it later in the project when we write the first sketch.

This is an image of the assembled board and the Ethernet shield:

Hardware and software requirements

You will also need a USB B to USB A cable to connect the Arduino board to your computer, and an Ethernet cable to connect the shield to a router.

On the software side, all you need is the Arduino IDE, which can be found at the following address:

http://arduino.cc/en/main/software

The Ethernet library is included by default in the Arduino software, so you don't need to install any additional components.

Hardware configuration

The hardware configuration in this chapter is really simple. At this point, you should already have the Arduino Ethernet shield connected to your Arduino Uno board. If that's not the case, please do so now.

You then have only two cables to plugin: a USB cable between your computer and the Arduino Uno board and the Ethernet cable. Simply connect the USB cable to one port of your computer.

For the Ethernet cable, you have several options. The easiest thing you can do, which is what I recommend, is to connect the Ethernet cable directly from the shield to the main router of your home. Usually, you will have a Wi-Fi router in your home, which you use to enable Wi-Fi connectivity for your computer and other devices. This router should also have some Ethernet ports, where you can connect your Ethernet shield. The advantage of this solution is that your shield will automatically get an IP address and will have access to the Internet automatically. This assumes that your router is configured for DHCP; go to your router settings to enable DHCP. As this procedure varies depending on the brand of your router, refer to your router documentation to find out how to do this on your specific router.

If you don't have a router, you can directly connect the Ethernet cable to your computer. I don't recommend this alternative, as configuring your computer to connect to the shield and sharing the Internet connection with the shield is really complex, and it also depends on your operating system.

If you really don't have access to a router but you have access to a Wi-Fi network, I recommend buying one of these small Wi-Fi routers that have an Ethernet port. It can connect to any Wi-Fi network in range and share the connection automatically with other devices via Ethernet.

For example, as shown in the following image, I recommend the TP-Link MR3020 router:

Hardware configuration

This is an image of the assembled hardware for the tasks in this chapter, with the USB cable and the Ethernet cable plugged in the project:

Hardware configuration

If everything is wired correctly, you should see the ON LED on the Ethernet shield is switched on, as well as the LINK LED near the Ethernet port.

Testing your connection

Now that the hardware is ready, we can write our first sketch to test the Ethernet shield and the connection to the Web. Note that the pieces of code shown in this section are only the most important parts of the code, and you can find the complete code inside the GitHub repository of the book.

Start the Arduino sketch by including the following required libraries to use the shield:

#include <SPI.h>
#include <Ethernet.h>

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Then, we have to define the MAC address of the Ethernet shield. This address is located just behind the shield, and you should have noted it down already. You have to enter it in the following format:

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xFE, 0x40 };

We also need to define where we are going to connect to test the connection. You can use any web page you want, or even perform a search on Google, but for this first test, I want to use a simple page.

I found this website with a simple test page that we'll try to grab while making a request later in the sketch. You can also set up your own page if you have a web server online, for example, if you have a blog hosted somewhere.

The website address is stored in a char variable:

char server[] = "www.brainjar.com";

Note that you can also use other pages here, for example http://www.example.com/hello.

The Ethernet shield will then automatically get the IP address of this website.

To get an IP address for the Ethernet shield itself, we'll use DHCP to automatically get one from the router we are connected to. However, if DHCP fails, we need to assign a default address to the shield.

This is stored in an IPAddress variable. Note that you can put anything you want inside this variable. As for this first project, we really need DHCP to work to get connected to the Web. However, it is a good practice to specify an IP address in the same subnet as your router, so the shield can at least connect to your local network. For example, the IP address of my computer was 192.168.1.100, so I specified a similar IP address for the shield:

IPAddress ip(192,168,1,50);

We can now create the instance for the Ethernet client with the following code:

EthernetClient client;

Now, in the setup() function of the sketch, we will try to get an IP address using DHCP. If you're connected to a router, there is no reason it would fail. However, if it does indeed fail, we will automatically set the default IP address for the shield:

if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
}

Then, we will print out the IP address on the Serial port for debugging reasons:

Serial.begin(115200);
Serial.print("IP address: ");
Serial.println(Ethernet.localIP());

Now, in the loop() function of the sketch, we will actually connect to the server. It starts by calling the connect function and checks whether we are indeed connected. If that's the case, we print it out on the Serial monitor for debugging purposes:

if (client.connect(server, 80)) {
    if (client.connected()) {
      Serial.println("connected");

Now that we are connected, we can set the GET request for the test page we want to access:

client.println("GET /java/host/test.html HTTP/1.1");
client.println("Host: www.brainjar.com");
client.println("Connection: close");
client.println();

After the request is sent, we will read the data that is coming back from the server, to check whether everything went fine. We will also print out this data on the Serial monitor:

while (client.connected()) {
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
}

Finally, when we are sure that the client is not connected anymore, we will print the information on the Serial monitor and call the close() function on the Ethernet client:

if (!client.connected()) {
  Serial.println();
  Serial.println("disconnecting.");
  client.stop();
}

Finally, we don't want to continuously do this action, but only repeat it every five seconds. This is done with a delay() function:

delay(5000);

It's now time to test the sketch.

Note

The complete code for this first chapter can be found the GitHub repository of the book:

https://github.com/openhomeautomation/arduino-networking/tree/master/chapter1

Make sure that the Ethernet cable is plugged in your shield and your router, and upload the sketch to the Arduino board. You can now also open the Serial Monitor, and select the correct Serial speed (115200 for the Arduino sketch of this chapter); that's the first thing you should see, which is the IP address of your board:

IP address: 192.168.1.103

Then, the Arduino board should connect to the server:

Connecting...

If this is successful, the output will show that it is indeed connected:

connected

Now, the Arduino board will send the GET request to the server in order to grab the content of the test page. The server will answer with an HTTP 200 OK status if the request was successful, along with the contents of the page:

HTTP/1.1 200 OK
Content-Length: 308
Content-Type: text/html
Last-Modified: Tue, 27 May 2003 15:17:04 GMT
Accept-Ranges: bytes
ETag: "6291ea76324c31:5897"
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Thu, 15 May 2014 17:35:40 GMT
Connection: close

Inside this long answer, you should see many HTML tags, such as <html> and <head> tags. Inside the answer, you should also get the content of the page inside a <p> tag as follows:

<p>This is a very simple HTML file.</p>

If you can see this, congratulations! Your Ethernet shield is working correctly! Finally, the Arduino board will also display that the Ethernet shield has been disconnected from the remote server:

disconnecting

If everything worked correctly, it means that your Ethernet shield is working correctly, and it can connect without any problems to your local network and to the Web.

If something didn't work as expected, there are several things you can check. First, make sure that all connections are correctly made, and that the Ethernet cable is correctly plugged between the shield and your router.

If the DHCP fails at the beginning of the sketch and your shield can't get an IP address, please check whether DHCP is activated without limitations on the MAC addresses in the configuration panel of your router.

Finally, if the Arduino board can't connect to the remote server, first check whether the server itself is working by entering the URL of the test page manually in your browser.

Summary

In this first chapter of the book, we built our first project with the Arduino Ethernet shield, only to check whether the shield was working correctly and whether it could connect to your local network and to the Internet. We only made sure that the Ethernet shield could indeed be connected to the Internet, but this already gave you an overview of everything that you can do with the Ethernet shield. You saw how easy it is to send requests with the Ethernet shield, which is something we will use later in the book to send data from the Ethernet shield to a remote server.

These were the major takeaways from this first chapter:

  • First, we made sure that all the hardware was correctly set and that the shield was connected to your router via an Ethernet cable.
  • Then, we built the first sketch of this book simply to test that the Ethernet shield could indeed connect to the local network and the Web. To test this, we built a sketch that connects to the Web and grabs a test page from a remote server.
  • Finally, we uploaded this code to the board and checked that everything was working correctly by monitoring the status of the connection in the Arduino Serial Monitor. We also defined some strategies on what to do if the sketch didn't work and the shield could not connect to the Web.

In the next chapter of the book, we are going to build our first application using the Ethernet shield. We are also going to use an Ethernet client, but this time to connect to a server running on our own computer and to send measurements that come from a temperature and humidity sensor.

Left arrow icon Right arrow icon

Description

This book is intended for those who want to build their own network-connected projects using the Arduino platform. You will be able to build exciting projects that connect to your local network and the Web. You will need to have some basic experience in electronics and web programming languages. You will also need to know the basics of the Arduino platform as the projects mainly deal with the networking aspects of the Arduino Ethernet shield.

What you will learn

  • Interface the Ethernet shield with Arduino and to your Ethernet router
  • Learn the basics of the Arduino Ethernet library
  • Grab the content of a web page using the Ethernet shield
  • Measure data and send it to a server running on your computer
  • Create a data logging station with a web server running on the Ethernet shield
  • Send measured data to the cloud service Xively, so it can be monitored from anywhere
  • Discover how to use the Temboo web service to connect the Ethernet shield to more cloud services

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 21, 2014
Length: 118 pages
Edition : 1st
Language : English
ISBN-13 : 9781783986866
Vendor :
Arduino
Languages :
Concepts :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Aug 21, 2014
Length: 118 pages
Edition : 1st
Language : English
ISBN-13 : 9781783986866
Vendor :
Arduino
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 79.97
Arduino Home Automation Projects
€19.99
Arduino Networking
€21.99
Arduino Robotic Projects
€37.99
Total 79.97 Stars icon
Banner background image

Table of Contents

7 Chapters
1. Discover the Arduino Ethernet Shield Chevron down icon Chevron up icon
2. Sending Data to a Web Server Chevron down icon Chevron up icon
3. Data Logging Station Chevron down icon Chevron up icon
4. Controlling Objects from Anywhere Chevron down icon Chevron up icon
5. Internet of Things with Xively Chevron down icon Chevron up icon
6. Logging Data in Google Docs Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.9
(9 Ratings)
5 star 44.4%
4 star 11.1%
3 star 33.3%
2 star 11.1%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




RAUL LOPEZ Mar 23, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Really, great guide for beginning in networking with arduino
Amazon Verified review Amazon
Alison Taylor Dec 17, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Partner very impressed with this
Amazon Verified review Amazon
Steve Spence Nov 12, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
As much fun as it is connecting sensors to an Arduino, and displaying data on a LCD, lighting LED's, or other local actions, the real power is when you connect to a network. Whether it's a local network and you are communicating between them or collecting data in a central database (Raspberry Pi), or connecting to the internet and contributing local weather data to a server, or broadcasting data with email or Twitter, even pulling down information like emails, tweets, or scraping other websites for data for local display, there's a lot of fun to be had.One book that explains how this all works, and gives you easy to replicate (and understand) projects is "Arduino Networking" by Marco Schwartz. This book delves into the abilities of network connectivity, explaining how and why it works, and leaves your mind swirling with new applications, and the ability to execute them. It's a must have on any maker's shelf!
Amazon Verified review Amazon
Stefan Buttigieg Sep 10, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is explained in a very clear and step-wise way which makes it extremely easy for anyone to follow from the Arduino Hobbyist beginner to the most experienced. Mr.Schwartz has proven to be over and over again with the release of other books on this subject matter to be trustworthy.
Amazon Verified review Amazon
W Boudville Oct 10, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
For many of you Arduino enthusiasts, the project you work on will need a way to connect your hardware to a network. As a practical matter, that network is likely to be your local network or the Internet. This text gives a means, using the Arduino Ethernet shield. The slender nature of the book hints at how straightforward this can be.Given the hardware, the steps in the text's examples show how to upload data, including how to plot this in a rudimentary way on a terminal window. Back to basics indeed. For debugging, this makes the mininum needed. No fancy interactions with a web server. At least initially. But the text then goes onwards, to explain to to hook up to a web server. Chapter 3 actually makes the web on the Arduino motherboard. Impressive use of the memory and CPU now commonly available in Arduino !Another key variant is later in the book, when it shows how to use a web server on the Internet to remotely control your device. The advantages and uses are manifold. Albeit with the requirement that the device needs to be accessible from the Internet.Happy tinkering.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.