This is the full skech.
#define BLYNK_PRINT Serial
#include <Arduino.h>
#include <Wire.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SoftwareSerial.h>
#include <TimeLib.h> /* Program code related to Real Time Clock (RTC). */
#include <WidgetRTC.h> /* Communication code with Blynk Real Time Clock Widget */
void callPirSensior();
void callLDRSensor();
void LDRAndPIR();
bool calculateTimeGreater(int, int, int);
bool calculateTimeLesser(int, int, int);
// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XXXX";
// Your WiFi credentials for home network
char ssid[] = "2.4-XXXX";
char pass[] = "XXXXX";
//Software Serial on Uno, Nano... - This is were the ESP-01 module will communicate
SoftwareSerial EspSerial(2, 3); // RX, TX
BlynkTimer timer;
WidgetRTC rtc;
ESP8266 wifi(&EspSerial);
//INPUT
const uint8_t LDR = 13;
const uint8_t pirPin = 4;
//OUTPUT
const uint8_t relayModuleInput_2 = 8;
const uint8_t relayModuleInput_1 = 12;
const uint8_t buzzer = 7;
//Varibales
volatile boolean dontBuzz = true;
volatile boolean forceOff = false;
volatile boolean forceOn = false;
volatile boolean isRelayModuleOn = false;
const unsigned long pirConstTimer = 50000L;
WidgetLED led0(V1);
WidgetLED led1(V0);
void(* resetFunc) (void) = 0;
BLYNK_CONNECTED() {
// Synchronize time on connection
rtc.begin();
Blynk.syncAll();
}
// Two Led in the blynk app- This is to know if the relayModule is on or off.
void blinkLedWidget()
{
if (!isRelayModuleOn) {
led0.off();
led1.on();
} else {
led0.on();
led1.off();
}
}
void setup()
{
// Debug console
Serial.begin(9600);
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
delay(100);
//OUTPUT
pinMode(relayModuleInput_2, OUTPUT);
digitalWrite(relayModuleInput_2, HIGH);
digitalWrite(relayModuleInput_1, HIGH);
forceOn = false;
forceOff = false;
isRelayModuleOn = false;
blinkLedWidget();
setSyncInterval(20 * 60); // Sync interval in seconds
timer.setInterval(500L, blinkLedWidget);
timer.setInterval(2200L, LDRAndPIR);
timer.setInterval(50000L, resetForceOffBool);
timer.setInterval(60000L * 23, resetFunc);
//attachInterrupt(digitalPinToInterrupt(interruptPin), LDRAndPIR, CHANGE); //Arduino recommended way. [2 & 3 are Interrupts]
}
BLYNK_WRITE(V5)
{
int pinValue = param.asInt();
if (pinValue == 1) {
digitalWrite(relayModuleInput_2, LOW);
forceOff = false;
forceOn = true;
isRelayModuleOn = true;
}
else if (pinValue == 0) {
digitalWrite(relayModuleInput_2, HIGH);
forceOff = true;
forceOn = false;
isRelayModuleOn = false;
}
}
void loop()
{
Blynk.run();
timer.run();
}
void LDRAndPIR() {
if (forceOn) { // If light is On from the Blynk App.
isRelayModuleOn = true;
return;
}
bool morning = calculateTimeGreater (6, 30, 00);
bool toEve = calculateTimeLesser (17, 59, 59);
if (morning && toEve) {
//Skip till eve @ 6.10 PM
} else {
bool trunOnLDRAndPIR = calculateTimeGreater (18, 00, 00);
bool trunOnOnlyLDR = calculateTimeLesser (22, 29, 59);
if (trunOnLDRAndPIR)
{
//PIR Sensior
callPirSensior();
//LDR Sensor
if (trunOnOnlyLDR) {
callLDRSensor();
}
else {
digitalWrite(relayModuleInput_2, HIGH);
isRelayModuleOn = false;
}
} else {
//From midnight to morning.
bool trunOnOnlyPIR = calculateTimeGreater (00, 00, 00);
bool trunOffOnlyPIR = calculateTimeLesser (6, 30, 00);
if (trunOnOnlyPIR && trunOffOnlyPIR) {
//PIR Sensior
callPirSensior();
}
if (!forceOn) {
digitalWrite(relayModuleInput_2, HIGH);
isRelayModuleOn = false;
}
}
}
}
bool calculateTimeGreater(int inputHour, int inputMin, int inputSec) {
if (hour() > inputHour) {
return true;
} else if (hour() == inputHour) {
if (minute() > inputMin) {
return true;
}
if (minute() == inputMin) {
if (second() >= inputSec) {
return true;
}
}
}
return false;
}
bool calculateTimeLesser(int inputHour, int inputMin, int inputSec) {
if (hour() < inputHour) {
return true;
} else if (hour() == inputHour) {
if (minute() < inputMin) {
return true;
}
if (minute() == inputMin) {
if (second() <= inputSec) {
return true;
}
}
}
return false;
}
void showTime()
{
Serial.print("Time:");
Serial.print(hour());
Serial.print(':');
Serial.print(minute());
Serial.print(':');
Serial.print(second());
Serial.print(" ");
Serial.print(" ");
Serial.println();
}
/**
This will call from 18:10:00 to next day morning 6:30:00 morning. If there is any motion detected it will get on. If we force off from the Blynk app trun off the light.
**/
void callPirSensior() {
volatile long pirTimer = 0;
if (digitalRead(pirPin) == HIGH && !forceOff) {
pirTimer = millis();
isRelayModuleOn = true;
blinkLedWidget();// This has to be called if not the value dont gets changed.
forceOff = false;
timer = timer + pirConstTimer;
while (millis() < timer)
{
digitalWrite(relayModuleInput_2, LOW);
if (millis() > (pirTimer + 2300) && digitalRead(pirPin) == HIGH)
{
timer = millis();
}
}
}
//Serial.println("Leaving...callPirSensior");
}
/**
This method will be called only during the 18:10:00
to 22:30:00 hours. If force off from the Blynk app turn off the light.
**/
void callLDRSensor() {
//Serial.println("Entering...callLDRSensor");
if (digitalRead(LDR) == HIGH && !forceOff) {
digitalWrite(relayModuleInput_2, LOW);
isRelayModuleOn = true;
} else {
digitalWrite(relayModuleInput_2, HIGH);
isRelayModuleOn = false;
}
// Serial.println("Leaving...callLDRSensor");
}
void resetForceOffBool() {
//Serial.println("Entering...resetForceOffBool");
//bool trunOn = calculateTimeGreater (6, 29, 59);
//bool trunOff = caslculateTimeLesser (22, 29, 59);
if (forceOff) {
//Serial.println("Entering forceOn to reset...");
forceOff = false;
}
//Serial.println("Leaving...resetForceOffBool");
}