Blynk+ Wemos + Reed Switch + Relay for Garage Door Opener

I am currently trying to work on another garage door controller / status project. I know there are many such projects out there.

I am able to get the status of the reed switch without any problem on the blynk app but unable to trigger relay to open or close the garage door.

Please help. I have been on these for the last 4 hours! cant seem to get it to work

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

bool current = 0;
bool previous = 1;
#define relayPin D5

char auth[] = "";

char ssid[] = "PIKIN-2";
char pass[] = "";

BlynkTimer Mytimer; 

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  Mytimer.setInterval(1000L, switchstate); 
  pinMode(D7, INPUT_PULLUP); //DOOR
}


void switchstate(){
  current = digitalRead(D7);
  if (current != previous){
    previous = current;
    if (current == LOW)
          {
              Blynk.virtualWrite(2, "Door closed.");
              Blynk.notify("Your Door is closed!!");
          }
          else
         {
               Blynk.virtualWrite(2, "Door OPEN!");
               Blynk.notify("Your Door is OPEN!!");
         }          
  }
}


int value = LOW;

void program() {  
  if (current == LOW){
    digitalWrite(relayPin, HIGH);   // Turn the RELAY on (Note the Relay is active high
    value = LOW;
  }
else
{
    digitalWrite(relayPin, LOW);   // Turn the RELAY on (Note the Relay is active high
    value = HIGH;
  }
}

void loop()
{
  Blynk.run();
 Mytimer.run();
}

You should look through them all for hints and tips… such as…

Notifications have a 1 per 15 second limitation… you code has nothing to prevent multiple notification back to back

pinMode() should be set early in the setup, not last

Best to use Arduino(GPIO) numbering instead of the silk screened labels

And… well… better programming layout… I am not following yours very well I am afraid. You have a timer that seems to watch your switch and send too many status updates every second… but that is all your program seems to do. Nothing ever calls the supposed relay function.

PS, this forum is really about teaching about Blynk, not general programming. Lots of sites out there for that. Find some examples, learn how they work, then look at adding in Blynk functionality.

The code in void program() is never getting called.
It’s a stand-alone piece of code which won’t run on its own, so your relay will never be activated.

Pete.

how about the reed switch status part of the code, is that atleast making sense? I know it works on the blynk app but does the logic of the code and its flow make sense to your fresh eyes.

It’s not the way that I’d choose to write that bit of code, but them I’m a self-taught bodger.
These read switches can suffer from contact bounce, but it depends how often you poll the switch using the timer as to whether this will be an issue. You’re currently checking the switch every second, but this doesn’t seem very often. Is the read switch being used to sense the position of the door so that you know when to stop the motor running, or simply to sense if the door is open/closed. If it’s the latter then once per second (or even less frequently) should be fine. However, if it’s meant to act as a limit switch then you may not want to allow the motor to run for upto an additional second once it reaches the limit point.

Pete.

my intent with the reed switch is only to check status. if i see while i am away that the door is open somehow i want to actuate via blynk through a relay to close the door.

i have modified the code to new one. Still the virtual led on the blynk app changes color to show the currently status of the door ( red color for door open and green color for door closed) but the relays do not actuate or click. the 2 channel relay were expensive so i bought two of 1 channel relay.

The wiring setup is as follow

reed switch is hooked up to D3 & gnd
Relay 1 is hooked up to D5, 5V & gnd
Relay 2 is hooked up to D6, 5V & gnd
Relay 1 and 2 are jumpered at the middle connection ( common)
Both relay have built in physical leds ( one green and other red…but are always on)

virtual led on blynk is working fine like i previously said


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

char auth[] = "";

char ssid[] = "PIKIN-2";
char pass[] = "";

WidgetLED led1(V1);

#define BLYNK_GREEN   "#23C48E"
#define BLYNK_RED       "#D3435C"


SimpleTimer timer;

void blinkLedWidget()
 
 {
 if (digitalRead(D3)) {  

    led1.setColor(BLYNK_RED);  // magnetic switch is open, D3 is HIGH
    Serial.println("Door is open");

  } else {

    led1.setColor(BLYNK_GREEN);  // magnetic switch is closed, D3 is LOW
    Serial.println("Door is closed");

  }

}



void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  pinMode(D6, OUTPUT);   // connects to D1 of Relay
 pinMode(D5, OUTPUT);   // connects to D2 of Relay
  pinMode(D3, INPUT);    // connects to NC Magnetic Reed Switch


// Turn LED on, so colors are visible
  led1.on();
  timer.setInterval(1500L, blinkLedWidget);
}

void loop()
{
  Blynk.run();
  timer.run();
}

You still have pinMode() after Blynk.begin() which is a blocking command until connected… pinMode() should be first in setup as it can affect any devices connected to the pins at bootup.

D3 (GPIO0) is probably not a good pin to use for your input… see this chart for optimal pins to use first.

Use external power (with shared GND) for the relays… the Wemos 5v pin may not have the current to properly engage the coils.

GPIO 12 & 14 start HIGH then settle LOW… are your relays triggered on a HIGH or LOW?

There are no instructions here to activate the relays.
I’d expect to see something like this…

void blinkLedWidget()

 {
 if (digitalRead(D3)) {  
    led1.setColor(BLYNK_RED);  // magnetic switch is open, D3 is HIGH
    digitalWrite(D5,HIGH);
    Serial.println("Door is open");

  } else {
    led1.setColor(BLYNK_GREEN);  // magnetic switch is closed, D3 is LOW
    digitalWrite(D6,LOW);
    Serial.println("Door is closed");
  }
}

It depends on whether your relays are active HIGH, or LOW, and how they are supposed to be triggered in relation to each other.

Pete.

i got the code from here

the only bit of instruction on it related to the relay was as follows " On the relay, the commons are connected with a jumper wire. The use the NO connection from D1 and NC connection from D2 to connect to the physical garage door opener. This way if the power is lost on the project, once power is restored, it will no cause the door to open. Also be sure to use the 5volt connection from the WeMos to provide power to the relay…it requires 5vdc and will not work with the 3.3v output."

@rav527 I think you were on the right track with the code in your first post. You just need to restructure your setup() as @Gunner had mentioned (put PinMode() before Blynk.begin() and the timer).

I think this potion of code should keep you from sending too many notifications (unless you are opening and closing your garage every 15 seconds) You could even add the virtual LED stuff to your IF statement.

Next is to think of how the current button opens/closes your garage door. Most (if not all) I have seen just require a short push of a button to open/close. So this is what you will want to simulate with your relay/code. Using the existing button on the wall for local control makes things easier as you wont need to add code for monitoring a button (if there currently isn’t one then we can address that next). So to open/close your garage from the BLYNK app you will need the button widget set to switch and set it up so that it send the HIGH signal when pressed as your relays are active HIGH. In the code you will just need to monitor the BLYNK Button and take appropriate action when pressed, this will include sending the high signal to the relay for a short duration (approx. .5 second) then sending the LOW signal to deactivate it (like pushing the button on the wall) and finally updating the status of the button widget. Here is a snipet of code that should do the above functions.

BLYNK_WRITE(V0) //Button Widget set to Virtual pin 0, set to switch
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1)
  {
     digitalWrite(13, HIGH); // Set GPIO 13 HIGH, relay ON
     timer.setTimeout(500L, []() {  // Run after 0.5 seconds second
          digitalWrite(13, LOW);  // Set  Pin 13 LOW, relay OFF
          Blynk.virtualWrite(V0, LOW);// Set  Virtual Pin 0 LOW
           });  // END Timer Function

}

As for wiring, just tap off the existing button. One wire to the common and the other to the normally open.

Hope this all makes sense, and is of some help. My BLYNK garage door monitor/opener was one of my first projects and I use it almost daily. It got me hooked. BLYNK ON my friend.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

bool current = 0;
bool previous = 1;

char auth[] = "";
char ssid[] = "PIKIN-2";
char pass[] = "";

BlynkTimer timer; 

void setup()
{
  Serial.begin(9600);
  pinMode(13, INPUT_PULLUP); //reed sensor connection D7
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, switchstate); 
  }


void switchstate(){
  current = digitalRead(13);
  if (current != previous){
    previous = current;
    if (current == LOW)
          {
              Blynk.virtualWrite(2, "Door closed.");
              Blynk.notify("Your Door is closed!!");
          }
          else
         {
               Blynk.virtualWrite(2, "Door OPEN!");
               Blynk.notify("Your Door is OPEN!!");
         }          
  }
}

// Relay connection as follows: 5v to 5v, gnd to gnd, In1 to D5( gpio14) 
BLYNK_WRITE(V0) //Button Widget set to Virtual pin 0, set to switch
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1)
  {
     digitalWrite(14, HIGH); // Set GPIO 14 HIGH, relay ON
     timer.setTimeout(500L, []() {  // Run after 0.5 seconds second
          digitalWrite(14, LOW);  // Set  Pin 14 LOW, relay OFF
          Blynk.virtualWrite(V0, LOW);// Set  Virtual Pin 0 LOW
           });  // END Timer Function
}
}

void loop(){
  Blynk.run();
 timer.run();
}

Still no luck my friend. my first project was sump level monitor and fell in love with IOT world, this is my second project.

Try adding this to your setup()

pinMode(14, OUTPUT); //Relay Connected to D5 (GPIO14)

Thats also a no go. Any chance you can share your code from your first project? Did you also use wemos + blynk + reed + relay for your project?

I will have to see if I can dig it up. That project was some time ago. To be honest though, I don’t see why your code wont work.

Can you give a little detail as to what it is doing currently? Is the reed switch still working? Does the relay click when you push the app button?

reed switch works correctly. the relay does not click.

the relay yellow light comes up as soon as i hook up 5v and gnd to it. The red light as well as click sound comes up as soon as the wemos board boots up. After that no matter how many times i press the button on the app, it does not do anything. both light stay on and thats about it.

Two things to try. Try adding a

digitalWrite(13, HIGH);

To your setup ()

Or maybe relay is actually active LOW. Then switch the HIGH and LOW in the BLYNK_WRITE function.

none of that work. I tried removing the relay from the circuit and instead adding a led to the gpio that sending signal to relay and it lights up when i press the button on the app.

i have four of these relays and all are behaving same so dont think all the relays are defective also.

Well it sounds like the code is working (if it is flashing the LED when you press APP button), and I do not see anything wrong with the code. So it must be a hardware issue. Would you happen to have a link to the relays?

I will point this out again :wink: As por power can sometimes make relays click, but not properly work otherwise.

Also sounds like the relay is tripping (at least part way) into the Activated condition upon boot (may be Active LOW relays). reverse your pin logic so that instead of going HIGH when pressing button, it goes LOW.

1 Like

ok tried give the relay seperate power as well as reversing the logic and it does not work still.

f*** this is pissing me off