Last active
April 21, 2019 17:47
-
-
Save angipp01/28f2540ff730830493fae97d50393631 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************* | |
Download latest Blynk library here: | |
https://github.com/blynkkk/blynk-library/releases/latest | |
Blynk is a platform with iOS and Android apps to control | |
Arduino, Raspberry Pi and the likes over the Internet. | |
You can easily build graphic interfaces for all your | |
projects by simply dragging and dropping widgets. | |
Downloads, docs, tutorials: http://www.blynk.cc | |
Sketch generator: http://examples.blynk.cc | |
Blynk community: http://community.blynk.cc | |
Follow us: http://www.fb.com/blynkapp | |
http://twitter.com/blynk_app | |
Blynk library is licensed under MIT license | |
This example code is in public domain. | |
************************************************************* | |
Blynk using a LED widget on your phone! | |
App project setup: | |
LED widget on V1 & V2 | |
*************************************************************/ | |
/* Comment this out to disable prints and save space */ | |
#define BLYNK_PRINT Serial | |
#include <ESP8266WiFi.h> | |
#include <BlynkSimpleEsp8266.h> | |
// You should get Auth Token in the Blynk App. | |
// Go to the Project Settings (nut icon). | |
char auth[] = ""; | |
// Your WiFi credentials. | |
// Set password to "" for open networks. | |
char ssid[] = ""; | |
char pass[] = ""; | |
// Select your pin with contact | |
const int btnPin1 = D1; | |
const int btnPin2 = D2; | |
//Garage Door Open Timer setup | |
double openForTooLongInMins = 15; //Change this timer to push notification. Currently set at 15mins | |
int doorOpenedAtTimeInMills = 0; | |
int doorOpenDurationInSeconds = 0; | |
bool messageSentInThisOpening = false; | |
const int doorOpen = HIGH; | |
const int doorClosed = LOW; | |
WidgetLED led1(V1); //Match to the LED from the Blynk app | |
WidgetLED led2(V2);//Match to the LED from the Blynk app | |
BlynkTimer timer; | |
// V1 LED Widget represents the physical contact state | |
boolean btnState1 = false; | |
void buttonLedWidget1() | |
{ | |
// Read contact | |
boolean isPressed = (digitalRead(btnPin1) == LOW); | |
// If state has changed... | |
if (isPressed != btnState1) { | |
if (isPressed) { | |
led1.on(); | |
} else { | |
led1.off(); | |
} | |
btnState1 = isPressed; | |
} | |
} | |
// V2 LED Widget represents the physical contact state | |
boolean btnState2 = false; | |
void buttonLedWidget2() | |
{ | |
// Read contact2 | |
boolean isPressed = (digitalRead(btnPin2) == LOW); | |
// If state has changed... | |
if (isPressed != btnState2) { | |
if (isPressed) { | |
led2.on(); | |
} else { | |
led2.off(); | |
} | |
btnState2 = isPressed; | |
} | |
} | |
void resetDoorOpenCounter() { | |
doorOpenDurationInSeconds = 0; | |
messageSentInThisOpening = false; | |
} | |
void checkOpen() { //Push notification action door 1 | |
if ( digitalRead(btnPin1) == HIGH ) { | |
doorOpenDurationInSeconds += 5; | |
} | |
if (digitalRead(btnPin1) == LOW) { | |
} | |
int isButtonPressed = !digitalRead(btnPin1); | |
if (isButtonPressed && doorOpenDurationInSeconds > openForTooLongInMins * 60 ) { | |
Blynk.notify("Garage door open"); | |
doorOpenDurationInSeconds =0; | |
// todo: this does not know if it sent successfully. needs work | |
messageSentInThisOpening = true; | |
} | |
} | |
void checkOpen2() { //Push notification door 2 | |
if ( digitalRead(btnPin2) == HIGH ) { | |
doorOpenDurationInSeconds += 5; | |
} | |
if (digitalRead(btnPin2) == LOW) { | |
} | |
int isButtonPressed = !digitalRead(btnPin2); | |
if (isButtonPressed && doorOpenDurationInSeconds > openForTooLongInMins * 60 ) { | |
Blynk.notify("Garage door open"); | |
doorOpenDurationInSeconds =0; | |
// todo: this does not know if it sent successfully. needs work | |
messageSentInThisOpening = true; | |
} | |
} | |
void setup() | |
{ | |
// Debug console | |
Serial.begin(11500); | |
Blynk.begin(auth, ssid, pass); | |
// You can also specify server: | |
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80); | |
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080); | |
// Setup physical button pin (active low) | |
pinMode(btnPin1, INPUT); | |
attachInterrupt(digitalPinToInterrupt(btnPin1), checkOpen, CHANGE); | |
pinMode(btnPin2, INPUT); | |
attachInterrupt(digitalPinToInterrupt(btnPin2), checkOpen2, CHANGE); | |
timer.setInterval(500L, buttonLedWidget1); | |
timer.setInterval(500L, buttonLedWidget2); | |
timer.setInterval(1000000, checkOpen); //Time will go up to 16mins because push notification was set at 15mins | |
timer.setInterval(1000000, checkOpen2); //Time will go up to 16mins because push notification was set at 15mins | |
} | |
void loop() | |
{ | |
Blynk.run(); | |
timer.run(); | |
} |
Something is going wrong with the timers. My Blynk app eventually goes offline.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I edited the timers for push noticiations. I haven't tested it yet, but I am guess this is why my notification was not pushing because the timer was set too low.
timer.setInterval(1000000, checkOpen); //Time will go up to 16mins because push notification was set at 15mins
timer.setInterval(1000000, checkOpen2); //Time will go up to 16mins because push notification was set at 15mins