Reed switch on RPI3

Hi all!

I’m only new to Blynk and home automation in general. I have managed to get my RPI 3B to control my garage door via a relay, meaning I can remotely open my garage door which is great!

Initially I did this through python and a basic web page, but in discovering Blynk the interface on the phone is WAY better and I have seen some really fantastic projects so far on this platform.

Where I am stuck however is the same as where I was before moving over to Blynk - getting the reed switch to work so that I know the state of the garage door, is it open or closed.

I have searched for reed switch info in general and see a lot on other platforms, but not for RPI with blink so I am feeling lost at see. Being new I have only worked in the app to get the basic open/closed working, and can see some people talking about coding which I have no idea where this is done.

Is anyone able to help me understand what I need to do to get the reed switch working?

Below is my rpi as it is currently wired, with relay working and reed switch not working (Black and white wires)

!

Here’s the reed switch I have…

A reed switch is just a simple contact switch, with either OPEN or CLOSED contacts, depending on the magnetic field.

And just like any normal button or switch, it’s biggest issue is what is called contact bounce… as in opening and closing the contacts rapidly many times in the initial milliseconds of intended closing action.

So any code you need is just basic anti-bounce code, or even better, an anti bounce circuit… Google for both.

Then from there it is just the simple scanning of the digital pin (via timed polling or hardware interrupts - Google for that as well, and if ON do this or if OFF do that (or nothing).

I’ve got a Reed switch installed, the part of the code checking the door (every 0.35 seconds) goes like this:

void checkDoor() {
    
    Doorstatus= digitalRead(sensorDoor);
    if(Doorstatus == HIGH) { 
      
      Blynk.virtualWrite(V8,1);
       
      if(AlarmActive==1){
  
        if (AddEvent==1){
          
         int gmthour = hour();
         if (gmthour == 24){gmthour = 0;}
         String displayhour = String(gmthour, DEC);
         int hourdigits = displayhour.length();
         if(hourdigits == 1){ displayhour = "0" + displayhour;}
  
         String displayminute = String(minute(), DEC);
         int minutedigits = displayminute.length();  
         if(minutedigits == 1){displayminute = "0" + displayminute;} 
   
         String displaysecond = String(second(), DEC);
         int seconddigits = displaysecond.length();  
         if(seconddigits == 1){displaysecond = "0" + displaysecond;}
  
         String displaycurrenttime = displayhour + ":" + displayminute;

         String whatDayIsIt = String(day(), DEC);
         int daydigits = whatDayIsIt.length();
         if(daydigits == 1){whatDayIsIt = "0" + whatDayIsIt;}
         
         String whatMonthIsIt = String(month(), DEC);
         int monthdigits = whatMonthIsIt.length();
         if(monthdigits == 1){whatMonthIsIt = "0" + whatMonthIsIt;}
         
         String whatYearIsIt = String(year(), DEC);
         String displaycurrentdate = whatDayIsIt + "/" + whatMonthIsIt + "/" + whatYearIsIt;
  
         table.addRow(rowIndex,displaycurrenttime,displaycurrentdate);
         table.pickRow(rowIndex);//highlighting latest added row in table
         rowIndex++; 
         
         AddEvent=0;
         Blynk.notify("Puerta abierta: "+displaycurrenttime+" - "+displaycurrentdate);
        }
      }
   }else {
     Blynk.virtualWrite(V8,0);
     AddEvent=1;
    }
}

Everytime the door is opened a notification shows the time and data:

At the same time, a table is filled with that information:

HTH

1 Like

Hi guys, thanks for the comments so far but I am totally lost. From what I gather I need to create a virtual pin but I have no idea how to bring it all together. The relay to open the garage door I can get to work using the Blynk app, but from what I gather I need to write code similar to the above to assign a virtual pin to the reed switch and somehow assign it to the LCD screen in the app. Is anyone able to give guidance on a more simple level? This is my first project.

You can’t connect a physical switch to a virtual pin, it needs to be connected to a physical pin and you need to add code to your device to read the state of that switch.

The problem is that the RPI isn’t the easiest platform to use for this. It’s a massive overkill for the task and there aren’t that many people on this forum that use the Pi for this type of solution. Most people would use a NodeMCU for this type of task and write the code in C++.

Pete.

As @PeteKnight said… the RPI, while supported, is not well documented with Blynk.

Mostly you, alone in the dark, lit by LCD backlight, looking at the limited examples and spending hours experimenting with syntax… oh, wait, that is me not you :blush:

You have two primary choices that seem the best option for that hardware with Blynk

NodeJS - https://github.com/vshymanskyy/blynk-library-js - I have some JS examples here - NodeJS Blynk - Code Examples for Basic Tasks (Work in Progress)

Python - https://github.com/vshymanskyy/blynk-library-python - I have posted a few Python examples in this forum, but have not compiled any into a topic yet.

Shifted to a NodeMCU to learn from there, and it’s going well so far! Thanks for the comments!

2 Likes