Nathan Charpentier
Published © GPL3+

PCL-Home Automation-IndoorLightDetector

Uses a particle photon 2 device to send messages to my phone when the lights are on and there's nobody in a room.

IntermediateShowcase (no instructions)1.5 hours1
PCL-Home Automation-IndoorLightDetector

Things used in this project

Hardware components

Photon 2
Particle Photon 2
×1
Photo resistor
Photo resistor
5 mm
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
HC-SR501 Pir Motion IR Sensor
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
For the PIR sensor
×3
Jumper wires (generic)
Jumper wires (generic)
For the photoresistor/everything else* Number of wires may vary depending on how you wire it
×4
USB A to Micro-B Cable
Digilent USB A to Micro-B Cable
Any USB to Micro USB will work, needed to connect the computer/power supply to the photon.
×1
Breadboard (generic)
Breadboard (generic)
×1
Through Hole Resistor, 330 ohm
Through Hole Resistor, 330 ohm
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE

Story

Read more

Code

Untitled file

C/C++
Paste this in the Particle Web IDE
// Include Particle Device OS APIs
#include "Particle.h"

// Let Device OS manage the connection to the Particle Cloud
SYSTEM_MODE(AUTOMATIC);


SerialLogHandler logHandler(LOG_LEVEL_INFO);

int photoresistorPin = A0;  
int lightValue = 0; 
int pirPin = D6;
int lastmove=0;
// A variable to hold the sensor's current state
int pirState = 0;
// This function runs once when the device is started
void setup() {
    Serial.begin(9600);
  // Set the PIR pin as an input
  pinMode(pirPin, INPUT);
  
}


void loop() {
  // Read the state of the PIR sensor
  pirState = digitalRead(pirPin);
int ti=millis();
if (pirState == HIGH) {
    lightValue = analogRead(photoresistorPin);
    Serial.println("Motion detected!");
    Serial.println("lightvalue= ");
    Serial.println(lightValue);
    lastmove=ti;
    delay(5000);
    // The PIR sensor also has a built-in delay based on your potentiometer setting
  }
else if(ti-900000==lastmove){
     lightValue = analogRead(photoresistorPin);
    if(lightValue>0){
    Serial.println("No motion, lights on");
    Serial.println("lightvalue= ");
    Serial.println(lightValue);
    Particle.publish("push-notification", "lights on");}
    else{
        Serial.println("No motion, lights off");
    }
    lastmove=ti;
    delay(1000);
  }
}

Credits

Nathan Charpentier
1 project • 0 followers

Comments