#define BLYNK_PRINT Serial
// Overhead garage door and/or Parking sensor sketch
// Send the sketch version information to the gateway and Controller
#include <SPI.h>
#include <ESP8266_PINS.h>
#include <SimpleTimer.h> // Standard BLYNK libraries
#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// Required for LIGHT_SLEEP_T delay mode
extern “C” {
#include “gpio.h” //change to where you have saved gpio.h listed below
}
extern “C” {
#include “user_interface.h”
// uint16 readvdd33(void);
}
char SSID[11] = “******”;
char password[13] = "******;
#define PIR_PIN 5 // SonOff/ESP8266 pin D1/5, tied to PIR (motion) sensor.
unsigned long SLEEP_TIME = 8.64e+7; // Sleep time(1 day) between reads (in milliseconds)
//float distance = 0;
//----------------BLYNK Virtual Pins -----
#define DOOR_MOTION_LED V1 // LED, Motion @ garage Door
#define WINDOW_MOTION_LED V2 // LED, Motion @ Garage Window(s)
#define DOOR_OPEN_LED V3 // LED, Garage Door OPEN
#define DOOR_PARTIALLY_OPEN_LED V4 // LED, Garage Door PARTIALLY OPEN
#define DOOR_CLOSED_LED V5 // LED, Garage Door CLOSED
#define CLOSE_DOOR_RELAY V12 // RELAY, for Opening/closing garage door
#define SOUND_LED V13 // LED, loud Sound detected
#define DOOR_OPENING V14 // Garage Door Opening in CM
#define ALARM_LED V15 // LED, ALARM triggered
// Timer for blynking
SimpleTimer timer;
//********************** Misc Variables **********************
int old_motion = 0; // variable to store a request for sleep
void setup()
{
Serial.begin(9600);
Serial.println(“Entering Setup”);
pinMode(PIR_PIN, INPUT_PULLUP); // PIR (motion) sensor pin
digitalWrite(PIR_PIN, LOW); // disable pullups
// pinMode(GREEN_LED, OUTPUT); // sets the digital pin as output
// pinMode(RED_LED, OUTPUT); // sets the digital pin as output
// pinMode(interruptPin, OUTPUT); //
// pinMode(sleepPin, INPUT); // sets the digital pin as input
// pinMode(wakePin, INPUT);
Blynk.begin("*******", SSID, password);
if(Blynk.connected())
{
Serial.println(“Blynk.connected”);
}
Serial.println(“Exiting Setup”);
// printf(“Buffered, will be flushed”);
// fflush(stdout); // Prints to screen or whatever your standard out is
attachInterrupt(digitalPinToInterrupt(PIR_PIN), PIR_HIGH, RISING); // use interrupt PIR_PIN and run function
}
void loop()
{
Blynk.run();
timer.run();
Serial.println(“Alive & well”);
}
void PIR_HIGH()
{
if (1 != old_motion) {
Blynk.virtualWrite(V1, HIGH); // motion detected
old_motion = 1;
detachInterrupt(digitalPinToInterrupt(PIR_PIN));
attachInterrupt(digitalPinToInterrupt(PIR_PIN), PIR_LOW, CHANGE); // use interrupt PIR_PIN and run function
}
}
void PIR_LOW()
{
// if (0 != old_motion) {
Blynk.virtualWrite(V1, LOW);
old_motion = 0;
// }
sleepNow();
}
void sleepNow() {
// Serial.println(“Entering Sleep”);
// WiFi.mode(WIFI_STA);
// WiFi.disconnect();
// digitalWrite(PIR_PIN, LOW);
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
wifi_fpm_open();
detachInterrupt(digitalPinToInterrupt(PIR_PIN));
attachInterrupt(digitalPinToInterrupt(PIR_PIN), PIR_HIGH, RISING); // use interrupt PIR_PIN and run function
wifi_fpm_do_sleep(0xFFFFFFF);
if(WiFi.status() != WL_CONNECTED)
{
// delay(250);
WiFi.begin(SSID, password);
// Serial.println(“WiFi.connect”);
}
if(WiFi.status() == WL_CONNECTED && !Blynk.connected())
{
// delay(250);
Blynk.connect();
// Serial.println(“Blynk.connect”);
}
// delay(1000);
// Serial.println(WiFi.localIP());
// Serial.println(“Exiting Sleep”);
}
BLYNK_WRITE(V12)
{
Blynk.virtualWrite(V12, param.asInt());
digitalWrite(V12, HIGH);
delay(500);
digitalWrite(V12, LOW);
delay(5000);
BLYNK_READ(V14);
}
BLYNK_READ(V14)
{
//* distance calc not part of the problem
}