New to Coding - Need a little help - Particle Photon - Garage Door

Hey folks - I have been working on a project to control my garages from my google assistant using ifttt and I have most of it working, I need a little guidance.

I have my garage code working ok. I am able to call on the two functions in ifttt and it works correctly after some debugging.

I’d like to add a function that would open both garages at the same time. I thought the cleanest way would be to create a new function and have my pinmode for both D0 and D3 fire high when the function is called.

I added the new function “Both”, Its only working for 1 door (the D0).

Is there a special way I need to sequence that part of the code? I tried doing some delays, it didn’t work.

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

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

// * I created this for the particle photon
// * to connect  photon to blynk and use it to open garage doors on 2 channel relay
// *
// **************************************************************/
// This is my auth token, this gets generated when you create a project in blynk and get the auth emailed to you
char auth[] = "authcode";
int OpenGarage (String command);
int OpenGarage2 (String command);
int Both (String command);
void setup()
{
  
  Particle.function("OpenGarage", OpenGarage);
  Particle.function("OpenGarage2", OpenGarage2);
  Particle.function("Both", Both);
  Serial.begin(9600);
  Blynk.begin(auth);
  pinMode(D0, OUTPUT);
  pinMode(D3, OUTPUT);

}
void loop()
{
    Blynk.run();    
}
//This Opens single car garage
int OpenGarage(String command)
{
    if(command == "OpenGarage")
    {
        digitalWrite(D0, HIGH);
        delay(1000);
        digitalWrite(D0, LOW);
        return 1;
    }
   else  
   {
        digitalWrite(D0, LOW);
        return 0;
   }

    return -1;
}
//This Opens two car garage
int OpenGarage2(String command)
{
    if(command == "OpenGarage2")
    {    
        digitalWrite(D3, HIGH);
        delay(1000); 
        digitalWrite(D3, LOW);
        return 1;
    }
    else
    {
        digitalWrite(D3, LOW);
        return 0;
    }
    return -1;
}
//This opens both garage
int Both(String command)
{
    if(command == "Both")
    {
        digitalWrite(D0, HIGH);
        digitalWrite(D3, HIGH);
        delay(1000);
        digitalWrite(D0, LOW);
        digitalWrite(D3, LOW);
        return 1;
    }
   else  
   {
        digitalWrite(D0, LOW);
        digitalWrite(D3, LOW);
        return 0;
   }

    return -1;
}

my50 Ct: you should delete your blynk code in your code snip

Hi Tom,
I just flashed your code in a photon. I commented out blynk and measured the outputs with a voltmeter.
If I call the Both function with a Both parameter then I see both outputs go up then down.

Seems like the firmware is working fine.
Could there be something else bugging your second garage door?

maybe you could comment out blynk too and try to hit the functions from the particle app or from the particle console/devices at : https://console.particle.io/devices

Gustavo.

You know you can use one function and just use different parameter sets to achieve the individual goals?

Also you should not put longish delays in a Particle.function().
Delayed tasks can be triggered from a function but should be handled outside - e.g. in loop() or a software timer.

1 Like

Scruff,
Thanks. I’m doing some testing on your suggestions now. I’m new to this so I’m not sure if I understand what you are saying.
Changes:
Took Blynk out
Removed OpenGarage2 and Both function and now will pass those as an argument.

The code compiled but I think I’m not doing the arguments correctly as the doors don’t open any longer. Do I need to put ‘else’ after each of the if statements?

char auth[] = "auth";
int OpenGarage (String command);
int OpenGarage2 (String command);
int Both (String command);
void setup()
{
  
  Particle.function("OpenGarage", OpenGarage);
  Serial.begin(9600);
  pinMode(D0, OUTPUT);
  pinMode(D3, OUTPUT);

}
void loop()
{

}
//This Opens single car garage
int OpenGarage(String command)
{
    if(command == "OpenGarage")
    {
        digitalWrite(D0, HIGH);
        digitalWrite(D0, LOW);
        return 1;
    }
    if(command == "OpenGarage2")
    {    
        digitalWrite(D3, HIGH);
        digitalWrite(D3, LOW);
        return 1;
        
    if(command == "Both")
    {
        digitalWrite(D0, HIGH);
        digitalWrite(D3, HIGH);
        digitalWrite(D0, LOW);
        digitalWrite(D3, LOW);
        return 1;
    }
   else  
   {
        digitalWrite(D0, LOW);
        digitalWrite(D3, LOW);
        return 0;
   }
    }
   else  
   {
        digitalWrite(D0, LOW);
        return 0;
   }

    return -1;
}

Your opening/closing curly braces don’t match, and the time bewteen HIGH and LOW is too short.
When I said you shouldn’t use delay() inside the function I didn’t mean to drop it all together, but have loop() or a software timer do the delayed setting-LOW.

Try this (there would be more elegant ways, but for clarity’s sake):

void resetAll() 
{
  digitalWrite(D0, LOW);
  digitalWrite(D3, LOW);
}

Timer switchOffTimer(1000, resetAll, true); // create a 1-second-one-shot timer that calls resetAll()

int OpenGarage(String command)
{
  int retVal = 0;

  if(command == "OpenGarage")
  {
    digitalWrite(D0, HIGH);
    retVal = 1;
  }
  else if(command == "OpenGarage2")
  {    
    digitalWrite(D3, HIGH);
    retVal = 2;
  }
  else if(command == "Both")
  {
    digitalWrite(D0, HIGH);
    digitalWrite(D3, HIGH);
    retVal = 1 & 2;
  }
  else // something went wrong, so be save and switch off immedeately
  {
    switchOffTimer.stop();
    resetAll(); 
  }
  
  if (retVal > 0) 
    switchOffTimer.start();

  return retVal;
}

PS: Remove the prototypes for OpenGarage2 and Both too.

Scruff, thanks again bud. You are awesome. I’m not real sure where to put that in the code.

Sadly, I have a different problem now.

With all the fun this weekend getting this project to work, I had my car and my wife’s car parked outside so that I could get ladders in to do the wiring. I mounted the project and parked the cars in the garage. I just found out that the garage door project is interfering with the car garage door buttons. The wifi on the photon is causing some noise and not allowing our garage door opener to work.

I had some problems when I moved in when my wife put a CFL in and her opener worked and mine did not (i have an older car).

Has anyone else had this experience? What did you do to get around it.

// I should probably post this in a new topic : /

These lines should go at the top of your project code

void resetAll() 
{
  digitalWrite(D0, LOW);
  digitalWrite(D3, LOW);
}

Timer switchOffTimer(1000, resetAll, true); // create a 1-second-one-shot timer that calls resetAll()

the rest just replaces your existing OpenGarage() implementation.

I doubt that the Photon’s WiFi would interfer with your existing remotes. I don’t know for sure, but I’d think these remote controles are not using a 2.4GHz band. But you can check.
Maybe the way you hooked up the relays on your existing system is the actual cause.
Have you wired the relays parallel or in series with the existing switches?

well, when I unplug the photon, everything works fine. It’s interfering somehow.

I think I found a spot in the garage that I can put the relay without it interfering much with the car openers. Unfortunately, you do have to get really close to the garage for the car opener to work. To be solved another day.

I tested the code and the both argument still doesn’t work. The single car and two car work fine.

void resetAll() 
{
  digitalWrite(D0, LOW);
  digitalWrite(D3, LOW);
}

Timer switchOffTimer(1000, resetAll, true); // create a 1-second-one-shot timer that calls resetAll()

char auth[] = "auth";
int OpenGarage (String command);

void setup()
{
  
  Particle.function("OpenGarage", OpenGarage);
  Serial.begin(9600);
  pinMode(D0, OUTPUT);
  pinMode(D3, OUTPUT);

}
void loop()
{

}

int OpenGarage(String command)
{
  int retVal = 0;

  if(command == "OpenGarage")
  {
    digitalWrite(D0, HIGH);
    retVal = 1;
  }
  else if(command == "OpenGarage2")
  {    
    digitalWrite(D3, HIGH);
    retVal = 2;
  }
  else if(command == "Both")
  {
    digitalWrite(D0, HIGH);
    digitalWrite(D3, HIGH);
    retVal = 1 & 2;
  }
  else // something went wrong, so be save and switch off immedeately
  {
    switchOffTimer.stop();
    resetAll(); 
  }
  
  if (retVal > 0) 
    switchOffTimer.start();

  return retVal;
}

Have you written Both exactly that way - without leading or trailing blanks?

Instead of the equality operator == you could also use this

int OpenGarage(String command)
{
  command.trim();  // remove possible leading/trailing white space
  ...
  else if (command.equalsIgnoreCase("both"))
  ...
}
1 Like

Hi Scott,
I cannot explain either the interference among the RF openers and the photon, but if it helps, I have the photon that controls my garage in my basement (I use it for other projects at the same time), and I wired the relay to the garage. Maybe you could do similar if you have another room (and you can wire it there).

what is a CFL?

Gustavo.

Cfl-compact fluorescent light
@ledfortr check your relay wiring, if you wired in series, common MUST go to common, and NO MUST go to NO, crossing these two doesn’t seem like a big deal, but in 15 years of gate experience, it causes more headaches than you think.

1 Like

MJones, How would I check to see that common goes to common? Is there a picture I can look at?

Here is the relay that I am using

It has 6 spots for wires. If I label them 1-6 from top to bottom of the picture. I have wired 2 and 5 directly together. One of the garages is on 3-4 with the red wire in 3 and white in 4. The other garage is wired into 1 and 6 with 1 being the red and 6 the white. Does that help?

Scruff, would i put the code in like this?

int OpenGarage(String command)
{
  command.trim();  // remove possible leading/trailing white space
  ...
  else if (command.equalsIgnoreCase("both"))
  ...
}
{
  int retVal = 0;

  if(command == "OpenGarage")
  {
    digitalWrite(D0, HIGH);
    retVal = 1;
  }
  else if(command == "OpenGarage2")
  {    
    digitalWrite(D3, HIGH);
    retVal = 2;
  }
  else if(command == "Both")
  {
    digitalWrite(D0, HIGH);
    digitalWrite(D3, HIGH);
    retVal = 1 & 2;
}

Or should i put it under the brackets where the int retVal = 0; starts?

2 and 5 are your commons, wiring them together won’t get you anything. You need to wire one garage door into 1 and 2, and the other into 4 and 5. 3 and 6 are normally closed, and are not used in an application like this.

No, these ellipses (...) are meant as a place hoolder for the code that already exists between the code lines explicitly stated.

So you just find these explicit lines, insert the “new” lines or replace the exiting lines that look similar (a.k.a. roughly do the same thing) with the “new” code.

1 Like

I won’t use blynk to open garage door.
I would use something like Particle Alexa to open garage door.
“alexa open garage door”.

For advance user:
Buy iPhone or Android car mount; Relay…

IOS


Android

Mac

I’m not using blynk any longer. I’m using ifttt with google home. Blynk has been taken out of the code so its not part of the problem.

gotcha!
I’ll do something right eventually.

int OpenGarage(String command)
{
  command.trim(); // remove possible leading/trailing white space
  int retVal = 0;

  if(command == "OpenGarage")
  {
    digitalWrite(D0, HIGH);
    retVal = 1;
  }
  else if(command == "OpenGarage2")
  {    
    digitalWrite(D3, HIGH);
    retVal = 2;
  }
  else if (command.equalsIgnoreCase("both"))
  {
    digitalWrite(D0, HIGH);
    digitalWrite(D3, HIGH);
    retVal = 1 & 2;
  }
  else // something went wrong, so be save and switch off immedeately
  {
    switchOffTimer.stop();
    resetAll(); 
  }
  
  if (retVal > 0) 
    switchOffTimer.start();

  return retVal;
}
1 Like