void check() {
-
Serial.println("Checking");
30
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);
}
Chapter 11. Creating a Burglar Alarm with Email Notification • 198
report erratum • discuss
www.it-ebooks.info