Adam DeMelloMyles GainesSarah Howard
Published

MEGR 3171: IOT Alarm System

This project is an alarm system for while you are away that tracks changes in sound, light, and motion, and implements the use of Photon 2s.

BeginnerShowcase (no instructions)39

Things used in this project

Hardware components

Photon 2
Particle Photon 2
×3
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
SparkFun Sound Detector (with Headers)
SparkFun Sound Detector (with Headers)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Photo resistor
Photo resistor
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×3

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Communication Mapping

This is an image that shows the overall mapping of our IoT alarm system. Each photon outputs a voltage signal that powers that particular circuit. Each sensor then receives this signal and is able to collect data. This data then gets sent back through the circuits and into their respective photons. The photons are also mapped together, so we can have an LED on a photon trigger based off the data it is readings from its individual sensor. All of the sensor data is then transferred to the Particle Console. From there it can be viewed under the events tab or viewed under our ThingSpeak Channel.

IoT Alarm System Circuit Image

This in an image of all 3 sensors and circuits used within this project.

Motion Sensor Schematic

This is an image of the circuit schematic that includes the motion sensor and photon 2 used in this project.

Sound Sensor Schematic

This is an image of the circuit schematic that includes the sound sensor and photon 2 used in this project.

Photoresistor Schematic

This is an image of the circuit schematic that includes the photoresistor and photon 2 used in this project.

Code

Motion Sensor Counter

C/C++
This code was used to create a counter for the pir motion sensor used in the IoT alarm system, The motion sensor was set at a no-movement threshold and whenever the sensor picked up movement, a count was added to the counter within the code. This count was then graphed on ThingSpeak and could be tracked remotely using ThingSpeak
int led = D7;

// PIR on D4
int pir = D4;

// tracks if the publish was successful
bool published;

//initializes count
int count=0;

void setup()
{
    // Subscribe to the integration response event
  Particle.subscribe("hook-response/MotionCounter", myHandler, MY_DEVICES);
  
    pinMode(led, OUTPUT);
    
    // sets the PIR sensor input pin as input
    pinMode(pir, INPUT);
    
    // ensures the LED is off
    digitalWrite(led, LOW);
    
  // Subscribe to the integration response event
  Particle.subscribe("hook-response/MotionCounter", myHandler);
}

void myHandler(const char *event, const char *data) {
  // Handle the integration response
}


void loop()
{
    if (digitalRead(pir) == HIGH)
    // if motion is detected
    {
        // turn on the LED to indicate that motion was detected
        digitalWrite(led, HIGH);
        published = Particle.publish("MotionCounter", "true");
        count++;
        Particle.publish("MotionCounter", String(count), 60);
        delay(5000);
        // Trigger the integration
        if (!published)
        // if the publish was unsuccessful
        {
            // flash the LED 10 times as an indicator of the publish failure
            for (int i = 0; i < 10; i++)
            {
                digitalWrite(led, LOW);
                delay(100);
                digitalWrite(led, HIGH);
                delay(100);
            }
        }
    }
    else
        // if no motion is detected, turn off the LED
        digitalWrite(led, LOW);
        
delay(2000);
}

Sound Sensor Counter

C/C++
This code was used to create a counter for the big sound sensor used in the IoT alarm system. The sound sensor was set at a low volume threshold to simulate an empty house and whenever a sound broke through this threshold, a count was added to the counter within the code. This count was then graphed on ThingSpeak and could be tracked remotely using ThingSpeak
// Sound Sensor on A0
const int soundSensorPin = A0;
// Led on D2
const int ledPin = D7;
// Sound threshold is 150
const int threshold = 190;
// tracks if the publish was successful
bool published;
//initializes count
int count=0;

void setup() {
  pinMode(soundSensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  // Subscribe to the integration response event
  Particle.subscribe("hook-response/SoundCounter", myHandler, MY_DEVICES);
}

void myHandler(const char *event, const char *data) {
  // Handle the integration response
}

void loop() {
  int soundValue = analogRead(soundSensorPin);
  Serial.println(soundValue);

  if (soundValue > threshold)
  {
    // Sound detected, turn on the LED
    digitalWrite(ledPin, HIGH);
    published = Particle.publish("SoundCounter", "true");
    count++;
    Particle.publish("SoundCounter", String(count), 60);
    delay(5000);
    // Trigger the integration
    if (!published)
    // if the publish was unsuccessful
    {
        // flash the LED 10 times as an indicator of the publish failure
        for (int i = 0; i < 10; i++)
        {
            digitalWrite(ledPin, LOW);
            delay(100);
            digitalWrite(ledPin, HIGH);
            delay(100);
        }
    }
  } else {
    // No sound or sound below the threshold, turn off the LED
    digitalWrite(ledPin, LOW);
  }
  
  delay(2000);
}

Photo-Resistor Counter

C/C++
This code was used to create a counter for the photo-resistor used in the IoT alarm system, The photoresistor was set at a dark threshold and whenever a light source broke through this threshold, a count was added to the counter. This count was then graphed on ThingSpeak and could be tracked remotely using ThingSpeak
// LED on pin D7
int led = D7;

// Light sensor on A1
int lightSensor = A1;

// Threshold for light level
int lightThreshold = 700;

// Tracks if the publish was successful
bool published;

// Initializes count
int count = 0;

void setup() {
  Particle.subscribe("hook-response/LightCounter", myHandler, MY_DEVICES);
  pinMode(led, OUTPUT);
  pinMode(lightSensor, INPUT);
  digitalWrite(led, LOW);
}

void myHandler(const char *event, const char *data) {
  // Handle the integration response if needed
  // For example, you can print it to Serial
  Serial.println("Integration Response Received: " + String(data));
}

void loop() {
  int lightValue = analogRead(lightSensor);
  Serial.println(lightValue);

  if (lightValue > lightThreshold) {
    // Sufficient light, turn on the LED
    digitalWrite(led, HIGH);
    published = Particle.publish("LightCounter", "true");
    count++;
    Particle.publish("LightCounter", String(count), 60);
    delay(5000);

    // Trigger the integration
    if (!published) {
      // If the publish was unsuccessful, flash the LED 10 times
      for (int i = 0; i < 5; i++) {
        digitalWrite(led, LOW);
        delay(100);
        digitalWrite(led, HIGH);
        delay(100);
      }
    }
  } else {
    // Low light, turn off the LED
    digitalWrite(led, LOW);
  }

  delay(500);
}

Credits

Adam DeMello
1 project • 2 followers
Contact
Myles Gaines
1 project • 1 follower
Contact
Sarah Howard
1 project • 1 follower
Contact

Comments

Please log in or sign up to comment.