BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Garage door project with WeMos D1 mini + sheilds + Blynk


#1

Hello Blynkers, I have a garage door project that I need help with. I am teaching my self so it very well could be an obvious answer. I have a WeMos D1 mini with a DHT sheild and a Relay shield. The project works fine as of now but I wanted to add a notification when the door is left up for longer than 10 or 15 minutes. I have seen other code that allows a notification if a temperature gets to high or at a certain time with the timer, but I was wanting it to realize the change in the switch and start counting till 30 minutes and then let me know I left the door up. The code I putting up is the very basic that works as of now to open and close the door with a relay, read the temperature status and show the status of the magnetic switch I have on the door. The Magnetic switch is on D2 and when its low the switch is together and when its high then the switch is broken and the door is up. I will also show a picture of the code that i tried to implement but it will only send me an alert as soon as the switch is broken. I would just want it on a timed delay to notify me. Any help would be greatly appreciated!!


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxx";
#define DHTPIN D4          
#define DHTTYPE DHT22     
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(true); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup() {
  // put your setup code here, to run once:
Blynk.begin(auth, ssid, pass);


  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop() {
  // put your main code here, to run repeatedly:
 
  
 Blynk.run();
 timer.run();
}



#2

Why you posted a screenshot of your code instead your code? I can help you but i don’t work with screenshots…


#3
indent preformatted text by 4 spaces[quote="trentonf4i, post:1, topic:26478, full:true"]

Hello Blynkers, I have a garage door project that I need help with. I am teaching my self so it very well could be an obvious answer. I have a WeMos D1 mini with a DHT sheild and a Relay shield. The project works fine as of now but I wanted to add a notification when the door is left up for longer than 10 or 15 minutes. I have seen other code that allows a notification if a temperature gets to high or at a certain time with the timer, but I was wanting it to realize the change in the switch and start counting till 30 minutes and then let me know I left the door up. The code I putting up is the very basic that works as of now to open and close the door with a relay, read the temperature status and show the status of the magnetic switch I have on the door. The Magnetic switch is on D2 and when its low the switch is together and when its high then the switch is broken and the door is up. I will also show a picture of the code that i tried to implement but it will only send me an alert as soon as the switch is broken. I would just want it on a timed delay to notify me. Any help would be greatly appreciated!!


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxx";
#define DHTPIN D4          
#define DHTTYPE DHT22     
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(true); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup() {
  // put your setup code here, to run once:
Blynk.begin(auth, ssid, pass);


  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop() {
  // put your main code here, to run repeatedly:
 
  
 Blynk.run();
 timer.run();
}



[/quote]

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxl";
#define DHTPIN D4          
#define DHTTYPE DHT22     
DHT dht(DHTPIN, DHTTYPE);
int inputPin = D2:
BlynkTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(true); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup() {
  pinMode(inputPin, INPUT);
Blynk.begin(auth, ssid, pass);


  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop() {
  if(digitalRead(inputPin == HIGH > 10000){
   Blynk.notify("Garage Door - Put Me Down!");
 }
 
  
 Blynk.run();
 timer.run();
}

#4

Sorry, that is the code after I added what I thought would work. I Figured if i told the code the D2 pin was an input and then told it that if the input was HIGH and on longer than a couple seconds to alert me. It seems to alert me but as soon as the pin goes HIGH. No delay. Thank you again for any help!!


#5

Putting code in your void loop like that isn’t good idea with Blynk.

I would do the follow8ng (I’m not going to write the code for you, so do t ask)

  1. Add a variable that stores the last value of the reed switch on D2. Initialise this variable to LOW in void setup. We’l call this LastDoorState.
  2. Create a timer that checks your D2 pin to see if it’s HIGH. This timer could run every few seconds, it doesn’t need to be more often than that.
  3. If when D2 is checked it’s HIGH and the value of LastDoorState was LOW then set LastDoorState to HIGH and start a timer for 30 minutes. When this timer completes it should call a new function, wel’ll call this SendNotification. and set the last value of D2 variable to HIGH.
  4. Create the SendNotification function to send your ”door has been left open” message.

Doing it this way will ensure that only one notification is sent, regardless of how long afte the 30 minutes the door stays open for.
You may also want to add a LED widget that shows whether the door is closed (off or green) and open red). You can update this when you’re reading the value of D2, with an if statement.

I’d also try to move away from using the D numbers printed on the Wemos D1 Mini and use GPIO numbers instead D2 = GPIO4, D4 = GPIO2. That way, your code is more portable between devices. By all means add a comment to tell you which D pin to connect your wires to, but stick with the GPIO numbers in the code.

Pete.


#6

Thank you Pete, That gives me alot to go on and narrow down my research. I can grasp adding the variable and LastDoorState but those timers are a little beyond me at this point (very beginner) so Ill do some reading and work on it. The LED widget was a thought, I would also like to add an RFID to the outside of the door to allow entry after I get the hang of this. I read that I should use the GPIO pins and also I came across someone having issues with certain pins not working properly due to other functionality? Anything else like that I should stay away from???
Thank you again for any help! Ill update after i read and try to add Pete’s suggestions!


#7

@Gunner’s tutorial/examples of how to use timers is a good starting point:

This is also an excellent reference of which GPIO pins to avoid:

I’ve done some work with RFID recently and it’s pretty easy to do when you get the hang of it, but most weatherproof RFID readers need a 12v supply and use 5v logic levels (the Wemos uses 3.3v logic levels) so you’ll need to use a logic level shifter and have a 12v supply available (which shouldn’t be that difficult in a garage with mains voltages available).

I just made a new controller for my door entry system that will be used on my front gate. I mounted the logic level shifter and a 12v to 5v regulator on a Wemos sized prototyping board, along with sockets for the RFID reader (bottom) and 12v power + reset button so that it can plug in to a Wem,os tripple base along with the WEmos D1 Mini Pro and the relay that will activate the gate release.

I’m using the Wemos Pro because it will all be mounted in an aluminium box, so I need an external aerial to allow the Wemos to pick up a decent Wi-Fi signal from the house.

Pete.


Help - NodeMCU
#8

Nice looking project!! I went down a rabbit hole with that logic level shifter lol never seen one of those yet. But anyway I have read and gone over multiple projects but I cant seem to piece together how to assign this time variable! So based off Pete’s suggestions I did the following … I defined the GPIO pin of the Magnetic Switch, i listed the integer of LastDoorState so the code knows where the door is. I added a VirtualWrite so it tells Blynk what the LastDoorState is (just for me to try out V pins). In Setup I told it that the switch was an input and that the last door state should be LOW at the beginning. Now the Loop code is what gets me and where i get errors … in my head it reads that the int should be the last door state and that state is grabbed off of the Magswitch pin that i defined. then the If statement should be if the last door state was high then start a timer.interval for 10 seconds and send notification after that 10 seconds. I have tried Timer.countdown, timer.timeout, timer.setinterval … and all with errors. Im thinking maybe i need a Void.timer but i cant seem to find anything reading on what that is or does. I tried all the variations in Gunners examples but still errors. here is what i left off with lol

#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
char auth[] = "xxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxx";
#define DHTPIN D4   // pin of the DHT shield        
#define DHTTYPE DHT22
#define MagSwitch 4 // GPIO of the magnetic switch for door
int LastDoorState;  // state of the door   
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(true); // or dht.readTemperature(true) for Fahrenheit


  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  Blynk.virtualWrite(V7, LastDoorState);
}

void setup() {
  pinMode(MagSwitch, INPUT);
  LastDoorState = LOW;
Blynk.begin(auth, ssid, pass);


  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop() {
  int LastDoorState = digitalRead(MagSwitch);
  
    if(LastDoorState = HIGH){
      timer.setInterval(10000L); 
       Blynk.notify("Garage Door - Put Me Down!");
    }

     
 Blynk.run();
 timer.run();
}```

#9

A few things that I can see wrong with this:

  1. LastDoorState is already declared as a global variable it type Integer at the top of your code. Having int in front of it here is re-declaring it, and clearing it’s contents.

  2. if(LastDoorState = HIGH) is setting LastDoorState to HIGH becaue you only have one equals symbol.

  3. you’re reading the digital pin and processing the if statement every time thye void loop executes. You should really put this in a separate function and call it with a timer.

Fixing (1) and (2) would look like this…

void loop() {
  LastDoorState = digitalRead(MagSwitch);   // int declaration removed by Pete.

    if(LastDoorState == HIGH){              // Second equals symbol added by Pete.
      timer.setInterval(10000L); 
       Blynk.notify("Garage Door - Put Me Down!");
    }

 Blynk.run();
 timer.run();

If you start getting Blynk disconnections then do what I suggested in (3).

Pete.


#10

So after a couple weeks of going other other code and videos … its working with the notification. Let me explain, I found a YouTube video of a similar project that had the timed notification. I reached out and he sent me his code so that I could see exactly what needed to happen. After understanding what needed to happen this is the result. The code reads the magnetic switch every second and if it goes high then it starts a timer, when the timer hits 2000 as I have defined then it will send the notification and then reset the timer to 0. The reason I do this is so I only get one notification every 28 minutes. If the switch is low then the timer stays at 0.

Here is the link to his youtube video that helped me! https://youtu.be/kB-XWPtbxsI

Thank you to Pete that help point me in the right direction and BlueDevilNation4 for the code example

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
char auth[] = "xxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxxl";
#define DHTPIN D4                                  // Digital pin of the DHT shield        
#define DHTTYPE DHT22                              // What DHT sensor Im using
#define MagSwitch 4                                // GPIO of the magnetic switch for door
int DoorUpTime = 2000;                             // time that I want the door to be up before it sends the notification
int DoorCount = 0;  
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer Temp;                                   // Timer for Blynk to look at the sensor data
BlynkTimer Door;                                   // Timer for Blynk to check the MagSwitch state


void sendSensor()                                 // Code for the DHT sensor to compute and send the information to virtual pins
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(true);             // or dht.readTemperature(true) for Fahrenheit


  // You can send any value at any time. Please don't send more that 10 values per second.
  
  Blynk.virtualWrite(V5, h);                      // Value that shows me the humidity
  Blynk.virtualWrite(V6, t);                      // Value that shows me the temperature
  Blynk.virtualWrite(V7, DoorCount);              //Value that shows me the time the door has been up  
}
void countDoor(){                                 // Code for Door Alert
  
 if (digitalRead(MagSwitch) == HIGH) {            // If the Magnetic swith is HIGH 
  DoorCount++;                                    // Then start timer that goes up by 1
  } 
   if (DoorCount > DoorUpTime) {                  // If the DoorCount Timer is greater than DoorUpTime which was defined as 2000
    Blynk.notify("The Garage Door is Open!");     // Notify me the door is open
     DoorCount = 0;                               // Reset thee timer back to 0. This ensures I only get one notification about every 28 minutes. 
    }
   if (digitalRead(MagSwitch) == LOW) {           // If the Magnetic switch is LOW
  DoorCount = 0;                                  // Reset the timer to 0
  } 
  } 
void setup() {
Blynk.begin(auth, ssid, pass);

 pinMode(MagSwitch, INPUT);                       // Tell the code your defined MagSwitch is an input pin

  dht.begin();

  // Setup a function to be called every second
  Temp.setInterval(1000L, sendSensor);           
  Door.setInterval(1000L, countDoor);             
}

void loop() {
         
 Blynk.run();
 Temp.run(); 
 Door.run(); 
}



#11

Looking at the code, it appears that it waits 28 minutes before sending the first “door open’ notification. Is this what you want?
I would have thought it would be better to get your first notification after say 5 minutes (which seems like a sensible amount of time to get your car in/out or fetch some item from the garage). In my neighbourhood, if a garage door was left open for 28 minutes without someone standing guard then everything in the garage would have disappeared, including that garage door!

Pete.


#12

LOL yea its fine for me! I left it open all night one night so thats why I needed a timer on it. ( Thank God nothing was stolen) This has helped me alot with understanding time in arduino so I can change it if need be. I like it this way because ill get on notification every 28 mins if the door is never put down. I had it with just alerting me until I put the door down but that was way to many alerts!! if i go to mow the lawn or something i will only get 3 alerts max vs 50 + alerts. Or next to add to my list of learning is a disable notifications button!!

Again thanks for all the help!!!


#13

Use a global Boolean variable as a flag maybe call it MuteAlerts and set it to false when it’s declared.
In the code that’s called by your “Mute Alerts” widget switch, if the value coming from Blynk is a “1” then set your MuteAlerts variable to true, else false.

Before you send your alert notification, check if MureAlerts is false. If it is then send the alert, if not then don’t.

Pete.


#14

You’ll be able to get what you need from my water tank level project. Water Tank Level Indicator with Low Level Warning Notifications