Switch stuck on [0]

Hey Blynk Community,

I have my Blynk setup and working right now to open my garage. It’s hooked up to IFTTT so that I can use voice commands. I can also just use curl on my phone to open/close the garage (instead of the full app).

The problem is, when I make the request via put, it only works once. I figured out that this is getting the switch stuck as “on” (when I open the app, it says “on” when it should say off).

Really, I guess I want more of a button than a switch (The way this works is the board is plugged into the same circuit that a regular garage button would be).

The only solution I can think of, is to chain IFTTT so that AFTER it sends the body as [“0”] it will send the body as [“1”] (which seems backwards to me, but this is how it needs to be set). If the switch is already 0, sending another 0 doesn’t cause the garage to open.

Is there a simpler way to fix this? I feel like I’m missing something obvious. Maybe it’s just because I’m misusing a switch function instead of a button?

Here is the code on the ESP8266 board:

#include <Blynk.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#define REST      1000    //Rest Between Inhalations.
char auth[] = "REDACTED";
char ssid[] = "REDACTED";
char pass[] = "REDACTED";

long rssi;

void setup()
{
  pinMode(0, OUTPUT);
  digitalWrite(12, LOW); //GPIO13
  pinMode(12, OUTPUT);       // Pin connected to the Relay
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,19));
}

// ---------[ Widget Button using  virtual pin # 7 ]--------------------

BLYNK_WRITE(V7)
{
  if (param.asInt())
  {
    delayMicroseconds(500000);
    digitalWrite(12, LOW); //GPIO13
    delayMicroseconds(500000);
  }
  else
  {
    delayMicroseconds(500000);
    digitalWrite(12, HIGH); //Close Relay
    delayMicroseconds(500000);
  }
}


// ==============( Void Loop  ) ====================================

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

The only difference between a “switch” and a “button” is whether or not it requires a manual or automatic reversal of state, but still two processes, one for each state…

And since a webhook pin command is only sending one “state” at a time, then regardless of whether you use a Button Widget in “switch” or “button” state, you will need to send TWO commands via Webhook to implement both states.

PS what’s with the delays before the digitalWrites? and as for the ones after, you should use non-blocking timers instead. I believe even delayMicroseconds() halts basic program flow.

@TeddJohnson

Instead of trying to send two commands, make your code do what you want.

Basically when the virtual pin is one/HIGH, have the code trigger the relay briefly, then have it send the virtual pin back to zero/low with a virtual write.

As I am not sure how you set up you button widget in the APP, results may vary. This is how I Open/Close my garage with IFTTT.

BLYNK_WRITE(V7) {
  if (param.asInt())
  {
  digitalWrite(12, LOW); // turn relay ON
  timer.setTimeout(500, openOff); // run this program after 500mS
  }
  
}

void openOff() {
  digitalWrite(12, HIGH); // turn relay OFF
  Blynk.virtualWrite(V7, 0);  // set virtual pin 7 LOW 
}
1 Like