Things used in this project

Hardware components:
Photon new
Particle Photon
×1
Relay (generic)
Songle SRD-05VDC-SL-C
×1
13285 01
PIR Motion Sensor (generic)
×1
10167 01
DHT22 Temperature Sensor
×1
Adafruit industries ada161 image 75px
Photo resistor
×1
Omron b3f 1000 image 75px
SparkFun Pushbutton switch 12mm
×1
11026 02
Jumper wires (generic)
×1
12002 04
Breadboard (generic)
×1
Mfr 25frf52 10k sml
Resistor 10k ohm
protect photo-resistor
×1
Mfr 25fbf52 221r sml
Resistor 221 ohm
protect DHT22-Sensor
×1
Mfr 25frf52 1k sml
Resistor 1k ohm
LED control PIR-Sensor activities
×1
Software apps and online services:
Blynk logo avatars
Blynk

Schematics

Schematic
Myled relay sketch steckplatine pye3doci0a

Code

Untitled fileC/C++
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>

// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>

// Sources:
// http://diotlabs.daraghbyrne.me/7-communicating-events/pir/
// https://learn.adafruit.com/pir-passive-infrared-proximity-motion-sensor/using-a-pir
// https://gist.github.com/zsup/9496462
// http://community.garadget.com/t/instructions-for-home-grown-garadget-clone/69 --> Relay Belegung und Schaltung

int inputPin = D0;              // choose the input pin (for PIR sensor)
int ledPin = D1;                // LED Pin
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int boardLed = D7;              // photon onBoard LED
int boardRelay = D2;            // Relay
int calibrateTime = 10000;      // wait for the thingy to calibrate
float light_threshold = 20;     // brightness to decide its time for more light in %
int analogvalue;                // Here we are declaring the integer variable analogvalue, which we will use later to store the value of the photoresistor.
int photoresistor = A0;         // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below).
int photopower = A5;            // This is the other end of your photoresistor. The other side is plugged into the "photoresistor" pin (above).
                                // The reason we have plugged one side into an analog pin instead of to "power" is because we want a very steady voltage to be sent to the photoresistor.
                                // That way, when we read the value from the other side of the photoresistor, we can accurately calculate a voltage drop.
const int pushButton = D4;      // Button auf D4 (buttonPin)
int x = 1;
// variables will change:
int pushButtonState = 0;        // Statusabfrage vom Button (buttonState)
int oldButtonState = LOW;



// DHT parameters
#define DHTPIN 5 // what pin we´re connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
    // Variables
float temperature;
int humidity;
    // DHT sensor
DHT dht(DHTPIN, DHTTYPE);
unsigned long lastmillis = 0;


char auth[] = "<< your Blync code here >>"; 

void setup()
{
  Serial.begin(9600);
    // Start Blynk
  Blynk.begin(auth);
    // Start DHT22 Sensor
  dht.begin();

  pinMode(photoresistor,INPUT);  // Our photoresistor pin is input (reading the photoresistor)
  pinMode(photopower,OUTPUT);         // The pin powering the photoresistor is output (sending out consistent power)
                                 // Next, write one pin of the photoresistor to be the maximum possible, so that we can use this for power.
  digitalWrite(photopower,HIGH);      

    // We are going to declare a Particle.variable() here so that we can access the value of the photoresistor from the cloud.
    Particle.variable("PIR-Motion", &analogvalue, INT);
    //Particle.publish("PIR-Motion", String(analogvalue), PRIVATE);
    // This is saying that when we ask the cloud for "analogvalue", this will reference the variable analogvalue in this app, which is an integer variable.

    
  pinMode(boardLed,OUTPUT); // on-board LED 
  pinMode( ledPin, OUTPUT );
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(boardRelay,OUTPUT);   // declare Relay
  pinMode(pushButton, INPUT_PULLUP);   // declare Button as input
  
   // Now flash the D7 LED on and off 
  digitalWrite(boardLed,HIGH); // Start der Kallibrierung mit D7 on
  Particle.publish("PIR-Motion","now online",100,PRIVATE);
  digitalWrite(boardLed,LOW); // Start der Kallibrierung mit D7 off
}


void loop()
{
    
  // Start Blynk
  Blynk.run();
  
  // Start Button-check
  readButton_einaus();

      // check to see what the value of the photoresistor is and store it in the int variable analogvalue
    analogvalue = analogRead(photoresistor);
    delay(100);
    
  // if the sensor is calibrated
  if ( calibrated() )
  {
  // get the data from the sensor
    readTheSensor();

    // report it out, if the state has changed
    reportTheData();
  }
  
            if ((millis() - lastmillis) > 20000) {
                lastmillis = millis();
                TempHumi();
            }
  
}

void TempHumi() {
    // Humidity measurement
    temperature = dht.getTempCelcius();
    
    // Humidity measurement
    humidity = dht.getHumidity();
    
    Particle.publish("PIR Motion - temp", String(temperature), PRIVATE);
    Particle.publish("PIR Motion - humidity", String(humidity), PRIVATE);
    Blynk.virtualWrite(V3, String(temperature)); // 
    Blynk.virtualWrite(V4, String(humidity)); //
    Blynk.virtualWrite(V5, String(temperature)); // 
    Blynk.virtualWrite(V6, String(humidity)); //
}

void readButton_einaus() {
    // Get the current state of the button
  int newButtonState = digitalRead(pushButton);

  // Has the button gone high since we last read it?
  if (newButtonState == HIGH && oldButtonState == LOW) {

    if (x == 0) {
      // Toggle on
      digitalWrite(boardRelay, HIGH);
      x = 1;

    } else {
      // Toggle off
      digitalWrite(boardRelay, LOW);
      x = 0;
    }
  }

  // Store the button's state so we can tell if it's changed next time round
  oldButtonState = newButtonState;
}



void readTheSensor() {
  val = digitalRead(inputPin);
}

bool calibrated() {
  return millis() - calibrateTime > 0;
}

void reportTheData() {

  // if the sensor reads high
  // or there is now motion
  if (val == HIGH) {

    // the current state is no motion
    // i.e. it's just changed
    // announce this change by publishing an eent
    if (pirState == LOW) {
      // we have just turned on
      Particle.publish("PIR Motion", "Motion detected", PRIVATE);
      
      // Update the current state
      pirState = HIGH;
      setLED( pirState );
      
        if (analogvalue < light_threshold) {
              //Particle.publish("PIR Motion", String(light), PRIVATE);
              Particle.publish("PIR Motion", String(analogvalue), PRIVATE);
              Blynk.virtualWrite(V1, String(analogvalue));
              //Blynk.virtualWrite(V1, String(light)); 
              Particle.publish("PIR Motion", String(light_threshold), PRIVATE);
              digitalWrite(boardRelay, HIGH); // Relay anschalten
              Particle.publish("PIR Motion", "Relay on", PRIVATE);
              delay(15000); // 15 sec bleibt das Relay geschalten
              digitalWrite(boardRelay, LOW); // Relay ausschalten
        } else {
            Particle.publish("PIR Motion", String(analogvalue), PRIVATE);
            //Particle.publish("PIR Motion", String(light), PRIVATE);
            Blynk.virtualWrite(V2, String(analogvalue)); 
            //Blynk.virtualWrite(V2, String(light)); 
        }
    }
  } else {
    if (pirState == HIGH) {
      // we have just turned of
      // Update the current state
      pirState = LOW;
      setLED( pirState );
    }
  }
}

void setLED( int state )
{
  digitalWrite( ledPin, state );
}

Credits

Img 6554 3ywwumwxbh
Ingo Lohs

I am well over 40 years and come from the middle of Germany. You can contact me also in German. Donation for add sensors: paypal.me/ingolohs

Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Control Adafruit NeoPixels using Python and JQWidgets
Easy
  • 933
  • 8

Full instructions

In this tutorial, we'll see how to control an Adafruit NeoPixel ring via mobile using Python and JQWidgets.

What's My I2C Address?
Easy
  • 244
  • 4

The story of how to make a clock through using a LCD 1602 or LCD 2004 over I2C.

I Keep My Cucumber Growing Conditions Moist - Part 3
Easy
  • 268
  • 3

Full instructions

This is the next step with an Particle Photon and softAP integration: cucumbers go online.

MyHumidity Controlled by BME280 and Photon
Easy
  • 521
  • 6

Full instructions

Checks if humidity threshold is reached and informs the user by using Blynk/IFTTT-services with softAP integration.

Study Buddy
Easy
  • 2,850
  • 141

Full instructions

Know when an assignment is coming up!

Study Buddy

Team Alexa and Shannon

MyLCD20x4 clock with value-added information
Easy
  • 41
  • 0

Full instructions

Use a Particle Photon, a BME280 sensor and a cheap LCD20x4 to start your own clock-project.

Add projectSign up / Login