Arduino Code

Setting up the Sketch

Since we don't want our motion detector to alert us of daytime activities (like a dog passing by, or a skateboarder), we'll need to give the sketch (prior to uploading) the current hour, minutes and seconds. In the sketch, set startingHour, currentMinutes, and currentSeconds to the current time (we're using 24-hour time for this sketch). For the sketch to work properly, you'll need to change these values on every upload.

  • While we're not using one for this project, you can add a Real-Time-Clock (like the RTC FeatherWing) if you'd like more precise timekeeping. 
Download: file
/**** Time Setup ****/
// set the current hour
int startingHour = 0;
// set the current minutes
int currentMinutes = 0;
// set the current seconds
int currentSeconds = 0;

Code Overview

The first chunk of the code (setup()) starts a MQTT connection to the Adafruit IO server. We also add three message handlers to handle changes from the indoor-lights, outdoor-lights, and home-alarm feeds from the dashboard blocks. We also set up the PIR sensor, reed switch, SGP30 sensor, and initalize the NeoPixel strip.

Download: file
void setup() {
  // start the serial connection
  Serial.begin(115200);

  // wait for serial monitor to open
  while(! Serial);
  Serial.println("Adafruit IO Home: Security");
  
  Serial.println("Connecting to Adafruit IO");
  // start MQTT connection to io.adafruit.com
  io.connect();
  
  // attach a message handler for the `home-alarm` feed
  alarm->onMessage(handleAlarm);
  // subscribe to lighting feeds and register message handlers
  indoorLights->onMessage(indoorLightHandler);
  outdoorLights->onMessage(outdoorLightHandler);

  // wait for an MQTT connection
  // NOTE: when blending the HTTP and MQTT API, always use the mqttStatus
  // method to check on MQTT connection status specifically
  while(io.mqttStatus() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  // we are connected
  Serial.println();
  Serial.println(io.statusText());
  
  // declare PIR sensor as input
  pinMode(pirPin, INPUT);
  // declare reed switch as input
  pinMode(doorPin, INPUT);
  // set up the SGP30 sensor
  setupSGP30();
  // init the neopixel strip and set to `off`
  strip.begin();
  strip.show();
}

The next chunk of code (loop()) calls io.run() which keeps the client connected to Adafruit IO. Next, it makes a call to getTime() which calculates and returns the current time. Then we read the door, SGP30, and PIR sensor (readDoorSensor, readPIR, readSGP30) and send their values to their respective Adafruit IO feeds.

Download: file
void loop(){
  io.run();

  getTime();
  Serial.println("* read door sensor...");
  readDoorSensor();
  Serial.println("* read motion detector");
  readPIR();
  Serial.println("* reading SGP30...");
  readSGP30();
}

The toggle button on your Adafruit IO dashboard is connected to the handleAlarm message handler. When the toggle button's value is changed, it receives the current value of the feed and checks if it's ON or OFF and sets the isAlarm boolean.

Download: file
void handleAlarm(AdafruitIO_Data *data) {
// handle the alarm toggle on the Adafruit IO Dashboard
  String toggleValue = data->toString();
  Serial.print("> rcv alarm: ");
  Serial.println(toggleValue);
  if(toggleValue == String("ON")) {
    Serial.println("* Alarm Set: ON");
    isAlarm = true;
  } else {
    Serial.println("* Alarm Set: OFF");
    isAlarm = false;
  }
}

Finally, we'll check if the alarm is armed (isAlarm==true). If the alarm is armed, we check if the door is open (doorState == HIGH) or if the hour is later than alarmHour and if the motion detector is triggered, we'll make a call to the playAlarmAnimation() function.

Download: file
  // check if the alarm toggle is armed from the dashboard
  if (isAlarm == true) {
    if (doorState == HIGH || (hour>alarmHour && pirState == HIGH)) {
      playAlarmAnimation();
   }
  }

Using the Smart Home Security System

Connect your Feather Huzzah to USB and upload the io_home_security sketch. 

Open up the IO-Home dashboard to interact with your smart home. Push the door open. You should see the Front Door Indicator change from green to red.

Switch the toggle from OFF to ON to arm your house's security system. When you push the front door open (or move past the PIR sensor past the time set by alarmHour), the inside lights will blink red and the piezo will make noise.

Tired of the alarm? Want some peace and quiet? Toggle the alarm OFF.

This guide was first published on Sep 05, 2018. It was last updated on Sep 05, 2018. This page (Arduino Code) was last updated on Dec 14, 2019.