Jack Hughes
Published © GPL3+

Garage Door Notifier

This device will send you an email every time that you open or close your garage door using Particle Photon 2 and IFTTT.

IntermediateFull instructions provided8 hours120
Garage Door Notifier

Things used in this project

Hardware components

Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
Photon 2
Particle Photon 2
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Maker service
IFTTT Maker service
Pro subscription required

Story

Read more

Schematics

Circuit Layout

Code

Webhook code

C/C++
Flash this code to a Photon 2 and make sure to include HC_SR04 library.
// This #include statement was automatically added by the Particle IDE.
#include <HC_SR04.h>

#include "Particle.h"

SYSTEM_THREAD(ENABLED);

SerialLogHandler logHandler;

// How often to publish a value
const std::chrono::milliseconds publishPeriod = 5s;

// The event name to publish with

unsigned long lastPublish;

void publishTest();

double cm = 0.0;
double inches = 0.0;

double listofresults[5];
int listofresultscounter = 0;
int lastMajority = 1;

int open;

double inchValues[3];
int inchValueCounter = 0;

int trigPin = D4;
int echoPin = D5;


HC_SR04 rangefinder = HC_SR04(trigPin, echoPin, 1.0, 500.0);

void setup() 
{
    Spark.variable("cm", &cm, DOUBLE);
    Spark.variable("inches", &inches, DOUBLE);
    Serial.begin(9600);
}

void loop() 
{
    cm = rangefinder.getDistanceCM();
    inches = rangefinder.getDistanceInch();
    // Serial.println(inches);
    delay(500);
    
    
    if(inches!=-1) {
        inchValues[inchValueCounter] = inches;
        inchValueCounter += 1;
        if(inchValueCounter >= 2) {
            inchValueCounter = 0;
            double avgDistance = 0;
            avgDistance = avg(inchValues, 3);
        
            Serial.println(avgDistance);
            
            if(avgDistance >= 15) {
                open = 0;
            }
            if(avgDistance < 15) {
                open = 1;
            }
            
            listofresults[listofresultscounter] = open;
            listofresultscounter += 1;
            if(listofresultscounter >=5) {
                double average = avg(listofresults, 5);
                int majority = (int)(average < 0 ? (average - 0.5) : (average + 0.5));

                
                Serial.println(majority);
                
                
                if(majority != lastMajority) {
                    // majority to be defined for initial loop
                    
                    if (Particle.connected()) {
                        if (millis() - lastPublish >= publishPeriod.count()) {
                            lastPublish = millis();
                
                            publishTest(majority);
                        }
                    } else {
                        while(!Particle.connected()) {
                            delay(100);
                        }
                        if (Particle.connected()) {
                            if (millis() - lastPublish >= publishPeriod.count()) {
                                lastPublish = millis();
                                publishTest(majority);
                            }
                        }
                    }
                }
                lastMajority = majority;
                listofresultscounter = 0;
            }
        }
    }
}

void publishTest(int num) {
    if(num == 0){
        Particle.publish("JackHA");
    }
}

int avg(double nums[], int length) {
    double sum = 0;
    for (int i = 0; i < length; i++) {
        sum += nums[i];
    }
    return sum / length;
}

Credits

Jack Hughes
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.