I have two sensors
LDR and PIR sensors.
LDR and PIR should work together.
The light should On only after 18:10:00 till 22:29:59 and after that, if there is any motion detected then turn on the light for 45 sec or continue until there is any motion detected by PIR.
Second part: If I turn off the light from the app it should turn off and wait until it detects motion to turn on. Then the app should switch the LED light to “RED”.
The LED light in the app is to how if the relay module is On or Off.
Problem: All of the above works fine. If I call the “blinkLedWidget()” where is the relay module is set to LOW or High.
if I set the timer.setInterval(1000L,blinkLedWidget()) and setSyncInterval(10*60) . There timer is not getting set.
It doesn’t moves beyond the below message
10:45:05.578 -> ___ __ __
10:45:05.578 -> / _ )/ /_ _____ / /__
10:45:05.613 -> / _ / / // / _ \/ '_/
10:45:05.650 -> /____/_/\_, /_//_/_/\_\
10:45:05.686 -> /___/ v0.6.1 on Arduino Uno
10:45:05.724 ->
10:45:06.160 -> [631] Connecting to 2.4-XXXXX
10:45:09.343 -> [3837] AT version:1.1.0.0(May 11 2016 18:09:56)
10:45:09.416 -> SDK version:1.5.4(baaeaebb)
10:45:09.451 -> Ai-Thinker Technology Co. Ltd.
10:45:09.451 -> Jun 13 2016 11:29:20
10:45:09.484 -> OK
10:45:16.647 -> [11129] +CIFSR:STAIP,"XXXXXX"
10:45:16.684 -> +CIFSR:STAMAC,"XXXXXXX"
10:45:16.721 -> [11138] Connected to WiFi
10:45:27.062 -> [21524] Ready (ping: 24ms).
Stuck here!!!
#define BLYNK_PRINT Serial
#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 */
#include "pitches.h"
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[] = "XXXXXX";
// 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
int LDR = 13;
int pirPin = 4;
//OUTPUT
int relayModuleInput_2 = 8;
int buzzer = 7;
//Varibales
boolean dontBuzz = true;
boolean forceOff = false;
boolean forceOn = false;
boolean isRelayModuleOn = false;
WidgetLED led0(V1);
WidgetLED led1(V0);
BLYNK_CONNECTED() {
// Synchronize time on connection
rtc.begin();
//Blynk.syncAll();
}
// Two Led in the blynk app- This is to know if the relay module 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);
//OUTPUT
pinMode(relayModuleInput_2, OUTPUT);
digitalWrite(relayModuleInput_2, HIGH);
timer.setInterval(60000L, blinkLedWidget);
setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)
}
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();
LDRAndPIR();
}
void LDRAndPIR() {
if (forceOn) { // If light is On from the Blynk App.
isRelayModuleOn = true;
return;
}
bool trunOnLDRAndPIR = calculateTimeGreater (18, 10, 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 if (!forceOn) {
digitalWrite(relayModuleInput_1, HIGH);
digitalWrite(relayModuleInput_2, HIGH);
isRelayModuleOn = false;
}
bool trunOffOnlyLDRNextDay = calculateTimeLesser (18, 10, 00); //00:00:00 to 18:10:00 hrs.
if (trunOffOnlyLDRNextDay && !forceOn) {
digitalWrite(relayModuleInput_2, HIGH);
isRelayModuleOn = false;
}
//From midnight to morning.
bool trunOnOnlyPIR = calculateTimeGreater (00, 00, 00);
bool trunOffOnlyPIR = calculateTimeLesser (06, 30, 00);
if (trunOnOnlyPIR && trunOffOnlyPIR) {
//PIR Sensior
callPirSensior();
}
//The Alaram goes on at this time.
bool trunOnBuzzer = calculateTimeGreater (7, 30, 00);
bool trunOffBuzzer = calculateTimeLesser (7, 30, 30);
//Only for buzzer.
if (trunOnBuzzer && trunOffBuzzer)
{
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(buzzer, melody1[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(buzzer);
}
}
}
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 the next day morning at 6:30:00 morning. If there is any motion detected it will get on. If we force off from the Blynk app turn off the light.
**/
void callPirSensior() {
long timer = 0;
if (digitalRead(pirPin) == HIGH && digitalRead(LDR) == HIGH) {
timer = (millis() / 1000);
isRelayModuleOn = true;
while ((millis() / 1000) < (timer + 45))
{
digitalWrite(relayModuleInput_2, LOW);
if ((millis() / 1000) > (timer + 15) && digitalRead(pirPin) == HIGH)
{
timer = (millis() / 1000);
}
Blynk.run(); // If I force off from Blynk app.
if (forceOff) {
break;
}
}
}
if (forceOff && calculateTimeGreater (22, 30, 00) || calculateTimeLesser (6, 29, 59)) {
forceOff = false;
isRelayModuleOn = false;
}
}
/**
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() {
if (digitalRead(LDR) == HIGH && !forceOff) {
digitalWrite(relayModuleInput_2, LOW);
isRelayModuleOn = true;
} else if (digitalRead(LDR) == LOW) {
digitalWrite(relayModuleInput_2, HIGH);
isRelayModuleOn = false;
}
}