- •Acknowledgments
- •Preface
- •Who Should Read This Book
- •What’s in This Book
- •Arduino Uno and the Arduino Platform
- •Code Examples and Conventions
- •Online Resources
- •The Parts You Need
- •Starter Packs
- •Complete Parts List
- •1. Welcome to the Arduino
- •What You Need
- •What Exactly Is an Arduino?
- •Exploring the Arduino Board
- •Installing the Arduino IDE
- •Meeting the Arduino IDE
- •Hello, World!
- •Compiling and Uploading Programs
- •What If It Doesn’t Work?
- •Exercises
- •2. Creating Bigger Projects with the Arduino
- •What You Need
- •Managing Projects and Sketches
- •Changing Preferences
- •Using Serial Ports
- •What If It Doesn’t Work?
- •Exercises
- •3. Building Binary Dice
- •What You Need
- •Working with Breadboards
- •Using an LED on a Breadboard
- •First Version of a Binary Die
- •Working with Buttons
- •Adding Your Own Button
- •Building a Dice Game
- •What If It Doesn’t Work?
- •Exercises
- •4. Building a Morse Code Generator Library
- •What You Need
- •Learning the Basics of Morse Code
- •Building a Morse Code Generator
- •Fleshing Out the Morse Code Generator’s Interface
- •Outputting Morse Code Symbols
- •Installing and Using the Telegraph Class
- •Publishing Your Own Library
- •What If It Doesn’t Work?
- •Exercises
- •5. Sensing the World Around Us
- •What You Need
- •Measuring Distances with an Ultrasonic Sensor
- •Increasing Precision Using Floating-Point Numbers
- •Increasing Precision Using a Temperature Sensor
- •Creating Your Own Dashboard
- •What If It Doesn’t Work?
- •Exercises
- •What You Need
- •Wiring Up the Accelerometer
- •Bringing Your Accelerometer to Life
- •Finding and Polishing Edge Values
- •Building Your Own Game Controller
- •More Projects
- •What If It Doesn’t Work?
- •Exercises
- •7. Writing a Game for the Motion-Sensing Game Controller
- •Writing a GameController Class
- •Creating the Game
- •What If It Doesn’t Work?
- •Exercises
- •8. Generating Video Signals with an Arduino
- •What You Need
- •How Analog Video Works
- •Building a Digital-to-Analog Converter (DAC)
- •Connecting the Arduino to Your TV Set
- •Using the TVout Library
- •Building a TV Thermometer
- •Working with Graphics in TVout
- •What If It Doesn’t Work?
- •Exercises
- •9. Tinkering with the Wii Nunchuk
- •What You Need
- •Wiring a Wii Nunchuk
- •Talking to a Nunchuk
- •Building a Nunchuk Class
- •Using Our Nunchuk Class
- •Creating Your Own Video Game Console
- •Creating Your Own Video Game
- •What If It Doesn’t Work?
- •Exercises
- •10. Networking with Arduino
- •What You Need
- •Using Your PC to Transfer Sensor Data to the Internet
- •Registering an Application with Twitter
- •Tweeting Messages with Processing
- •Communicating Over Networks Using an Ethernet Shield
- •Using DHCP and DNS
- •What If It Doesn’t Work?
- •Exercises
- •11. Creating a Burglar Alarm with Email Notification
- •What You Need
- •Emailing from the Command Line
- •Emailing Directly from an Arduino
- •Detecting Motion Using a Passive Infrared Sensor
- •Bringing It All Together
- •What If It Doesn’t Work?
- •Exercises
- •What You Need
- •Understanding Infrared Remote Controls
- •Grabbing Remote Control Codes
- •Cloning a Remote
- •Controlling Infrared Devices Remotely with Your Browser
- •Building an Infrared Proxy
- •What If It Doesn’t Work?
- •Exercises
- •13. Controlling Motors with Arduino
- •What You Need
- •Introducing Motors
- •First Steps with a Servo Motor
- •Building a Blaminatr
- •What If It Doesn’t Work?
- •Exercises
- •Current, Voltage, and Resistance
- •Electrical Circuits
- •Learning How to Use a Wire Cutter
- •Learning How to Solder
- •Learning How to Desolder
- •The Arduino Programming Language
- •Bit Operations
- •Learning More About Serial Communication
- •Serial Communication Using Various Languages
- •What Are Google Chrome Apps?
- •Creating a Minimal Chrome App
- •Starting the Chrome App
- •Exploring the Chrome Serial API
- •Writing a SerialDevice Class
- •Index
Bringing It All Together • 197
Bringing It All Together
With our PassiveInfraredSensor and SmtpService classes, it’s a piece of cake to build a burglar alarm with email notifications. Connect the PIR sensor to the Ethernet shield, as shown in the following figure.
Then enter the following code in your Arduino IDE:
Ethernet/BurglarAlarm/burglar_alarm.h
Line 1 #ifndef __BURGLAR_ALARM_H__
-#define __BURGLAR_ALARM_H__
-#include "pir_sensor.h"
-#include "smtp_service.h"
5
-class BurglarAlarm {
-PassiveInfraredSensor _pir_sensor;
- |
SmtpService |
_smtp_service; |
-void send_alarm() { 10 Email email(
-"arduino@example.com",
-"info@example.net",
-"Intruder Alert!",
-"Someone's moving in your living room!"
15 );
-_smtp_service.send_email(email);
-}
-
-public:
20 BurglarAlarm(
-const PassiveInfraredSensor& pir_sensor,
- |
const SmtpService& |
smtp_service) : |
-_pir_sensor(pir_sensor),
-_smtp_service(smtp_service)
25 {
-}
-
-
report erratum • discuss
Chapter 11. Creating a Burglar Alarm with Email Notification • 198
-void check() {
30 Serial.println("Checking");
-if (_pir_sensor.motion_detected()) {
-Serial.println("Intruder detected!");
-send_alarm();
-}
35 }
-};
-#endif
This defines a class named BurglarAlarm that aggregates all the code we’ve written so far. It encapsulates a SmtpService instance and a PassiveInfraredSensor object. Its most complex method is send_alarm, which sends a predefined email.
The rest of the BurglarAlarm class is pretty straightforward. Beginning on line 20, we define the constructor that initializes all private members. If the PIR sensor detects movement, the check method sends an email.
Let’s use the BurglarAlarm class:
Ethernet/BurglarAlarm/BurglarAlarm.ino
#include <SPI.h> #include <Ethernet.h>
#include "burglar_alarm.h"
const unsigned int PIR_INPUT_PIN = 2;
const unsigned int SMTP_PORT |
= 25; |
||
const unsigned int BAUD_RATE |
= 9600; |
||
const String |
USERNAME |
= "bm90bXl1c2VybmFtZQ=="; // Encoded in Base64. |
|
const String |
PASSWORD |
= "bm90bXlwYXNzd29yZA=="; // Encoded in Base64. |
|
byte mac[] = { 0xDE, |
0xAD, 0xBE, 0xEF, 0xFE, 0xED }; |
||
IPAddress my_ip(192, |
168, 2, |
120); |
|
// Insert IP address |
of your |
SMTP server below! |
IPAddress smtp_server(0, 0, 0, 0);
PassiveInfraredSensor pir_sensor(PIR_INPUT_PIN);
SmtpService |
smtp_service(smtp_server, |
SMTP_PORT, USERNAME, PASSWORD); |
BurglarAlarm |
burglar_alarm(pir_sensor, |
smtp_service); |
void setup() { Ethernet.begin(mac, my_ip); Serial.begin(BAUD_RATE); delay(20 * 1000);
}
void loop() { burglar_alarm.check(); delay(3000);
}
report erratum • discuss
Bringing It All Together • 199
First we include all of the libraries we need, and we define constants for the PIR sensor pin and our MAC address. Then we define SmtpService and PassiveInfraredSensor objects and use them to define a BurglarAlarm instance. Note that we pass a username and a password, implying that we’re using an authenticated SMTP connection to send our emails. If you use an unauthenticated connection, you can safely remove the USERNAME and PASSWORD parameters and all of their occurrences.
In the setup method, we define the serial port and the Ethernet shield. I’ve also added a delay of twenty seconds, which gives you enough time to leave the room before the alarm begins to work.
The loop function is simple, too. It delegates all the work to BurglarAlarm’s check method. In the following figure, you can see what happens when the burglar alarm detects an intruder.
Did you notice how easy object-oriented programming on an embedded device can be? We’ve cleanly hidden in two small classes the complexity of both email and the PIR sensor. To build the burglar alarm, we then only had to write some glue code.
One word regarding privacy: do not abuse the project in this chapter to observe other people without their knowledge. Not only is it unethical, but in many countries it’s even illegal!
In this and the preceding chapter, you learned different ways to connect the Arduino to the Internet. Some of them require an additional PC, while others need an Ethernet shield, but they all open the door to a whole new range of embedded computing applications.
report erratum • discuss
Chapter 11. Creating a Burglar Alarm with Email Notification • 200
Networking is one of those techniques that may have a direct impact on the outside world. In the next chapter, you’ll learn about another technique that has similar effects: you’ll learn how to control devices remotely.
What If It Doesn’t Work?
The burglar alarm is a networking project, so you should check all the things mentioned in What If It Doesn’t Work?, on page 180. In addition, you should double-check the connection parameters and authentication information for your email provider. Did you use the correct IP address for your email provider? Did you use the right SMTP port? Did you use the right username and password? Did you use the right Base64 version of the username and password?
PIR sensors are fairly simple devices. Still, you can wire them the wrong way, so if the motion detection doesn’t work, double-check the wiring.
Exercises
•Build a project similar to the burglar alarm, but use another type of sensor. There’s tons of inspiration out there on the Web.9
•Add the current timestamp to the burglar alarm email. Get the timestamp from a Daytime service.
•Add support for DHCP and DNS to the burglar alarm.
•Add support for Base64 to the burglar alarm, so you no longer have to manually encode your username and password. A Base64 library is available.10
•Add a piezo buzzer to the project and emit a beeping sound whenever a burglar is detected.
•Get a TTL Serial Camera11 and attach photos of the burglar to your emails. This is a fairly advanced exercise. You have to learn how to control the camera, and you also have to learn how to send email attachments.
9.http://www.tigoe.net/pcomp/code/arduinowiring/873
10.https://github.com/adamvr/arduino-base64
11.https://learn.adafruit.com/ttl-serial-camera/overview
report erratum • discuss
CHAPTER 12
Creating Your Own Universal Remote
Control
Remote controls add a lot of convenience to our lives, but they aren’t without annoyances. Sometimes remotes don’t have a certain function that you’d like, such as a sleep timer. Plus, remote controls seem to reproduce at the same rate as rabbits. They quickly occupy your whole coffee table, and you have to feed them with expensive batteries that you don’t have at home when you need them during a football game. Universal remote controls reduce the pain, but even the most expensive products aren’t perfect.
Although we use remote controls every day, few of us understand how they work. In this chapter, you’ll find out how remote controls work from the inside out, and then you’ll build your own universal remote control that’s better than a store-bought one because you can fully customize it to your needs. You can easily add all of your favorite functions, and you can add functions that other remotes don’t offer. If a commercial product doesn’t support a certain vendor, you’re usually stuck. With your own remote, you can easily add new protocols. You can even support not only infrared, but also more transmission technologies, such as Bluetooth or Wi-Fi.
We get started by learning the basics of infrared signaling. You’ll build an infrared circuit to grab control codes from any remote you have on hand. Once you grab the control codes, you can emit them using an infrared LED, and you’ll start to build your own universal remote control.
Then we’ll even take the idea of a remote control a step further. Once we have a universal remote, we’ll control the Arduino itself using the serial port or an Ethernet connection. This way, you can control the Arduino using a web browser, so you can control your TV set or DVD player using a web browser.
report erratum • discuss