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 Ingo, well over 40 years and come from the middle of Germany. You can contact me in English or German.

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

Map Your Particles!
Easy
  • 463
  • 12

Protip

Locate your Particle Photons, P1s and Electrons on a map without needing extra hardware. Use Ubidots or an offline map alternative

Create a PCB from Scratch with Upverter
Easy
  • 155
  • 4

Full instructions

This tutorial is intended as an example to carry out the Hackster live Meetup of May 2017, that is the creation of a PCB with Upverter.

BBC micro:bit + Blynk Smartphone App + Arduino IDE
Easy
  • 98
  • 2

Protip

Don't let your BBC micro:bit collect dust! Here is how to use it with Blynk and Arduino IDE.

LEDog Collar
Easy
  • 946
  • 10

Full instructions

The LEDog collar lights up when it gets dark, helping you locate your dog from far away.

Handheld Infinity Kaleidoscope Group Kit
Easy
  • 2,695
  • 30

Full instructions

Use the orientation sensor in your Arduino 101 to control a cool Petri dish infinity-mirror illusion. This project is great for groups.

Sending Photos and Video Over the Internet!
Easy
  • 1,934
  • 27

Full instructions

Record video, perform object and face recognition, take a time-lapse or whatever else with a Particle Photon and the 5-megapixel Arducam!

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login
Feedback