Ingredients
Special items
Optional items
Until now, your smart objects sensed things nearby and communicated with you with blinking lights or numbers on your screen. We can make the Arduino even smarter by letting it surf the Internet! Once online, it can nab weather reports, monitor your environment, and even send emails at the push of a button!
To get online, we need to get your Arduino on to your home wifi network. And to do that we need to add some wifi parts to your Arduino. We do that with a new circuit board about the same size as an Arduino that piggybacks on top of your Arduino. In Arduino-speak, these add-on boards are known as “shields.” I like to think that shields give Arduinos superpowers.
This particular superpower will be key to the rest of the book; all of the other projects use this board for exciting jobs. So let’s take a moment to get one of these little guys up and running.
The SparkFun WiFi Shield is the key link between your Arduino and the Internet. There are links for ordering the wifi board, and the rest of these parts, at http://keefe.cc/arduino-online.
As with the “Electric Candle” project, we need to solder some headers onto the wifi board. Again, soldering is a whole lot easier than you might think. And there are some great tutorials online, complete with videos. One of my favorite guides is here: http://keefe.cc/soldering.
To help solder the headers on this board, I made a little mounting platform using parts in the “Optional items” section under “Ingredients”—a set of “Break Away Headers.” Note that unlike the stackable headers, these are just pins; they don’t have any holes.
You can break these headers into smaller sections (hence the name). I broke off four groups of five pins and pushed the long ends of each group into the breadboard at rows 1 and 20.
When you’re done, it’ll look like this:
Then I line up the set stackable headers, their pins pointing up, onto the breadboard setup in the same pattern as holes on the wifi shield—if the shield is facing down. That pattern looks like Figure 10-4
Now the pins correspond to the holes on the face-down wifi shield. Note that we’re ignoring the holes on the “middle” end of the wifi shield.
Slide the board onto the pins, making sure the side with the electronic components is facing down toward the breadboard.
You’ve accomplished all of the wiring with your awesome soldering job. Nice work.
Before we put the wifi board and the Arduino together, place a small piece of tape on top of the Arduino’s USB port—the shiny box you plug the USB cable into. That box actually is connected to “ground,” and we want to keep that separated from the metal contacts on the underside of the wifi board so it doesn’t accidentally complete a circuit we don’t want.
Electrical tape tends to stick to metal better, but any tape will do the trick.
Then carefully press the shield straight down onto your Arduino, making sure all of the pins from the wifi board go into all of the pins on the Arduino.
Then connect your Arduino to your computer to load up the code.
As we’ve seen before, in the “A Gentle Touch” project, sometimes we need to add to Arduino’s knowledge, and that’s often true when we add a new part like this. So we’ll need the “library” for the wifi shield.
Remember that a “library” isn’t a comprehensive as it sounds. It is a little bit of code containing commands for a particular task. Here’s how to get the library for the wifi board.
SparkFun_ESP8266_AT_ArduinoLibrary-master.zip
. It will probably end up in your “Downloads” folder. Don’t unzip it!SparkFun_ESP8266_AT_ArduinoLibrary-master.zip
(it’s very likely in your “Downloads” folder).SparkFun_ESP8266_AT_Arduino_Library-master.zip
and click the “Choose” button.Fortunately, the library you just installed also comes with the code we need to get up and running. Start from the Arduino software menu bar and select File → Examples → SparkFun ESP8266 AT Library → ESP8266_Shield_Demo.
To make this code work, though, you need to add some information about your own wifi network. Look for this section of the code:
//////////////////////////////
// WiFi Network Definitions //
//////////////////////////////
// Replace these two character strings with the name and
// password of your WiFi network.
const char mySSID[] = "yourSSIDhere";
const char myPSK[] = "yourPWDhere";
Replace the yourSSIDhere
with the name of your wifi network, such as “Keefe Family WiFi,” and replace the yourPWDhere
with the password for your network. Be sure to keep the double quotes around each.
Save your sketch using File → Save. (You’ll be prompted to save it with a new name.)
Before you upload your sketch to the Arduino, look for the little switch on the surface of the wifi board marked “UART.” If it’s not already, slide it to “SW.”
Now upload your sketch using Sketch → Upload or the arrow button at the top of the window.
It’s the Moment of Truth!
Open the Serial Monitor window with Tools → Serial Monitor. You should see
Press any key to begin.
That’s not quite true. You need to put your cursor in the input box at the top of the Serial Monitor window and then press Return
or Enter
to begin. Do this any time the program says “Press any key.”
If everything is in order, you’ll see
ESP8266 Shield Present
Mode set to station
Connected to: [Your wifi network name]
My IP: 192.168.1.2
Press any key to connect client.
. . . (and a whole bunch of other text) . . .
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is established to be used for illustrative examples in
documents. You may use this domain in examples without prior
coordination or asking for permission.</p>
<p><a href="http://www.iana.org/domains/example">More information. . .</a></p>
</div>
</body>
</html>
0,CLOSED
Press any key to test server.
That text scrolling by actually lives on the Internet, on a site called “example.com”—so your Arduino is connected to the Internet! You are ready for the next chapters.
If that didn’t happen, check out the “Fixes” below.
If you’re having trouble getting your Arduino on the Internet, try these steps:
Press any key to begin
but didn’t get to see all the scrolling text after you pressed Return
. Sometimes it takes a couple of attempts to get online.Just like a laptop computer, the Arduino now has wifi capabilities, which gives it Internet capabilities. The steps it is taking are, essentially:
Arduino → WiFi board → WiFi network → The Internet → example.com
Once it gets there, example.com sends a web page back. Viewed by the Arduino, it’s not all nice and pretty, like when you go to http://example.com in a browser:
Instead, you’re seeing the underlying text, in Hypertext Markup Language (HTML) that your browser turns pretty. Still, you’re seeing the same web page.
Okay, there’s a lot going on in this sketch, and you’ve already done a lot. But here’s a quick guide to what’s happening:
void setup()
section runs a whole lot of functions, which are used to get the wifi board up and running.void loop()
has just one function inside: serverDemo()
.void setup()
and void loop()
.In several places you’ll see mention of a string
. Strings are special things in coding that represent words, such as “Hello.” The computer, an Arduino in our case, knows to handle them as a string of characters—letters, numbers, or symbols—that should be used together.
Strings are often contained in "
quote marks, which distinguish them from variables, so string "hello"
is different from variable hello
.
The quote marks also let the computer know when numbers should be treated like words, not something you could add or subtract. This comes up a lot with U.S. zip codes, where you might want the string "02134"
instead of the integer 2134
.
So when you see String
, think “characters” or “words.”
It might seem natural to test out other sites like “google.com” by adjusting the code, and you are absolutely right. Trouble is, that clean, basic Google home page actually has, at last check, 267 lines of code behind it—and many of them are loooong lines! All of that text quickly overwhelms your little Arduino.
In the next chapter, we’ll make some precision requests of sites that will get us little bursts of useful information.
You may have noticed that the program actually continues beyond where I stopped, with
Press any key to test server.
Beyond this point, the wifi board actually switches from looking at websites to being a website—at least within your own home. If you’d like to see how, press Return
or Enter
here. It should respond with something like
Server started! Go to 192.168.1.2
The line of numbers may be different for you, but whatever they are, copy the lot of them (in my case 192.168.1.2
), switch to a web browser on your computer, and paste them into the location bar at the top of the browser, where you’d normally put a website address:
Hit return, and you should see a bunch of numbers like
A0: 339
A1: 332
A2: 339
A3: 335
A4: 368
A5: 375
The super-cool thing? Those numbers are actually readings from the Arduino’s six analog pins—A0
through A5
! They’re pretty random at the moment, because nothing is attached to those pins. But add a sensor circuit, and those values would change with the sensor.