// 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 );
}
Comments