Ingredients
Special Items
Optional Item
If making a computer say “Hello, world” is the first step of programming, and making an LED blink is the “Hello, world” of do-it-yourself smart objects, it’s quite possible that making an “Internet Button” is the “Hello, world” of things that do things on the Internet. The idea is that you push a button in the physical world and something happens in the Internet world. In our case, we’re going to send an email—to you!
For a few projects in a row, we’ve taught an Arduino to take information from the Internet and do something with it. Now we going to send information to the Internet, where we’ll do something with it.
Our information is going to be a “trigger” signal, which we’ll send with the push of a button.
That signal will go to a free service called IFTTT, which stands for “If This Then That.” In our case: “If you get a signal from my Arduino, then send me an email!”
First, let’s put the button and the rest of the ingredients together.
13
on the wifi board.5V
pin, and the other end into the breadboard’s Row 1 at Column c.2
, and the other end into the breadboard’s Row 3 at Column c.GND
pins, and the other end into the breadboard anywhere along the blue “–” rail on the left side, closest to Column a.It should look like this:
You’re surely an expert at getting project code by now, but here are the details for this chapter:
family-projects-sketches-master
folder, and double-click it to open it.email_button
folder.If you’re reading this on a computer:
No matter how you got the project code into your Arduino software, be sure to save your work, using File → Save.
As you did for previous chapters, look for this section and replace YourWiFiNetworkNameGoesHere
with your wifi’s name and YourWiFiPasswordGoesHere
with its password:
//////////////////////////////
// WiFi Network Definitions //
//////////////////////////////
const char mySSID[] = "YourWiFiNetworkNameGoesHere";
const char myPSK[] = "YourWiFiPasswordGoesHere";
All right. The button is ready to send the trigger signal. Now we need to set up IFTTT to receive the signal. That requires a free IFTTT account. Here are the steps for making one:
Account made! Now, IFTTT takes you on a little tour. Let’s get through that.
Eventually you’ll get to a page with this toolbar at the top:
That’s where you want to be! There are a bunch of steps below, but they’re really straightforward and will only take a couple of minutes.
OK! Recipe made. Now we have to get a little bit of information from IFTTT to complete the process.
We’re about to get a little key from IFTTT that we’re going to put into our program.
////////////////////
// IFTTT Variable //
////////////////////
const String maker_key = "YourMakerKeyGoesHere";
YourMakerKeyGoesHere
and replace it with the key from the IFTTT page by pasting the key between the quote marks, using Edit → Paste.Sweet. You are good to go.
Did you get an email? If not, check out “Fixes” below.
Important note: I’ve written the program so you have to wait again for a full minute to send another email. This way, if something is amiss, you don’t accidentally send yourself hundreds of emails right away (and irritate the servers along the way). The LED will go dark when a minute is over.
The best way to troubleshoot this program is to watch two things: the little blue LED on the SparkFun board and the Serial Monitor.
The little blue light needs to be steady and stable. If it isn’t, there’s a problem staying on the wifi network. One thing that I found helps is to make certain the USB cable is firmly in the computer. Sometimes it slides out even a little, and that seems to cause problems.
The Serial Monitor should look like this:
If it doesn’t, check out the error solutions below.
If you get the “looping forever” error, that means the connection between the Arduino and the wifi board didn’t work. Unplug the USB cable from the computer, count to five, and put it back in firmly. There’s a chance this was because of a soldering issue, but more likely it is just being flakey.
This I got a lot. The key seems to be that once you press the reset button, you need to wait for a good minute or two. Waiting allows the board to get connected to the Internet.
Again, make sure the USB cable is firmly in both your computer and your Arduino. That matters.
If the script says it’s posting to IFTTT, but you see nothing else, be sure your maker_key
has been copied into your code correctly.
The Arduino is actually doing something incredibly simple: When you push the button, it essentially visits a web page at IFTTT—and when IFTTT sees the “visit” it triggers the email. You can test this with a web browser, too. Copy this link into your browser . . .
https://maker.ifttt.com/trigger/button_pressed/with/key/YourMakerKeyGoesHere
. . . and before you press Enter
, change YourMakerKeyGoesHere
to your unique Maker key, putting it right after the last slash at key/. Then hit Enter
and you should get an email!
The core of this sketch is definitely the void sendTrigger()
section. That’s what actually sends a signal to the IFTTT website. In this section, the communication action happens anywhere you see client
, such as:
client.print(httpHeader);
client.println();
With every instance of client
, the sketch is communicating with the wifi board. Those two lines actually “print” the website information to the wifi board, causing it to visit the IFTTT website and trigger the Maker part of the recipe.
You don’t need to keep your Arduino tethered to your computer for this program to work. Simply unplug the USB cable from your Arduino and power it instead with your Arduino power supply. Your program will be active and running a few moments after your Arduino powers up.
The email you get is a little robotic, and not particularly friendly.
But you can customize it!
There are dozens of things you can make your button do besides send email. By activating new channels in IFTTT, you can change the “then” part of “If the button is pressed, then . . .” Here are some possibilities:
In most cases, you’ll need to activate the channel, which may include verifying your information (like your phone number) or getting your permission to use another service (like Google).
There are lots of other channels, many attached to brands and companies that sell Internet-connected devices, such as lamps and appliances. If you have one, you can trigger it with your new button!