Paul DeCarlo
Published © MIT

Auto-Away Assist for NEST Thermostat

Let Nest know when you are in another room to assist Auto-Away using motion sensors, Particle.io, and Azure!

IntermediateFull instructions provided6 hours2,914
Auto-Away Assist for NEST Thermostat

Things used in this project

Hardware components

PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
I picked up a 5-pack from: https://www.amazon.com/DIYmall-HC-SR501-Motion-Infrared-Arduino/dp/B012ZZ4LPM
×1
Spark Core
Particle Spark Core
×1

Software apps and online services

Microsoft Azure
Microsoft Azure

Story

Read more

Schematics

Circuit Diagram

Code

nest-motion.ino

C/C++
Used to control NEST device using PIR Motion Sensor and Particle Azure IoT-Hub integration.
/*
*********************
Nest Motion Detection
*********************
A tunable program for determining motion events using a PIR sensor, with an emphasis on reducing false-positives and minimizing energy consumption.

When motion is detected, an event is published to Particle.io.
This event is then forwarded to an Azure IoT Hub which calls a Serverless function informing Nest to set Away Status to "Home"

Instruction for end-to-end configuration are avaiable @ 
*/


#include "CircularBuffer.h"

#define PIRPIN D0 //The Pin attached to the PIR Sensor

const int CalibrationTimeInSeconds = 30; //Seconds to await for calibration of the PIR sensor
const int SampleWindowSize = 10; //Ex: SampleWindowSize is the amount of samples to keep track of for evaluating the occurence of a motion event
const int PostiveSamplesToTriggerMotion = 10; //Ex: A value of x will require that at least x samples produced within the sampleWindow are postive to trigger a motion event 
const int SleepIntervalInSeconds = 600;  //The amount of time to go into deep sleep mode afer motion is reported, 600 seconds equates to a max of 10 alerts per hour

CircularBuffer<bool, SampleWindowSize> sampleWindow;

char output[50];

//Per PIR spec, allow 30s to calibrate (warm up) the sensor
void CalibrateSensor()
{
    Serial.print("Calibrating Sensor... ");
    for(int i = 0; i < CalibrationTimeInSeconds; i++){
        delay(1000);
    }
    Serial.println("PIR Sensor Calibrated");
}

void setup() {

    Serial.begin(9600);

    Serial.println("***************************************");
    Serial.println("    Nest Motion Detection Started ");
    Serial.println("***************************************");

    pinMode(PIRPIN, INPUT);
    CalibrateSensor();


}

void loop() {

    sampleWindow.push(SamplePIR());

    if(CheckSampleWindowForMotion())
    {
        Serial.print("Publishing motion event... ");
        //Motion accurately detected, time to inform Nest that we are we are home
        Particle.publish("motion",  "true", PRIVATE); //Trigger the integration
        delay(1000); //Extra sleep to ensure message delivery
        Serial.println("Motion event published");
        Serial.println("Going to sleep now...");
        System.sleep(SLEEP_MODE_DEEP, SleepIntervalInSeconds); //Go into deep sleep low-power mode for SleepIntervalInSeconds seconds

        CalibrateSensor();//Recalibrate Sesnor on awaken
    }
    
}

//Takes ten readings per second, returns true if a postive reading is encountered
bool SamplePIR() {
    
    Serial.print("Sampling PIR... ");
    
    int val = 0;
    for(int i = 0; i < 10; i += 1)
    {
        if(val == LOW)
            val = digitalRead(PIRPIN);
        
        delay(100);
    }
    
    if(val)
     {
         Serial.println(" Motion Detected in sample!");
         return true;
     }
    else
    {
        Serial.println(" No Motion Detected in sample");
        return false;
    }
}

//Loops through the sampleWindow, returns true if enough positive samples are found
bool CheckSampleWindowForMotion()
{
    Serial.print("Checking Sample Window... ");
    int positiveSamples = 0;
    
    for(int i = 0; i < SampleWindowSize ; i++){
        if(sampleWindow.pop() == true)
            positiveSamples++;
    }
    
    Serial.print(positiveSamples);
    Serial.println(" positive samples were found in sample window");
    
    if(positiveSamples >= PostiveSamplesToTriggerMotion)
        return true;
    else
        return false;
}

CircularBuffer.h

C Header File
Used to provide a sample windows for tracking motion sensor samples over time
/*
  CircularBuffer.h - circular buffer library for Arduino.
  Copyright (c) 2009 Hiroki Yagita.
  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  'Software'), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:
  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.
  THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#ifndef CIRCULARBUFFER_h
#define CIRCULARBUFFER_h
#include <inttypes.h>

template <typename T, uint16_t Size>
class CircularBuffer {
public:
  enum {
    Empty = 0,
    Half = Size / 2,
    Full = Size,
  };

  CircularBuffer() :
    wp_(buf_), rp_(buf_), tail_(buf_+Size),  remain_(0) {}
  ~CircularBuffer() {}
  void push(T value) {
    *wp_++ = value;
    remain_++;
    if (wp_ == tail_) wp_ = buf_;
  }
  T pop() {
    T result = *rp_++;
    remain_--;
    if (rp_ == tail_) rp_ = buf_;
    return result;
  }
  int remain() const {
    return remain_;
  }

private:
  T buf_[Size];
  T *wp_;
  T *rp_;
  T *tail_;
  uint16_t remain_;
};

#endif

Auto-Away-Assist-for-NEST-Thermostat

The Source Code for the "Auto Away Assist for Nest Thermostat" project

Credits

Paul DeCarlo

Paul DeCarlo

28 projects • 240 followers
Paul DeCarlo is a prof @ #Bauer college of Business @UniversityOfHouston and Software Engineer @Microsoft focused on IoT, Cloud, and Mobile.

Comments