[SOLVED] Particle Photon Garage Door Opener w/ Reed Switch Status

Hello fellow Blynkers. First off, thank you for all the help as I’ve being reading all the postings on similar projects. I’ve been able to piece together a working program, but in a move that was obviously too greedy, I somehow broke my code. I had been trying to write the garage door status to another virtual pin and display the status as a string variable (Open/Closed) in Blynk app. It didn’t work – and after removing my new code, the old working code is broken. I am hoping someone can take a quick look and help me find the problem. It is verifying, compiling, and flashing fine. The led widget is no longer reading the value off of the virtual pin correctly.

// This #include statement was automatically added by the Particle IDE.
#include “SparkCorePolledTimer/SparkCorePolledTimer.h”

// This #include statement was automatically added by the Particle IDE.
#include “blynk/blynk.h”

char auth[] = “token”;

SparkCorePolledTimer updateTimer(1000); //Create a timer object and set it’s timeout in milliseconds
void garageStatus(void);

const int reedSwitch = D7;
const int relaySwitch1 = D0;

int reedStatus = 0;
int ledStatus = 0;
int openTimer = 0;

void setup()
{
Serial.begin(9600);
Blynk.begin(auth);
pinMode(relaySwitch1, OUTPUT);
pinMode(reedSwitch, INPUT_PULLDOWN);
updateTimer.SetCallback(garageStatus);
reedStatus = digitalRead(reedSwitch);
}

void garageStatus(void)
{
reedStatus = digitalRead(reedSwitch);
if (reedStatus = LOW) {
ledStatus = LOW;
openTimer++;
}
else {
ledStatus = HIGH;
openTimer = 0;
}
}

BLYNK_READ(3)
{
Blynk.virtualWrite(3, ledStatus);
}

void loop()
{
Blynk.run();
updateTimer.Update();
}

Here are some more photos to help convey the setup. The D7 LED seems to function correctly (goes on and off based on the reed switch). However, the value is not getting passed into the Blynk virtual pin (v3).

Might I be able to connect the setup to my computer and use the desktop IDE to open a serial monitor and observe the values? Just not sure where to go from here in trying to pinpoint the issue.

yes debugging with Serial should help a lot…

Trying to add some code to print the serial values out. Trying this, but it does not compile.

Serial.println(ledStatus " Status");
Serial.println(reedStatus " ReedStatus");

No matter where I put it, it seems to throw an error.

I was able to get serial to work. This was the code I used:

Serial.println("Hello world! ");
Serial.println(ledStatus);
Serial.println(reedStatus);
}

It prints out:

Hello world!
0
1

It stays that way when I open and close the switch. Doesn’t seem to be changing. Any ideas?

I think you need to add this to the top area:
WidgetLED myled(V3);

and in here do this:

void garageStatus(void)
{
   reedStatus = digitalRead(reedSwitch);
   if (reedStatus = LOW) {
      myled.off();
      openTimer++;
   }
   else {
      myled.on();
      openTimer = 0;
   }
}

See this: Blynk LED Example

Thanks for the reply tyler785, I’ll try that.

I was thinking I might need to define something in the setup (like the below) in order to convert the 0 and 1 signal to HIGH or LOW:

#define HIGH 0x1
#define LOW 0x0

Needs to be == LOW not = LOW.

Yup, I missed that. That would cause it not to work.

For sure, = LOW means set the variable to LOW, whereas == is checking the variable for LOW.

You would think that by now a decent IDE would show a warning if you have a line with if and a single = sign as it is almost always an error.

1 Like

Costas! Thank you. I’ll try this tonight when I get home.

I really appreciate the help Tyler and Costas.

So, we’ve got some progress!

Changed the
if (reedStatus = LOW)

to

if (reedStatus == LOW) {

and I’m now getting the serial to print 0 when the garage is open and 1 when the garage is closed. I can also get the 1 or 0 to appear on the Blynk Value Display widget.

I am still not able to get the Blynk LED widget to light up. I set it to the correct (V3) pin, but it doesn’t change when it moves from Low (0) to High (1).

Look for the official LED sketch and try that. Unless you use #define LOW 0 and #define HIGH 255 you can’t use HIGH and LOW in your sketch for LED’s.

Costas - you are a saint. Changed my code to the below and am now able to monitor the status correctly. Thank you everyone for your help. I hope to pay it forward as I get more adept at this stuff.

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

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

char auth[] = "token";

SparkCorePolledTimer updateTimer(1000); //Create a timer object and set it's timeout in milliseconds

void garageStatus(void);

const int reedSwitch = D7;
const int relaySwitch1 = D0;

int reedStatus = 0;
int ledStatus = 0;
int openTimer = 0;

void setup()
{
Serial.begin(9600);
Blynk.begin(auth);
pinMode(relaySwitch1, OUTPUT);
pinMode(reedSwitch, INPUT_PULLDOWN);
updateTimer.SetCallback(garageStatus);
reedStatus = digitalRead(reedSwitch);
}

void garageStatus(void)
{
reedStatus = digitalRead(reedSwitch);
if (reedStatus == LOW) {
Blynk.virtualWrite(3, 255);
openTimer++;
}
else {
Blynk.virtualWrite(3, 0);
openTimer = 0;
}
Serial.println("Garage Status ");
Serial.println(ledStatus);
Serial.println(reedStatus);
}

void loop()
{
Blynk.run();
updateTimer.Update();
}
1 Like

countless times I fell in that trap!

1 Like