You are previewing Making Things Talk, 2nd Edition.

Making Things Talk, 2nd Edition

Cover of Making Things Talk, 2nd Edition by Tom Igoe Published by O'Reilly Media, Inc.
  1. Special Upgrade Offer
  2. Preface
    1. Who This Book Is For
    2. What You Need to Know
    3. Contents of This Book
    4. On Buying Parts
    5. Using Code Examples
    6. Using Circuit Examples
    7. Acknowledgments for the First Edition
    8. Note on the Second Edition
      1. Software Reference
      2. Hardware Reference
      3. Acknowledgments for the Second Edition
  3. 1. The Tools
    1. It Starts with the Stuff You Touch
    2. It’s About Pulses
    3. Computers of All Shapes and Sizes
    4. Good Habits
      1. Listen More Than You Speak
      2. Never Assume
      3. Agree on How You Say Things
      4. Ask Politely for Clarification
    5. Tools
      1. Physical Tools
      2. Software Tools
    6. Using the Command Line
      1. Controlling Access to Files
      2. Creating, Viewing, and Deleting Files
      3. Hardware
      4. Connecting Components to the Module
    7. Using an Oscilloscope
    8. It Ends with the Stuff You Touch
  4. 2. The Simplest Network
    1. Supplies for Chapter 2
    2. Layers of Agreement
    3. Making the Connection: The Lower Layers
    4. Saying Something: The Application Layer
      1. Project 1: Type Brighter
      2. The Protocol
    5. Complex Conversations
      1. Project 2: Monski Pong
    6. Flow Control
      1. Project 3: Wireless Monski Pong
    7. Finishing Touches: Tidy It Up, Box It Up
      1. Project 4: Negotiating in Bluetooth
    8. Conclusion
  5. 3. A More Complex Network
    1. Supplies for Chapter 3
    2. Network Maps and Addresses
      1. Network Maps: How Things Are Connected
      2. Hardware Addresses and Network Addresses
      3. Packet Switching: How Messages Travel the Net
      4. Clients, Servers, and Message Protocols
      5. How Web Browsing Works
      6. HTTP GET and POST
      7. How Email Works
      8. Project 5: Networked Cat
      9. Putting Sensors in the Cat Mat
      10. Sending Mail from the Cat
      11. Making a Web Page for the Cat Cam
      12. Uploading Files to a Server Using PHP
      13. Capturing an Image and Uploading It Using Processing
      14. Putting It All Together
      15. One Final Test
    3. Conclusion
  6. 4. Look, Ma, No Computer! Microcontrollers on the Internet
    1. Supplies for Chapter 4
    2. Introducing Network Modules
      1. Project 6: Hello Internet!
    3. An Embedded Network Client Application
      1. Project 7: Networked Air-Quality Meter
    4. The Finished Project
    5. Programming and Troubleshooting Tools for Embedded Modules
      1. The Three Most Common Mistakes
      2. Diagnostic Tools and Methods
    6. Conclusion
  7. 5. Communicating in (Near) Real Time
    1. Supplies for Chapter 5
    2. Interactive Systems and Feedback Loops
    3. Transmission Control Protocol: Sockets & Sessions
      1. Project 8: Networked Pong
      2. A Test Chat Server
    4. The Clients
    5. Conclusion
  8. 6. Wireless Communication
    1. Supplies for Chapter 6
    2. Why Isn’t Everything Wireless?
    3. Two Flavors of Wireless: Infrared and Radio
      1. Transmitters, Receivers, and Transceivers
      2. How Infrared Works
      3. Project 9: Infrared Control of a Digital Camera
    4. How Radio Works
      2. Project 10: Duplex Radio Transmission
      3. Step 1: Configuring the XBee Modules Serially
      4. Step 2: Programming the Microcontroller to Use the XBee Module
      5. Step 3: Two-Way Wireless Communication Between Microcontrollers
      6. Project 11: Bluetooth Transceivers
      7. Step 1: The Circuits
      8. Step 2: Getting to Know the Commands
      9. Step 3: Connecting Two Bluetooth Radios
      10. Step 4: Connecting Two Microcontrollers via Bluetooth
    5. Buying Radios
    6. What About WiFi?
      1. Project 12: Hello WiFi!
      2. WiFi Diagnostics
    7. Conclusion
  9. 7. Sessionless Networks
    1. Supplies for Chapter 7
    2. Sessions vs. Messages
      1. Sessions Versus Messages
    3. Who’s Out There? Broadcast Messages
      1. Querying for Other Devices Using UDP
      2. Querying for XBee Radios Using 802.15.4 Broadcast Messages
      3. Project 13: Reporting Toxic Chemicals in the Shop
      4. What Happens in the Subnet Stays in the Subnet
    4. Directed Messages
      1. Sending Directed UDP Datagrams
      2. Project 14: Relaying Solar Cell Data Wirelessly
    5. Conclusion
  10. 8. How to Locate (Almost) Anything
    1. Supplies for Chapter 8
    2. Network Location and Physical Location
      1. Step 1: Ask a Person
      2. Step 2: Know the Environment
      3. Step 3: Acquire and Refine
    3. Determining Distance
      1. Passive Distance Ranging
      2. Project 15: Infrared Distance Ranger Example
      3. Project 16: Ultrasonic Distance Ranger Example
      4. Active Distance Ranging
      5. Project 17: Reading Received Signal Strength Using XBee Radios
      6. Project 18: Reading Received Signal Strength Using Bluetooth Radios
      7. The Multipath Effect
    4. Determining Position Through Trilateration
      1. Project 19: Reading the GPS Serial Protocol
    5. Determining Orientation
      1. Project 20: Determining Heading Using a Digital Compass
      2. Project 21: Determining Attitude Using an Accelerometer
      3. Determining Pitch and Roll from an Accelerometer
    6. Conclusion
  11. 9. Identification
    1. Supplies for Chapter 9
    2. Physical Identification
      1. Video Identification
      2. Project 22: Color Recognition Using a Webcam
      3. Project 23: Face Detection Using a Webcam
      4. Project 24: 2D Barcode Recognition Using a Webcam
      5. Radio Frequency Identification (RFID)
      6. Project 25: Reading RFID Tags in Processing
      7. Project 26: RFID Meets Home Automation
      8. The Circuit
      9. Construction
      10. Project 27: Tweets from RFID
      11. The Circuit
      12. SonMicro Communications Protocol
      13. Writing to Mifare Tags
      14. Reading from Mifare Tags
      15. Circuit Additions
      16. Saving Program Memory
      17. Troubleshooting
      18. Construction
    3. Network Identification
      1. HTTP Environment Variables
      2. Project 28: IP Geocoding
      3. Mail Environment Variables
    4. Conclusion
  12. 10. Mobile Phone Networks and the Physical World
    1. Supplies for Chapter 10
    2. One Big Network
      1. A Computer in Your Pocket
      2. Start with What Happens
      3. Project 29: CatCam Redux
      4. The Circuit
      5. The Code
      6. Writing to an SD Card
      7. Good SD Card Practice
      8. Making Your Server Public
      9. Dynamic DNS
      10. Network Cameras
      11. Project 30: Phoning the Thermostat
      12. What’s the Standard?
      13. A Brief Introduction to XML
      14. Getting the Content Length Right
      15. HTML5 and Other Mobile Web Approaches
      16. PhoneGap
    3. Text-Messaging Interfaces
    4. Native Applications for Mobile Phones
      1. Processing for Android
      2. Setting Up Processing for Android
      3. Project 31: Personal Mobile Datalogger
      4. The Circuit
      5. The Construction
      6. What About USB?
    5. Conclusion
  13. 11. Protocols Revisited
    1. Supplies for Chapter 11
    2. Make the Connections
      1. Know Your Options at the Physical Level
      2. Picking a Serial Protocol
      3. Plan the Physical System and Information Flow Early
    3. Text or Binary?
      1. Isn’t All Data Binary?
      2. Interpreting a Binary Protocol
    4. MIDI
      1. Project 32: Fun with MIDI
      2. DMX512
      3. The Structure and Syntax of Text-Based Protocols
      4. Markup vs. Programming Languages
    5. Representational State Transfer
      1. A Traditional Web Service
      2. A Web-Based Device
      3. Project 33: Fun with REST
    6. Conclusion
  14. A. Where to Get Stuff
    1. Supplies
    2. Hardware
    3. Software
  15. Index
  16. About the Author
  17. Special Upgrade Offer
  18. Copyright

Chapter 4. Look, Ma, No Computer! Microcontrollers on the Internet

image with no caption

Uncommon Projects’ YBox ( puts RSS feeds on your TV using an XPort serial-to-Ethernet module and a Propeller microchip. Image courtesy of Uncommon Projects.

The first response that comes to many people’s minds after building a project like the networked cat bed in Chapter 3 is: “Great, but how can I do this without needing to connect to my computer?” It’s cumbersome to have to attach the microcontroller to a laptop or desktop computer just to enable it to connect to the Internet. After all, as you saw in Chapter 3, Internet message protocols are just text strings, and microcontrollers are good at sending short text strings. So, in this chapter, you’ll learn how to connect a microcontroller to the Internet through a device that’s not much more complex than the Bluetooth radio modem you used in Chapter 2.

In the past few years, a wide array of commercial appliances has come on the market that can connect directly to the Internet without the aid of a personal computer. Companies like D-Link, Sony, Axis, and others make security cameras with network interfaces, both Ethernet and WiFi. Ceiva, eStarling, and others make picture frames with WiFi connections to which you can upload images from the Net. Ambient Devices makes lamps and displays of various sorts that connect to the Net and change their appearance based on changes in information, such as stock market data, weather, and other scalar quantities. Cable television set-top boxes are computers in a small box, capable of routing streams of audio, video, and data all at the same time. In fact, the operating system in your set-top box might even be a variation of the same Linux operating system that’s running on your network provider’s web hosting machine. Home alarm systems are made up of networks of microcontrollers that talk among themselves, with one that can communicate with a central server, usually over phone lines using a modem.

All of these appliances engage in networked communication. The simplest handle only one transaction at a time, requesting information from a server and then waiting for a response, or sending a single message in response to some physical event. Others manage multiple streams of communication at once, allowing you to surf the Web while watching television. The more processing power a given device has, the more it can handle. For many applications, however, you don’t need a lot of processing power, because the device you’re making has only one or two functions.

New parts for this chapter: 1. Arduino Ethernet shield—this one has a Power over Ethernet module on board 2. Photocells 3. Red, green, and blue lighting filter gels 4. Voltmeter. You can use an off-the-shelf one, but it’s better if you can find one that’s antique. Don’t forget plenty of male header pins for the breakout boards.
Figure 4-1. New parts for this chapter: 1. Arduino Ethernet shield—this one has a Power over Ethernet module on board 2. Photocells 3. Red, green, and blue lighting filter gels 4. Voltmeter. You can use an off-the-shelf one, but it’s better if you can find one that’s antique. Don’t forget plenty of male header pins for the breakout boards.

Supplies for Chapter 4


PROJECT 6: Hello Internet! Daylight Color Web Server

  • 1 Arduino Ethernet board A A000050 Alternatively, an Uno-compatible board (see Chapter 2) with an Ethernet shield will work.

    SF DEV-09026, J 2124242, A A000056, AF 201, F 1848680

  • 1 Ethernet connection to the Internet Your home router most likely has Ethernet jacks in the back. If you’ve hooked up your computer to the Internet using Ethernet, you know where the ports are.

  • 3 10-kilohm resistors D 10KQBK-ND, J 29911, F 9337687, RS 707-8906

  • 3 photocells (light-dependent resistors) D PDV-P9200-ND, J 202403, SF SEN-09088, F 7482280, RS 234-1050

  • 1 solderless breadboard D 438-1045-ND, J 20723 or 20601, SF PRT-00137, F 4692810, AF 64, SS STR101C2M or STR102C2M, MS MKKN2

  • 3 lighting filters One primary red, one primary green, and one primary blue. Available from your local lighting- or photo-equipment supplier.

PROJECT 7: Networked Air-Quality Meter

  • 1 Arduino Ethernet board A A000050 Alternatively, an Uno-compatible board (see Chapter 2) with an Ethernet shield will work.

    SF DEV-09026, J 2124242, A A000056, AF 201, F 1848680

  • 1 Ethernet connection to the Internet Your home router most likely has Ethernet jacks in the back. If you’ve hooked up your computer to the Internet using Ethernet, you know where the ports are.

  • 1 solderless breadboard D 438-1045-ND, J 20723 or 20601, SF PRT-00137, F 4692810, AF 64, SS STR101C2M or STR102C2M, MS MKKN2

  • 1 voltmeter Get a nice-looking antique one if you can. Ideally, you want a meter that reads a range from 0–5V, or 0–10V at most.

    SF TOL-10285, F 4692810, RS 244-890

  • 4 LEDs D 160-1144-ND or 160-1665-ND, J 34761 or 94511, F 1015878, RS 247-1662 or 826-830, SF COM-09592 or COM-09590

  • 4 220-ohm resistors D 220QBK-ND, J 690700, F 9337792, RS 707-8842

Introducing Network Modules

It’s possible to write a program for a microcontroller that can manage all the steps of network communication, from the physical and data connections to the network address management to the negotiation of protocols like SMTP and HTTP. A code library that encompasses all the layers needed for network connections is called a network stack, or TCP/IP stack. However, it’s much easier to use a network interface module to do the job.

There are many such modules on the market, with varying prices and features. Just as you can choose how technical you want to get when you pick a microcontroller platform, you can also choose your technical level when you select a network controller. Some modules—like Rabbit Semiconductor’s RabbitCore processors—come with all the source code for a TCP/IP stack, and expect you to modify it for your needs and program the device yourself. Others, like the Beagle Board, are a full network computer on a single circuit board. These are very powerful, and if you’re an experienced network programmer, they present a very comfortable programming environment. However, they don’t make it easy to add sensors and actuators, and for people not experienced with network programming, they present a steep learning curve. Others—like Lantronix’ modules, the XPort, XPort Direct, MatchPort, and WiPort—have a stack programmed into their firmware, and present you with a serial, telnet, or web-based interface. These are much simpler to use. The web interface gives you access from the browser of your personal computer; the telnet interface gives you access from a server or your personal computer; and the serial interface gives you access from a microcontroller. These are serial-to-Ethernet modems. They work much like the Bluetooth modems you used in Chapter 2, but they have a different serial protocol. These are a comfortable beginning place, and I used them extensively in the first edition of this book. For this edition, however, you’ll learn about Ethernet modules that have a synchronous serial interface. These offer the ease-of-use of the Serial-to-Ethernet modules, but they don’t take up an asynchronous serial port on your microcontroller. It means you can still use the serial port for debugging or communicating with other devices.

The Arduino Ethernet (left), an Arduino Ethernet shield (center), and an Ethernet shield with power-over-Ethernet module attached (right). You can use any of these for the Ethernet projects in this book.
Figure 4-2. The Arduino Ethernet (left), an Arduino Ethernet shield (center), and an Ethernet shield with power-over-Ethernet module attached (right). You can use any of these for the Ethernet projects in this book.

There are two options for Ethernet connections to an Arduino. One is the Arduino Ethernet board, which is an Arduino board that has an Ethernet module on the board itself. The Ethernet connector replaces the USB connector found on the standard Arduinos. To program this board, you also need an FTDI-style USB-to-Serial adapter. The other option is an add-on board for regular Arduino modules, called an Ethernet shield. There are a few versions of it on the market. The Arduino Ethernet shield uses an Ethernet chip from WizNet, the W5100. This shield has a built-in SD memory card slot as well, like the Arduino Ethernet board. The Adafruit Ethernet shield also uses the W5100 chip, or it can use the Lantronix serial-to-Ethernet modules mentioned earlier. The projects in this chapter will work with the Arduino Ethernet, with the Arduino Ethernet shield, or with the Adafruit Ethernet shield with the W5100 module, which are shown in Figure 4-2. They will not work with the Lantronix module, however.

Project 6: Hello Internet!

To use any network module, you first need to connect it to the network. That’s the goal here. In this project, you’ll make a very simple web server on your Arduino that serves a web page whose background color changes with the color of the light where the Arduino is located.

Making the Connections

If you’re using the Arduino Ethernet board, just plug it into a USB-to-Serial adapter and it will show up in the list of serial ports like a regular Arduino. The USB-to-Serial adapter will supply it with power like a normal USB cable. Programming it is the same as with any other Arduino, but in the Tools→Board menu, choose Arduino Uno or Arduino Ethernet.

The Ethernet shield, like many shields for the Arduino, is very easy to connect to your controller. Just plug it into the board. Both the Ethernet module and the shield use pins 10, 11, 12, and 13 for communication to the Ethernet controller, and pins 4, 11, 12, and 13 for communication to the SD card, so you can’t use those pins for other inputs or outputs. Connect the Ethernet module to your router using an Ethernet cable. Figure 4-3 shows the Arduino Ethernet shield’s connections to the microcontroller.

You can use the Arduino Ethernet or Ethernet ahield with a standard Arduino interchangeably, so from here on out, I’ll just use “Ethernet module” to refer to either.

The connections between the Ethernet shield and the Arduino controller when they’re stacked together.
Figure 4-3. The connections between the Ethernet shield and the Arduino controller when they’re stacked together.

To read the color of the ambient light, use three photocells and cover each one with a different color of lighting filter: red, green, and blue. You can get these at many lighting-supply stores, photo supply-stores, or art stores, or you can use any translucent colored plastic you have around the house. You don’t have to be scientific here. Figure 4-4 shows the connection between the Arduino Ethernet module and the three photocells.

How the Ethernet Library Works

To begin, you’re going to try a couple simple programs using the Ethernet library for Arduino. This library lets you control the Ethernet module using methods similar to those you use for printing to a serial port. There are two types of software objects you’ll make: servers and clients. A server waits for connections from remote devices on the Internet and allows them to connect to the Ethernet module, just like regular servers. A client initiates a connection to remote devices, makes requests, and delivers the replies, just like a regular client. Both servers and clients can be read from and written to, using the read(), write(), print(), and println() commands that you’ve already seen with the Serial library. There’s also an available() command, as in the Serial library, to see whether there’s any new data available from the server or client in question. The client object also has a connected() command that tells you whether it’s connected to a remote server. This will be useful later, when you’re trying to connect to a remote server to get data. Here, you’ll get started with a simple server sketch.

The RGB server circuit. The connections are the same for either the Arduino Ethernet or the Arduino and Ethernet shield combo. Note the three color filters over the photocells.
The RGB server circuit. The connections are the same for either the Arduino Ethernet or the Arduino and Ethernet shield combo. Note the three color filters over the photocells.
Figure 4-4. The RGB server circuit. The connections are the same for either the Arduino Ethernet or the Arduino and Ethernet shield combo. Note the three color filters over the photocells.

Before you can write your program, you need to establish some basic information about how your Ethernet module will connect to the Internet. Just as you did in Chapter 3, you’ll need the device’s Media Access Control (MAC) address. That’s the hardware address of your Ethernet controller. The Arduino Ethernet modules have a six-byte address on the back, written in hexadecimal notation, that you can use. If the sticker’s missing for any reason, you can make up your own MAC address, or use the generic one you find in the examples below. You’ll also need to know the router’s address (aka the gateway address because your router is the gateway to the rest of the Internet), as well as the address that your device will use on the router’s subnet.

Your device’s IP address will be similar to your router’s address, probably using the same three numbers for the start of the address, but a different number for the last. For example, if your router’s local address is, you can use an address like for your Ethernet module—as long as no other device connected to the router is using the same address.

When a router assigns addresses to its connected devices, it masks part of the address space so that those devices can use only addresses in the same subnet as the router itself. For example, if the router is going to assign only addresses in the range through, it masks out the top three numbers (octets). This is called the netmask, or subnet mask. In your PC’s network settings, you’ll see it written as a full network address, like so: With the Ethernet module, you’ll assign it similarly.

Once you know your MAC address, your router’s address, your IP address, and your subnet mask, you’re ready to go.

Run this sketch with the Ethernet module attached to your router and the Serial Monitor open. Then open a browser window and go to the Arduino’s address. Using the example as shown, you’d go to You won’t see anything in the browser, but you will see the HTTP request come through in the Serial Monitor. Now you’re seeing what the server saw when you made HTTP requests in Chapter 3. A typical request will look like this:

GET / HTTP/1.1
Connection: keep-alive
Accept: application/xml,application/xhtml+xml,text/
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X
10_6_5; en-US) AppleWebKit/534.10 (KHTML, like Gecko)
Chrome/8.0.552.215 Safari/534.10
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

You can ignore most of the HTTP parameters, but two pieces are useful: the first line, where you see what the client is requesting; and the end, where it sends a blank line to finish the request. In this case, the first line shows that the client is asking for the main index page at the root of the server, signified by the / in GET /. You’re only going to have one result to return, but in the future, you will write a more complex server that looks at this part of the request and responds with different results depending on what’s requested.

For now, you can keep it simple and just look for the end of the request, which will be a linefeed (\n or ASCII 10), followed by a carriage return (\r or ASCII 13), followed by another linefeed. Figure 4-5 shows what happens in the code that follows.

Logic flow for a simple server.
Figure 4-5. Logic flow for a simple server.

When you enter the Arduino’s address in your browser now, you’ll get a web page. There’s not much there, but it’s legitimate enough that your browser doesn’t know the difference between your Arduino and any other web server. You can add anything you want in the HTML in the makeResponse() method—even links to images and content on other servers. Think of the Arduino as a portal to any other content you want the user to see, whether it’s physical data from sensors or other web-based data. The complexity is really up to you. Once you start thinking creatively with the capabilities of the print() and println() statements, you get a wide range of ways to dynamically generate a web interface to your Arduino through the Ethernet shield.

An Embedded Network Client Application

Now that you’ve made your first server, it’s time to make a client. This project is an embedded web scraper. It takes data from an existing website and uses it to affect a physical output. It’s conceptually similar to devices made by Ambient Devices, Nabaztag, and others—but it’s all yours.

Project 7: Networked Air-Quality Meter

In this project, you’ll make a networked air-quality meter. You’ll need an analog panel meter, like the kind you find in speedometers and audio VU meters. I got mine at a yard sale, but you can often find them in electronics surplus stores or junk shops. The model recommended in the parts list is less picturesque than mine, but it will do for a placeholder until you find one you love.

Figure 4-8 shows how it works: the microcontroller makes a network connection to a PHP script through the Ethernet shield. The PHP script connects to another web page, reads a number from that page, and sends the number back to the microcontroller. The microcontroller uses that number to set the level of the meter. The web page in question is AIRNow,, the U.S. Environmental Protection Agency’s site for reporting air quality. It reports hourly air quality status for many U.S. cities, listed by ZIP code. When you’re done, you can set a meter from your home or office to see the current air quality in your city (assuming you live in the U.S.).

Control the Meter Using the Microcontroller

First, you need to generate a changing voltage from the microcontroller to control the meter. Microcontrollers can’t output analog voltages, but they can generate a series of very rapid on-and-off pulses that can be filtered to give an average voltage. The higher the ratio of on-time to off-time in each pulse, the higher the average voltage. This technique is called pulse-width modulation (PWM). In order for a PWM signal to appear as an analog voltage, the circuit receiving the pulses has to react much more slowly than the rate of the pulses. For example, if you pulse-width modulate an LED, it will seem to be dimming because your eye can’t detect the on-off transitions when they come faster than about 30 times per second. Analog voltmeters are very slow to react to changing voltages, so PWM works well as a way to control these meters. By connecting the positive terminal of the meter to an output pin of the microcontroller, and the negative pin to ground, and pulse-width modulating the output pin, you can easily control the position of the meter. Figure 4-9 shows the whole circuit for the project.

The networked air-quality meter.
Figure 4-8. The networked air-quality meter.
The circuit for a networked meter. The Ethernet controller shown in the schematic is on the shield or the Arduino Ethernet board.
The circuit for a networked meter. The Ethernet controller shown in the schematic is on the shield or the Arduino Ethernet board.
Figure 4-9. The circuit for a networked meter. The Ethernet controller shown in the schematic is on the shield or the Arduino Ethernet board.

Write a PHP Script to Read the Web Page

Next, you need to get the data from AIRNow’s site in a form the microcontroller can read. The microcontroller can read in short strings serially, and converting those ASCII strings to a binary number is fairly simple. Using a microcontroller to parse through all the text of a web page is possible, but a bit complicated. However, it’s the kind of task for which PHP was made. The program that follows reads the AIRNow page, extracts the current air-quality index (AQI) reading, and makes a simpler summary page that’s easy to read with the microcontroller. The Ethernet controller is the microcontroller’s gateway to the Internet, allowing it to open a TCP connection to your web host, where you will install this PHP script.


You could also run this script on one of the computers on your local network. As long as the microcontroller is connected to the same network, you’ll be able to connect to it and request the PHP page. For information on installing PHP or finding a web-hosting provider that supports PHP, see

Figure 4-10 shows AIRNow’s page for New York City ( AIRNow’s page is formatted well for extracting the data. The AQI number is clearly shown in text, and if you remove all the HTML tags, it appears on a line by itself, always following the line Current Conditions.


One of the most difficult things about maintaining applications like this, which scrape data from an existing website, is the probability that the designers of the website could change the format of their page. If that happens, your application could stop working, and you’ll need to rewrite your code. In fact, it happened between the first and second editions of this book. This is a case where it’s useful to have the PHP script do the scraping of the remote site. It’s more convenient to rewrite the PHP than it is to reprogram the microcontroller once it’s in place.

AIRNow’s page is nicely laid out for scraping. The PHP program used in this project ignores the ozone level.
Figure 4-10. AIRNow’s page is nicely laid out for scraping. The PHP program used in this project ignores the ozone level.

Read the PHP Script Using the Microcontroller

Next, it’s time to connect to the PHP script through the Net using the Ethernet module. This time, you’ll use the shield as a client, not a server. Before you start programming, plan the sequence of messages. Using the Ethernet module as a network client is very similar to using Processing as a network client. In both cases, you have to know the correct sequence of messages to send and how the responses will be formatted. You also have to write a program to manage the exchange of messages. Whether you’re writing that program in Processing, in Arduino, or in another language on another microcontroller, the steps are still the same:

  1. Open a connection to the web server.

  2. Send an HTTP GET request.

  3. Wait for a response.

  4. Process the response.

  5. Wait an appropriate interval and do it all again.

A flowchart of the Arduino program for making and processing an HTTP GET request.
Figure 4-11. A flowchart of the Arduino program for making and processing an HTTP GET request.

Figure 4-11 is a flowchart of what happens in the microcontroller program. The major decisions (if statements in your code) are marked by diamonds; the methods are marked by rectangles. Laying out the whole program in a flowchart like this will help you keep track of what’s going on at any given point. It also helps you to see what methods depend on a particular condition being true or not.

The circuit for this project also uses LEDs to keep track of the state of the program. LEDs attached to I/O pins will indicate the state. There’s an LED to indicate that it’s connected, another to indicate that it’s disconnected, a third to indicate if it got a valid reading, and a fourth to indicate that the microcontroller is resetting.

This program will check the PHP script every two minutes. If there’s a new value for the air quality, it’ll read it and set the meter. If it can’t get a connection, it will try again two minutes later. Because it’s a client and not a server, there’ s no web interface to the project, only the meter.

TextFinder Library

For this sketch, you’re going to need Michael Margolis’ TextFinder library for Arduino. Download it from, unzip it, and save the TextFinder folder to the libraries folder of your Arduino sketches directory (the default location is Documents/Arduino/libraries/ on OS X, My Documents\Arduino\libraries\ on Windows 7, and ~/Documents/Arduino/libraries/ on Ubuntu Linux. If the libraries directory doesn’t exist, create it and put TextFinder inside. Restart Arduino, and the TextFinder library should show up in the Sketch→Import Library menu. TextFinder lets you find a substring of text from the incoming stream of bytes. It’s useful for both Ethernet and serial applications, as you’ll see.

TextFinder was modified and included in version 1.0.1 of Arduino, after this edition was published. You can find its methods in the Stream class, from which Client, Server, and Serial inherit methods. For alternate versions of these sketches that use Stream instead of TextFinder, see the GitHub repository for this book’s code, at Check the Reference section of for details of the Stream class.

The Finished Project

The completed networked air-quality meter.
Figure 4-12. The completed networked air-quality meter.

Programming and Troubleshooting Tools for Embedded Modules

You may have hit a number of problems when making the connections in the last section. Probably the most challenging thing about troubleshooting them was that neither the Ethernet shield nor the microcontroller gave any indication that a problem occurred. This is the norm when you’re working with embedded modules that you build yourself. This section covers a few things you should always check, and a few tools that will help you solve problems. These principles apply whether you’re using the Ethernet modules or some other network or communications module. You’ll use these methods over and over again in the rest of the book and beyond.

The Three Most Common Mistakes

Check Power and Ground

Always check whether you have made the power and ground connections correctly. This is less of an issue when you’ve got a nice plug-in module like the Ethernet shield that can connect only one way. But even with plug-in modules, you can get a pin misaligned and not notice. If you’re lucky, the module you’re using will have indicator LEDs that light up when it’s working properly. Whether it does or not, check the voltage between power and ground with a meter to make sure you’ve got it powered correctly.

Check the Connections

When you’re wiring a module to a microcontroller by hand, it’s fairly common to get the wires wrong the first time. Make sure you know what each pin does, and double-check that the pin that one transmits on connects to the pin that the other receives on, and vice versa. If it’s a synchronous serial connection, make sure the clock is connected.

Check the Configuration

If you’re certain about the hardware connections, check the device’s configuration to make sure it’s all correct. Did you get the IP address correct? Is the router address correct? Is the netmask?

Diagnostic Tools and Methods

Once you know the device is working, you have to program the sequence of messages that constitutes your application. Depending on the application’s needs, this sequence can get complex, so it’s useful to have a few simple programs around to make sure things work as desired.

It’s a good idea to test your module first using sample code—if it’s provided. Every good communications module maker gives you a starting example. Use it, and keep it handy. When things go wrong, return to it to make sure that basic communications still work.

Physical Debugging Methods

Writing code makes things happen physically. It’s easy to forget that when you’re working on an exchange of information like the examples shown here. So, it’s helpful to put in physical actions that you can trigger; this way, you can see that your code is working. In the last project, you saw LEDs turn on when the client connected or disconnected, when it made a successful request, and when it reset. Triggering an LED is the most basic and reliable thing you can do from a microcontroller—even more basic than sending a serial debugging message—so use LEDs liberally to help make sure each part of your code is working. You can always remove them later.

Serial Debugging Methods

Besides their physical form factor, the Ethernet shield and Arduino Ethernet make a number of things easy for you. For example, the fact that they use synchronous serial communication to talk to the Ethernet controller means you can still use the serial port to get messages about what’s going on—as you saw in the previous example. Following are a few tips for effective serial debugging of networking code using serial messages.

It’s important to keep in mind that serial communication takes time. When you have serial statements in your code, they slow it down. The microcontroller has to save each bit of information received, which takes time away from your program. Even though the time is miniscule for each bit, it can add up. After a few hundred bytes, you might start to notice it. So, make sure to take out your serial transmission statements when you’re done with them.

Write a Test Client Program

It’s easiest to work through the steps of the program if you can step through the sequence of events. More complex development environments allow you to step through a program one line at a time. Arduino doesn’t give you that ability, but it can link to other environments. The following program simply passes anything that comes in the serial port to the Ethernet port, and vice versa. It turns your Arduino into a serial-to-Ethernet gateway. Using this code, you can connect to a serial terminal or to Processing—or to any other development environment that can communicate serially—to test the Ethernet connection.

Starting with this sketch as a base, you could work out the whole logic of the HTTP exchange in Processing, then convert your sketch into an Arduino sketch.

This approach allows you to work out the logical flow of an application in an environment with which you might be more familiar. It removes the complications of the microcontroller environment, allowing you to concentrate on the sequence of messages and the actions that trigger them.

Don’t get too caught up in the technical details of your program when working this way. You’re still going to have to deal with the details of the microcontroller program eventually. However, it often helps to see the similarities and differences between two languages when you’re working on a problem. It helps you concentrate on the logic that underlies them.

Write a Test Server Program

The previous program allowed you to connect to a remote server and test the exchange of messages. The remote server was beyond your control, however, so you can’t say for sure that the server ever received your messages. If you never made a connection, you have no way of knowing whether the module can connect to any server. To test this, write your own server program to which it can connect.


The activities in this chapter show a model for networked objects that’s very flexible and useful. The object is basically a browser or a server, requesting information from the Web and extracting the information it needs, or delivering information to a client. You can use these models in many different projects.

The advantage of these models is that they don’t require a lot of work to repurpose existing web applications. At most, you need to write a variation of the PHP web scraper from earlier in this chapter to summarize the relevant information from an existing website. This flexibility makes it easier for microcontroller enthusiasts who aren’t experienced in web development to collaborate with web programmers, and vice versa. It also makes it easy to reuse others’ work if you can’t find a willing collaborator.

The model has its limits, though, and in Chapter 5, you’ll see some ways to get around those limits with a different model. Even if you’re not using this model, don’t forget the troubleshooting tools mentioned here. Making simple mock-ups of the programs on either end of a transaction can make your life much easier. This is because they let you see what should happen, and then modify what actually is happening to match that.

The best content for your career. Discover unlimited learning on demand for around $1/day.