Garage door monitor using Blynk app

I am trying to add a door sensor to my garage door opener. I have successfully installed this code into my photon and the garage door opens and closes as it should, however, I cannot figure out the LED door status indicator. It will not light up when the switch I am using is opened or closed. The trouble I am having is, how do I connect my lead wires into the board? Is the LED widget in Blynk the correct one to accomplish this? Which posts? Is the switch normally open or closed when the garage door is closed? is there any certain sequence to follow when connecting the switch? Thanks in advance for help provided!!

#include "blynk/blynk.h"
#include "SparkCorePolledTimer/SparkCorePolledTimer.h"
char auth[] = “my Auth token";
SparkCorePolledTimer updateTimer(1000); //Create a timer object and set it's timeout in milliseconds
void garageStatus(void);
const int reedSwitch = D2;
const int relaySwitch1 = D0;
const int relaySwitch2 = D3;
int reedStatus = 0;
int ledStatus = 0;
int openTimer = 0;
void setup()
{
Serial.begin(9600);
Blynk.begin(auth);
pinMode(relaySwitch1, OUTPUT);
pinMode(relaySwitch2, OUTPUT);
pinMode(reedSwitch, INPUT);
updateTimer.SetCallback(garageStatus);
reedStatus = digitalRead(reedSwitch);
}
void garageStatus(void)
{
reedStatus = digitalRead(reedSwitch);
if (reedStatus == HIGH) {
ledStatus = 1023;
openTimer++;
}
else {
ledStatus = 0;
openTimer = 0;
}
}
BLYNK_READ(3)
{
Blynk.virtualWrite(3, ledStatus);
}
void loop()
{
Blynk.run();
updateTimer.Update();
}

We would need to know where the reed switches are, and where your magnets are to answer tis question. The switches will be closed when the magnet is in close proximity to the switch. It’s not clear why you are setting two of the pins connected to your switches to OUTPUT instead of INPUT; you should explain what those two switches are for.

Again, this kind of depends on your setup, but in general, I would connect one end of the switch to the pin you want to read, and the other end to the 3.3 v pin. The pinMode should be set to INPUT_PULLDOWN, so it reads LOW when the switch is open and HIGH when it is closed (you could also do it the opposite way, hooking the other end of the switch to ground and setting the pinMode to INPUT_PULLUP).

Thanks Ric for the help. I didn’t word my question very well about the switch position being open or closed. I am using a microswitch and have the option of making it normally open or normally closed when the garage door is closed. Which way should it be set for this application to work best? The output switches D0 and D3 are set as output and wired to the relay. The D2 is input for the reed switch. Also, I have the LED set on V3 in the Blynk app. All of this I got from the comments section in the Makezine article about adding a position sensor to the smartphone garage door. I tried to set the pinMode both ways you you said and still no luck.

Usually you would put a switch the way that in normal position (majority of time) the trigger pin is not activated/actuated.
For the mechanical side of things you might want to opt for a default position that means least strain on mechanical parts (e.g. springs, leavers).
So if your default garage door position is closed, I’d make this the “unactuated” state of the switch in respect to the controller.

What this exactly means for your setup also depends on the mechanical position of the switch and not only on the N/O or N/C contacts of it.

You should be more specific about what “still no luck” means. How was the switch connected when you tested the code, and what result did you get? Is garageStatus being called (you might want to throw in some print statements to find out)? Is the if statement (reedStatus == HIGH) executed?

Thanks Ric, I ended up using a different code and all works well now. Below is the code for others who may have had a similar problem.

// This #include statement was automatically added by the Particle IDE.
#include "blynk/blynk.h"
#include "SparkCorePolledTimer/SparkCorePolledTimer.h"

char auth[] = "change this";

// door 1 sensor and control
const int magSwitch1 = D0;
const int relaySwitch1 = D3;
// door 2 sensor and control
const int magSwitch2 = D1;
const int relaySwitch2 = D4;

// door 1 status
int magStatus1 = 0;
int ledStatus1 = 0;
// door 2 status
int magStatus2 = 0;
int ledStatus2 = 0;

// timeout in milliseconds
SparkCorePolledTimer updateTimer(1000);

void OnTimer(void) {
    sendDoorStatus();
}

void setup() {
    Serial.begin(9600);
    Blynk.begin(auth);
    while (Blynk.connect() == false) {
        // Wait until connected
    }

    pinMode(relaySwitch1, OUTPUT);
    pinMode(relaySwitch2, OUTPUT);
    pinMode(magSwitch1, INPUT_PULLDOWN);
    pinMode(magSwitch2, INPUT_PULLDOWN);

    updateTimer.SetCallback(OnTimer);
}

void sendDoorStatus() {
    Blynk.virtualWrite(V5, ledStatus1);
    Blynk.virtualWrite(V6, ledStatus2);
}

void loop() {
    Blynk.run();

    //constantly monitor the door magnetic switch status (garage door open or closed)
    magStatus1 = digitalRead(magSwitch1);
    magStatus2 = digitalRead(magSwitch2);

    if (magStatus1 == HIGH) {
    ledStatus1 = 255; // 100% brightness
    //Serial.println("LED1: high");
} else {
    ledStatus1 = 0;
    //Serial.println("LED1: low");
}
if (magStatus2 == HIGH) {
    ledStatus2 = 255;
} else {
    ledStatus2 = 0;
}

    updateTimer.Update();
}

Jason, how did you configure your Blynk buttons and what was the wiring schematic for the contact/ reed switch? Thanks for sharing.

I configured the Blynk button to output D3 and as a switch. The LED to input V5. I used the code above and only worried about the switch 1 and relay 1 as I only have a single door and the code is for 2 doors. For the relay I hooked up the corresponding power and ground wire from the relay to 3.3v and the ground posts on the photon. The output D3 on the photon into the input post on the relay. For the sensor switch, one wire to ground on the photon and the other to D0 on the photon. Hope this helps you out and that I explained it correctly. I know very little about these IoT projects and this is the first one I’ve tackled. Hopefully someone with more experience can shed some light on this.

1 Like